Clustering and the K-Means Algorithm
What Is Clustering?
Clustering is an unsupervised learning technique that groups data points based on their similarity. Points within a cluster should be more similar to one another under the chosen distance measure than to points in other clusters.
Common applications include:
- Customer segmentation
- Grouping news articles by topic
- Organizing images
- Exploring patterns in user behavior
Understanding K-Means
K-Means is a popular clustering algorithm that divides data into a specified number of clusters, represented by the value K.
The algorithm generally follows these steps:
- Select the number of clusters, K.
- Initialize K cluster centers, called centroids.
- Assign each data point to its nearest centroid.
- Recalculate each centroid using the points assigned to it.
- Repeat the assignment and recalculation steps until the clusters stabilize or a stopping condition is reached.
K-Means attempts to minimize the sum of squared distances between data points and their assigned cluster centers.
Implementing K-Means in Python
Install the required libraries if they are not already available:
pip install numpy matplotlib scikit-learn
Use the following example to group customers based on two illustrative features: annual spending and purchase frequency.
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
# Illustrative customer data:
# Annual spending and purchase frequency
X = [
[20, 2],
[25, 3],
[22, 2],
[80, 8],
[85, 9],
[78, 7],
[45, 5],
[50, 4],
[48, 5]
]
# Scale the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Create and train the model
model = KMeans(
n_clusters=3,
random_state=42,
n_init=10
)
labels = model.fit_predict(X_scaled)
print("Cluster assignments:", labels)
# Visualize the clusters
plt.scatter(
[row[0] for row in X],
[row[1] for row in X],
c=labels
)
plt.xlabel("Annual Spending")
plt.ylabel("Purchase Frequency")
plt.title("Customer Segmentation Using K-Means")
plt.show()Understanding the Output
The model assigns each customer a cluster label, such as 0, 1, or 2. These labels identify groups; they do not indicate a ranking or business category automatically.
You must inspect the characteristics of each group before assigning interpretations such as frequent buyers or low-spending customers.
Advantages and Limitations
Advantages:
- Relatively simple to understand and implement.
- Efficient for many datasets.
- Useful when the number of clusters can be estimated.
Limitations:
- Requires choosing the number of clusters in advance.
- Can be affected by outliers and feature scaling.
- Works best when clusters are reasonably compact and separated.
- Different initial centroids can produce different results.
Artificial Intelligence Tutorial
Vishalini A








