PythonPlaza - Python & AI

K-means clustering (unsupervised learning algorithm)


An unsupervised machine learning technique called K-Means Clustering assists in assembling data points into clusters according to their innate similarity.K-Mean is utilized when we have unlabeled data and the objective is to find hidden patterns or structures, as opposed to supervised learning, where models are trained using labeled data.

An online retailer, for instance, can divide its clientele into groups like ""Big Spenders," "Frequent Buyers," and "Budget Shoppers" according to their past purchases
"k" stands for the number of clusters or groups into which we wish to divide our objects.












Let's see an example with a sample data..














K-Means Customer Segmentation

Complete step-by-step calculation using 10 customers, 4 independent variables, and K = 3 clusters.

1. Customer Data

We use four independent variables:

CustomerIncomeSpendingPurchasesEngagement
C12222
C23223
C32322
C43333
C57777
C68787
C77878
C88888
C94949
C105859

Each customer is represented as:

Customer = (Income, Spending, Purchases, Engagement)

For example:

C1 = (2, 2, 2, 2)
C9 = (4, 9, 4, 9)

2. Choose the Number of Clusters

For this example:

K = 3

The three groups will eventually be interpreted as:

  1. Low-value customers
  2. High-value customers
  3. High-spending / highly engaged customers

3. Select Initial Centroids

We choose C1, C5, and C9 as the initial centroids.

Centroid 1

M₁ = (2, 2, 2, 2)

Centroid 2

M₂ = (7, 7, 7, 7)

Centroid 3

M₃ = (4, 9, 4, 9)

4. Euclidean Distance Formula

Because there are four independent variables, the distance between a customer and a centroid is:

d = √[(x₁-c₁)² + (x₂-c₂)² + (x₃-c₃)² + (x₄-c₄)²]

Example: C2 → Centroid 1

C2 = (3, 2, 2, 3), and M₁ = (2, 2, 2, 2).

d(C2,M₁) = √[(3-2)² + (2-2)² + (2-2)² + (3-2)²]

= √[1² + 0² + 0² + 1²]

= √2

= 1.414

C2 → Centroid 2

d(C2,M₂) = √[(3-7)² + (2-7)² + (2-7)² + (3-7)²]
= √[16 + 25 + 25 + 16]
= √82
= 9.055

C2 → Centroid 3

d(C2,M₃) = √[(3-4)² + (2-9)² + (2-4)² + (3-9)²]
= √[1 + 49 + 4 + 36]
= √90
= 9.487
C2 is assigned to Cluster 1 because 1.414 is the smallest distance.

5. Iteration 1 — Calculate All Distances

CustomerDistance to M₁Distance to M₂Distance to M₃Assigned Cluster
C10.00010.00010.2961
C21.4149.0559.4871
C31.0009.5399.6441
C42.0008.0008.6021
C510.0000.0005.0992
C611.0451.4146.3252
C711.0451.4144.4722
C812.0002.0005.8312
C910.2965.0990.0003
C1010.1493.6061.7323

Cluster 1

C1, C2, C3, C4

Cluster 2

C5, C6, C7, C8

Cluster 3

C9, C10

6. Calculate the New Centroid — Cluster 1

CustomerIncomeSpendingPurchasesEngagement
C12222
C23223
C32322
C43333
Income = (2+3+2+3)/4 = 10/4 = 2.50
Spending = (2+2+3+3)/4 = 10/4 = 2.50
Purchases = (2+2+2+3)/4 = 9/4 = 2.25
Engagement = (2+3+2+3)/4 = 10/4 = 2.50
New M₁ = (2.50, 2.50, 2.25, 2.50)

7. Calculate the New Centroid — Cluster 2

CustomerIncomeSpendingPurchasesEngagement
C57777
C68787
C77878
C88888
Income = (7+8+7+8)/4 = 7.50
Spending = (7+7+8+8)/4 = 7.50
Purchases = (7+8+7+8)/4 = 7.50
Engagement = (7+7+8+8)/4 = 7.50
New M₂ = (7.50, 7.50, 7.50, 7.50)

8. Calculate the New Centroid — Cluster 3

