Clustering

Clustering is an unsupervised learning technique that groups data points together based on similarity - without using any predefined labels.

Supervised vs Unsupervised

# Everything covered so far (Logistic Regression, KNN, SVM,
# Decision Trees) was supervised - the target label was known.
#
# Clustering is unsupervised - there is no target label;
# the algorithm discovers groupings on its own.

Implementing K-Means Clustering in Python

import pandas as pd
from sklearn.cluster import KMeans

df = pd.DataFrame({
    "AnnualIncome": [15, 16, 17, 40, 41, 42, 70, 71, 72],
    "SpendingScore": [39, 81, 6, 50, 55, 45, 20, 90, 10]
})

model = KMeans(n_clusters=3, random_state=42, n_init=10)
df["Cluster"] = model.fit_predict(df)

print(df)
print("Cluster centers:", model.cluster_centers_)

Common Use Cases

Clustering is widely used for customer segmentation, grouping similar documents or images, and detecting patterns in data before applying any supervised model.

Since there's no ground-truth label in clustering, choosing the right number of clusters usually relies on techniques like the elbow method rather than a straightforward accuracy score.

Coming Up Next

Now that you understand clustering, the next step is to look at how it fits into the broader picture of supervised vs unsupervised learning, along with the distance measures and linkage functions that power it.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course