1. Objectives
- Explain clustering as unsupervised learning: finding groups without labels.
- Group households with K-Means and read the cluster centres and sizes.
- Choose the number of clusters k with the elbow method and the silhouette score.
- Use the clusters to place new households and give advice.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Unsupervised learning | Learning from data WITHOUT answers (labels) | Grouping customers nobody has labelled |
Cluster | A group of similar items | Households that use a lot in summer only |
Centroid (centre) | The average point of a cluster | (22.7, 5.4) units per day |
K-Means | Algorithm that puts each point in the nearest of k centres and moves the centres | k = 4 groups |
Inertia | Total squared distance of points to their centres (lower = tighter clusters) | 336.6 for k = 4 |
Elbow method | Pick k where inertia stops dropping quickly | The bend at k = 4 |
Silhouette score | How well each point fits its own cluster vs the next one (-1 to 1, higher is better) | 0.727 |
3. Background
Learning without answers
In P02-P05 every example had an answer (rust / no rust, the digit, the units). That is supervised learning. In clustering there are NO answers: the algorithm only finds groups of similar items. The outline topic 'NN: Clustering' is done in MATLAB with self-organising maps; K-Means is the simplest and most widely used method and gives the same kind of result.
Real-world link
An electricity company (like MEPCO in Multan region) has millions of customers. Grouping them by how much they use in summer and winter helps it plan: which areas need more supply in June, who could benefit from an inverter AC, who uses electric heaters in January. households.csv has 60 houses with their average daily units in summer and in winter.
How K-Means works
1) Place k centres. 2) Give every point to its nearest centre. 3) Move each centre to the mean of its points. Repeat 2-3 until nothing changes. n_init=10 runs this 10 times from different starts and keeps the best; random_state fixes the starts so everyone gets the same answer.
Data files for this lab
households.csv
house,summer_units,winter_units H01,6.6,1.8 H02,5.9,3.5 H03,19.4,3.7 H04,20.7,5.9 H05,24.1,5.4 H06,8.5,4.2 H07,7.9,11.7 H08,19.6,6.1 H09,22.1,2.4 H10,4.8,2.6 ... (60 data rows in total - copy the full file from the lab folder) |
4. Time Plan (50 minutes)
Minutes | Activity | Part |
|---|---|---|
0-5 | Background: supervised vs unsupervised; how K-Means works | 1-3 |
5-10 | Guided warm-up: look at the data; first K-Means | Part A |
10-13 | Predict the output | Part B |
13-17 | Bug hunt | Part C |
17-47 | Graded tasks 1-3 | Part D |
47-50 | Wrap-up; post-lab questions set as homework | Part E |
Part A - Guided Warm-up (type, run, observe)
A1. Look at the households
Each point is one house: average units per day in summer (x) and in winter (y). Can you see groups?
import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv("households.csv") print(df.shape); print(df.head(3).to_string(index=False)) plt.figure(figsize=(5.5, 3.6)) plt.scatter(df["summer_units"], df["winter_units"], s=20) plt.xlabel("Summer: units per day"); plt.ylabel("Winter: units per day") plt.title("60 households (no labels)"); plt.grid(alpha=0.3) plt.tight_layout(); plt.savefig("houses.png", dpi=100) print("Saved houses.png") |
Output (60, 3) house summer_units winter_units H01 6.6 1.8 H02 5.9 3.5 H03 19.4 3.7 Saved houses.png |
A2. The first K-Means
Ask for 4 clusters. labels_ gives the cluster number of each house; cluster_centers_ the centre of each group.
import pandas as pd from sklearn.cluster import KMeans df = pd.read_csv("households.csv") X = df[["summer_units", "winter_units"]].to_numpy() km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X) print("centres (summer, winter):") print(km.cluster_centers_.round(1)) print("houses per cluster:", pd.Series(km.labels_).value_counts().sort_index().tolist()) print("first 10 labels:", km.labels_[:10]) |
Output centres (summer, winter): [[ 7.8 13.5] [22.7 5.4] [ 5.2 3.5] [30.1 16.3]] houses per cluster: [12, 20, 20, 8] first 10 labels: [2 2 1 1 1 2 0 1 1 2] |
Part B - Predict the Output
Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.
B1.
import numpy as np from sklearn.cluster import KMeans x = np.array([[1], [2], [10], [11]]) km = KMeans(n_clusters=2, n_init=10, random_state=0).fit(x) print(sorted(km.cluster_centers_.ravel().round(1).tolist())) |
My prediction |
B2.
import numpy as np points = np.array([1, 2, 10, 11]); centres = np.array([1.5, 1.5, 10.5, 10.5]) print(((points - centres) ** 2).sum()) |
My prediction |
B3.
import numpy as np centres = np.array([[5, 3], [23, 5], [8, 14], [30, 16]]) house = np.array([24, 6]) print(np.linalg.norm(centres - house, axis=1).round(1).argmin()) |
My prediction |
Part C - Bug Hunt
Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.
C1.
import pandas as pd from sklearn.cluster import KMeans df = pd.read_csv("households.csv") km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(df) # cluster the whole table |
Bug, why it happens, and the fixed line(s) |
C2.
import pandas as pd from sklearn.cluster import KMeans df = pd.read_csv("households.csv") X = df[["summer_units", "winter_units"]].to_numpy() km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X.T) # houses as columns? |
Bug, why it happens, and the fixed line(s) |
C3.
import pandas as pd from sklearn.cluster import KMeans df = pd.read_csv("households.csv") df["winter_month"] = df["winter_units"] * 30 # winter as units per MONTH X = df[["summer_units", "winter_month"]].to_numpy() km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X) print("houses per cluster:", pd.Series(km.labels_).value_counts().sort_index().tolist()) print("centres:"); print(km.cluster_centers_.round(1)) |
It runs and prints houses per cluster: [14, 18, 6, 22] centres: [[ 12.4 401.6] [ 18.1 184.2] [ 26.7 523.5] [ 10.6 91.8]] |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | The elbow method: how many groups? | 3 |
T2 | Medium | Name the groups | 3 |
T3 | Challenge | Check k with silhouette, then advise new customers | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): The elbow method: how many groups?
Fit K-Means for k = 1 to 7 (n_init=10, random_state=0). Print the inertia for each k, plot inertia against k (save task1.png) and write which k you choose and why.
Sample run (your output must match):
Output k=1 inertia 7342.3 k=2 inertia 2129.7 k=3 inertia 1130.9 k=4 inertia 336.6 k=5 inertia 268.3 k=6 inertia 224.6 k=7 inertia 199.2 Saved task1.png |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): Name the groups
Fit K-Means with k = 4. For each cluster print its centre, the number of houses and a name you choose from the centre (for example 'low users', 'AC users', 'heater users', 'heavy users'). Draw the houses coloured by cluster with the centres marked X (save task2.png).
Sample run (your output must match):
Output cluster 0: centre (7.8, 13.5) houses 12 -> heater users (winter) cluster 1: centre (22.7, 5.4) houses 20 -> AC users (summer only) cluster 2: centre (5.2, 3.5) houses 20 -> low users cluster 3: centre (30.1, 16.3) houses 8 -> heavy users (AC and heater) Saved task2.png |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): Check k with silhouette, then advise new customers
(a) Print the silhouette score for k = 2 ... 6 and say which k is best. (b) With k = 4, predict the group of three new houses: (25, 5), (8, 15) and (5, 3) units per day (summer, winter). Print the centre each one is nearest to and one line of advice (e.g. inverter AC, gas heater instead of electric, no change).
Sample run (your output must match):
Output k=2 silhouette 0.642 k=3 silhouette 0.674 k=4 silhouette 0.727 k=5 silhouette 0.606 k=6 silhouette 0.488 house [25, 5] -> cluster 1, centre (22.7, 5.4): consider an inverter AC and solar panels for summer house [8, 15] -> cluster 0, centre (7.8, 13.5): use a gas heater or better insulation instead of an electric heater house [5, 3] -> cluster 2, centre (5.2, 3.5): no change needed - a low-use tariff fits |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. What is the difference between supervised learning (P02-P05) and clustering?
Your answer |
E2. Explain the elbow method using your Task 1 numbers.
Your answer |
E3. Why must the features be on the same scale before K-Means?
Your answer |
Marking Rubric (applied to Part D)
Criterion | Full | Partial | None |
|---|---|---|---|
Code runs without errors | Runs fully | Small fixable error | Does not run |
Correct features, k and K-Means settings | All correct | One slip | Several wrong |
Plots and printed results | All present | Some missing | None |
Explanations (choice of k, names, advice) | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P06a with task1.py, task2.py, task3.py (or P06a.ipynb), households.csv, task1.png and task2.png.
- Upload the zipped folder to the LMS / Google Classroom by the end of the day.
- Copy Task 1 and Task 3 code and output into your Lab Book (Practical 06a page).
- Use n_init=10 and random_state=0 so your numbers match the sample runs.