CustomerIncomeSpendingPurchasesEngagement
C94949
C105859
Income = (4+5)/2 = 4.50
Spending = (9+8)/2 = 8.50
Purchases = (4+5)/2 = 4.50
Engagement = (9+9)/2 = 9.00
New M₃ = (4.50, 8.50, 4.50, 9.00)

9. New Centroids After Iteration 1

ClusterIncomeSpendingPurchasesEngagement
Cluster 12.502.502.252.50
Cluster 27.507.507.507.50
Cluster 34.508.504.509.00

10. Iteration 2 — Recalculate Distances

Using the new centroids, we calculate the distances again.

Example: C1 → New M₁

C1 = (2,2,2,2)
M₁ = (2.5,2.5,2.25,2.5)

d = √[(2-2.5)² + (2-2.5)² + (2-2.25)² + (2-2.5)²]
= √[0.25 + 0.25 + 0.0625 + 0.25]
= √0.8125
= 0.901

C1 → New M₂

d = √[(2-7.5)² + (2-7.5)² + (2-7.5)² + (2-7.5)²]
= √121
= 11.000

C1 → New M₃

d = √[(2-4.5)² + (2-8.5)² + (2-4.5)² + (2-9)²]
= √103.75
= 10.186
C1 remains in Cluster 1 because 0.901 is the smallest distance.

11. Iteration 2 — All Distances

CustomerTo M₁To M₂To M₃Cluster
C10.90111.00010.1861
C20.90110.0509.3141
C30.90110.5369.5791
C41.1469.0008.4111
C59.1281.0004.3302
C610.1891.0005.5452
C710.1641.0003.7082
C811.1271.0005.0742
C99.4775.3850.8663
C109.2903.8730.8663
Convergence: The cluster assignments did not change from Iteration 1 to Iteration 2. Therefore, K-Means has converged for this example.

12. Final Customer Segmentation

Cluster 1 — Low-Value Customers

C1, C2, C3, C4

Centroid: (2.50, 2.50, 2.25, 2.50)

  • Low income
  • Low spending
  • Few purchases
  • Low engagement

Possible strategies: discounts, introductory offers, loyalty programs, cross-selling, and engagement campaigns.

Cluster 2 — High-Value Customers

C5, C6, C7, C8

Centroid: (7.50, 7.50, 7.50, 7.50)

  • High income
  • High spending
  • Frequent purchases
  • High engagement

Possible strategies: VIP programs, premium products, exclusive offers, early access, and personalized recommendations.

Cluster 3 — High-Spending / Highly Engaged

C9, C10

Centroid: (4.50, 8.50, 4.50, 9.00)

  • Moderate income
  • Very high spending
  • Moderate purchase frequency
  • Very high engagement

Possible strategies: premium products, upselling, personalized marketing, subscriptions, and loyalty rewards.

13. Final Results

CustomerIncomeSpendingPurchasesEngagementFinal Cluster
C122221
C232231
C323221
C433331
C577772
C687872
C778782
C888882
C949493
C1058593

14. Complete K-Means Mathematical Summary

Step 1 — Data Matrix

X = [
(2,2,2,2), (3,2,2,3), (2,3,2,2), (3,3,3,3),
(7,7,7,7), (8,7,8,7), (7,8,7,8), (8,8,8,8),
(4,9,4,9), (5,8,5,9)
]

Step 2 — Choose K

K = 3

Step 3 — Initial Centroids

M₁ = (2,2,2,2)
M₂ = (7,7,7,7)
M₃ = (4,9,4,9)

Step 4 — Distance Calculation

d = √Σ(xⱼ - cⱼ)²

Step 5 — Cluster Assignment

Cluster(Cᵢ) = arg minₖ d(Cᵢ,Mₖ)

Step 6 — Recalculate Centroids

Mₖ = (1/nₖ) Σ Xᵢ

Step 7 — Repeat

Repeat the distance and centroid calculations until the cluster assignments no longer change.

Final result: K-Means converged to 3 customer segments.

15. Business Interpretation

