Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Explore how algorithms discover hidden geometry, groups, and latent structures without human labels.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Number of clusters | Predefined count of groups | |
| Centroid of cluster k | The center coordinate of cluster k | |
| Set of points in cluster k | All observations assigned to group k |
Point x=3 assigned to centroid mu=2. Squared distance is (3 - 2)^2 = 1.
Result: Distance contribution to inertia is 1
K random points are chosen as initial cluster centers.
Every data point is assigned to its nearest centroid.
Centroids are recalculated as the mean of all points assigned to them.
Repeat until centroid positions stabilize.
Problem: Identifying disease subtypes among 20,000 gene expressions.
Solution: Hierarchical clustering identifies genetic cohorts responding to specialized therapies.
Problem: Zero-day cyberattacks have never been seen or labeled before.
Solution: Density-based anomaly detection flags network packets deviating from typical baseline traffic.
Interactive activity component
Cluster customer spend and visit frequency without any labels.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install scikit-learn numpy1 from sklearn.cluster import KMeans 2 import numpy as np
from sklearn.cluster import KMeans β Import K-Means
import numpy as np β NumPy array support
1 # Customer data: [Visits per month, Average Spend in Dollars] 2 customers = np.array([[1, 20], [2, 25], [1, 15], [15, 300], [18, 350], [14, 280]]) 3 kmeans = KMeans(n_clusters=2, random_state=0).fit(customers) 4 print("Cluster Centers (Centroids):\n", kmeans.cluster_centers_) 5 new_shopper = [[16, 320]] 6 print("Assigned Cluster for [16 visits, $320]:", kmeans.predict(new_shopper)[0])
# Customer data: [Visits per month, Average Spend in Dollars]
Data setup
customers = np.array([[1, 20], [2, 25], [1, 15], [15, 300], [18, 350], [14, 280]])
Casual vs VIP customers
kmeans = KMeans(n_clusters=2, random_state=0).fit(customers)
Run K-Means with K=2
print("Cluster Centers (Centroids):\n", kmeans.cluster_centers_)
Show centers
new_shopper = [[16, 320]]
New customer
print("Assigned Cluster for [16 visits, $320]:", kmeans.predict(new_shopper)[0])
Predict cluster
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # K-Means Clustering with Scikit-Learn 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install scikit-learn numpy 8 9 # Step 2: Import libraries 10 from sklearn.cluster import KMeans 11 import numpy as np 12 13 # Step 3: Run Demonstration 14 # Customer data: [Visits per month, Average Spend in Dollars] 15 customers = np.array([[1, 20], [2, 25], [1, 15], [15, 300], [18, 350], [14, 280]]) 16 kmeans = KMeans(n_clusters=2, random_state=0).fit(customers) 17 print("Cluster Centers (Centroids):\n", kmeans.cluster_centers_) 18 new_shopper = [[16, 320]] 19 print("Assigned Cluster for [16 visits, $320]:", kmeans.predict(new_shopper)[0])
Calculates centers for casual shoppers (~$20) and VIP shoppers (~$310).
Assigns new shopper with 16 visits and $320 spend to the VIP cluster.
Customer behavior matrix with two distinct customer cohorts
Cluster Centers: [[ 1.33 20.00] [15.67 310.00]] Assigned Cluster for [16 visits, $320]: 1
Without any supervisory intervention, K-Means discovered the high-value VIP segment and the low-frequency casual shopper segment.
Data, prediction, and the bridge to modern artificial intelligence.
Test your understanding with these questions. Remember, this is for learning, not examination!