Day 45: Cluster Plot in Python (K-Means Explained Simply)
Today we’re visualizing how machines group data automatically using K-Means clustering.
No labels.
No supervision.
Just patterns.
Let’s break it down ๐
๐ง What is Clustering?
Clustering is an unsupervised learning technique where the algorithm groups similar data points together.
Imagine:
-
Customers with similar buying habits
-
Students with similar scores
-
Products with similar features
The machine finds patterns without being told the answers.
๐ What is K-Means?
K-Means is one of the most popular clustering algorithms.
It works in 4 simple steps:
-
Choose number of clusters (K)
-
Randomly place K centroids
-
Assign points to nearest centroid
-
Move centroids to the average of assigned points
-
Repeat until stable
That’s it.
๐ What This Code Does
1️⃣ Import Libraries
numpy → create data
matplotlib → visualization
KMeans from sklearn → clustering algorithm
2️⃣ Generate Random Data
X = np.random.rand(100, 2)This creates:
-
100 data points
-
2 features (x and y coordinates)
So we get 100 dots on a 2D plane.
3️⃣ Create K-Means Model
๐ Create 3 clusters.
4️⃣ Train the Model
kmeans.fit(X)Now the algorithm:
-
Finds patterns
-
Groups points
-
Calculates cluster centers
5️⃣ Get Results
labels → Which cluster each point belongs to
centroids → Center of each cluster
6️⃣ Visualize the Clusters
plt.scatter(X[:, 0], X[:, 1], c=labels)Each cluster gets a different color.
Then we plot centroids using:
marker='X', s=200Big X marks = cluster centers.
๐ What the Graph Shows
-
Different colors → Different clusters
-
Big X → Center of each cluster
-
Points closer to a centroid belong to that cluster
The algorithm has automatically discovered structure in random data.
That’s powerful.
๐ง Core Learning From This
Don’t memorize the code.
Understand the pattern:
That’s the real workflow.
๐ Where K-Means Is Used in Real Life
-
Customer segmentation
-
Image compression
-
Market basket analysis
-
Recommendation systems
-
Anomaly detection
๐ก Why This Matters
Clustering is one of the first steps into Machine Learning.
If you understand this:
You’re no longer just plotting charts.
You’re analyzing patterns.