ClusterCustomersSegmentBusiness Value
1C1–C4Low-value / low engagementNeed nurturing
2C5–C8High-value / loyalHighest priority
3C9–C10High spending / highly engagedStrong upsell opportunity
Important ML concept: K-Means does not inherently know what a "good" or "bad" customer is. It mathematically groups customers based on similarity across the four independent variables. The business meaning of each cluster is interpreted after the clustering is completed.

USE CASE 1: Healthcare Patient Grouping using K-Means Clustering: Hospitals often need to group patients with similar characteristics to: Identify high-risk patients, Personalize treatment plans, Optimize resource allocation, Improve healthcare management

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

## Load data
#download from
#https://www.pythonplaza.com/healthcare_patient_dataset.html
df = pd.read_csv("patients_data.csv")

# Features used for clustering
X = df[['Age',
        'BMI',
        'Blood_Pressure',
        'Cholesterol',
        'Hospital_Visits']]

# Scale data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Train model
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
kmeans.fit(X_scaled)

# New patient?
new_patient = [[33, 28.0, 139, 210, 4]]

# Apply SAME scaling
new_patient_scaled = scaler.transform(new_patient)

# Predict cluster
cluster = kmeans.predict(new_patient_scaled)

print("Patient belongs to Cluster:", cluster[0])
#Example Output
Patient belongs to Cluster: 1


USE CASE 2: Use K-means clustering for customer segmentation in Market Basket Analysis. Instead of finding which products are purchased together (like Apriori or FP-Growth),use K-means to group customers based on their purchasing behavior. Once customers are clustered, you can create targeted promotions and personalized recommendations for each segment.




import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

# ----------------------------------
 # Step 1: Sample Market Basket Data 
# ----------------------------------
data = pd.DataFrame({
    'Customer': ['C001','C002','C003','C004','C005','C006','C007','C008'],
    'Bread': [12,10,11,1,0,2,6,5],
    'Milk': [10,8,9,2,1,1,5,6],
    'Eggs': [8,7,6,1,2,0,4,5],
    'Beer': [0,1,0,10,12,9,4,5],
    'Chips': [1,0,1,8,10,7,3,4]
})

#Load data
#You can also download from
#https://www.pythonplaza.com/sample_customer_shopping.html
data = pd.read_csv("customer_shopping.csv")

print("Original Data")
print(data)

# ----------------------------------
# Step 2: Select Features
# ----------------------------------
X = data[['Bread', 'Milk', 'Eggs', 'Beer', 'Chips']]


# ----------------------------------
# Step 3: Scale Features
# ----------------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)


# ----------------------------------
# Step 4: Train K-Means Model
# ----------------------------------

kmeans = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

kmeans.fit(X_scaled)


# ----------------------------------
# Step 5: Assign Clusters
# ----------------------------------

data['Cluster'] = kmeans.labels_

print("\nCluster Assignments")
print(data[['Customer', 'Cluster']])


# ----------------------------------
# Step 6: Cluster Profiles
# ----------------------------------

print("\nCluster Centers (Original Scale)")

centers = scaler.inverse_transform(kmeans.cluster_centers_)

cluster_profiles = pd.DataFrame(
    centers,
    columns=['Bread','Milk','Eggs','Beer','Chips']
)

print(cluster_profiles.round(2))


# ----------------------------------
# Step 7: Test New Customer
# ----------------------------------

new_customer = pd.DataFrame({
    'Bread': [11],
    'Milk': [9],
    'Eggs': [7],
    'Beer': [1],
    'Chips': [1]
})

# Scale using same scaler
new_customer_scaled = scaler.transform(new_customer)

# Predict cluster
predicted_cluster = kmeans.predict(new_customer_scaled)

print("\nNew Customer")
print(new_customer)

print(f"\nPredicted Cluster: {predicted_cluster[0]}")


# ----------------------------------
# Step 8: Recommendation Logic
# ----------------------------------

if predicted_cluster[0] == 0:
    print("Recommendation: Bread, Milk, Eggs promotions")
elif predicted_cluster[0] == 1:
    print("Recommendation: Beer and Chips promotions")
else:
    print("Recommendation: Mixed basket offers")



About Us  | Contact Us | Sitemap  | Privacy Policy