K-Means Clustering —assignment1
Topic: Online Learning Platform — Student Engagement Segmentation
1. Business Scenario
"EduSmart" is an online learning platform offering video courses and quizzes. The platform has thousands of students, but the EdTech team has no idea how to categorize them.
They want to know:
- Which students are highly engaged?
- Which students are at risk of dropping out (low engagement)?
- Which students fall in between?
There is no existing label for "engagement level" — nobody has manually tagged the students. The team wants to use unsupervised learning (K-Means) to discover natural student groups based on their behavior, so they can:
- Send reminder emails to low-engagement students
- Offer advanced courses to highly engaged students
- Adjust course content based on group patterns
2. Dataset
Two behavioral features are tracked for each student:
- HoursWatched → Total hours of video content watched in a month
- QuizzesAttempted → Number of quizzes attempted in a month
| Student | HoursWatched | QuizzesAttempted |
|---|---|---|
| S1 | 1 | 1 |
| S2 | 2 | 1 |
| S3 | 2 | 2 |
| S4 | 3 | 2 |
| S5 | 1 | 2 |
| S6 | 15 | 10 |
| S7 | 16 | 11 |
| S8 | 17 | 12 |
| S9 | 18 | 13 |
| S10 | 16 | 12 |
| S11 | 7 | 5 |
| S12 | 8 | 5 |
| S13 | 8 | 6 |
| S14 | 9 | 6 |
| S15 | 7 | 4 |
| S16 | 22 | 18 |
| S17 | 23 | 19 |
| S18 | 24 | 20 |
| S19 | 25 | 21 |
| S20 | 23 | 20 |
| S21 | 2 | 0 |
| S22 | 9 | 7 |
| S23 | 17 | 13 |
| S24 | 24 | 19 |
| S25 | 1 | 0 |
Note: The number of natural groups (K) is not given to you. Part of this assignment is figuring it out yourself using data-driven methods — this mirrors real workplace tasks, where nobody hands you the "right" K.
3. Assignment Tasks
Part A — Data Preparation
- Load the dataset into a Pandas DataFrame.
- Check for missing values and basic statistics (
df.describe()). - Select the relevant features for clustering.
- Scale the features using
StandardScaler(important since both features should contribute equally).
Part B — Finding the Right K
- Use the Elbow Method: run K-Means for K = 1 to 10, record
inertia_, and plot the WCSS curve. - Use the Silhouette Score: run K-Means for K = 2 to 10 and print the silhouette score for each.
- Based on both methods, choose and justify the best value of K.
Part C — Building the Final Model
- Train the final
KMeansmodel using your chosen K. - Add a
Clustercolumn to the DataFrame. - Print the cluster centers (remember to inverse-transform if you scaled the data, so the centers are interpretable in real hours/quizzes).
Part D — Visualization
- Create a scatter plot of
HoursWatchedvsQuizzesAttempted, colored by cluster, with cluster centers marked clearly.
Part E — Business Interpretation
- Label each cluster in plain English (e.g., "Cluster 0 = Low Engagement / At Risk").
- Predict the cluster for a new student with
HoursWatched=10andQuizzesAttempted=7. - Write a short paragraph (5–6 sentences) recommending what EduSmart should do for each cluster.
4. Guiding Questions (answer in your submission)
- Why did you scale the data before clustering? What might go wrong if you didn't?
- Did the Elbow Method and Silhouette Score agree on the same K? If not, how did you decide?
- K-Means assumes clusters are roughly round/spherical in shape. Do you think that assumption fits this dataset? Why or why not?
- If EduSmart had 10 behavioral features instead of 2, how would that make choosing K and visualizing clusters harder?
- What is one limitation of K-Means that could cause it to mislabel a student's engagement group?
5. Code Skeleton
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
# ---------------------------------
# Part A: Data Preparation
# ---------------------------------
data = {
"HoursWatched": [1,2,2,3,1,15,16,17,18,16,7,8,8,9,7,22,23,24,25,23,2,9,17,24,1],
"QuizzesAttempted": [1,1,2,2,2,10,11,12,13,12,5,5,6,6,4,18,19,20,21,20,0,7,13,19,0]
}
df = pd.DataFrame(data)
print(df.describe())
X = df[____] # TODO: select features
scaler = StandardScaler()
X_scaled = scaler.____(X) # TODO: correct method (fit_transform)
# ---------------------------------
# Part B: Finding the Right K
# ---------------------------------
wcss = []
for k in range(1, 11):
model = KMeans(n_clusters=k, random_state=42, n_init=10)
model.fit(X_scaled)
wcss.append(model.____) # TODO: correct attribute for WCSS
plt.plot(range(1, 11), wcss, marker='o')
plt.xlabel("K")
plt.ylabel("WCSS")
plt.title("Elbow Method")
plt.show()
for k in range(2, 11):
model = KMeans(n_clusters=k, random_state=42, n_init=10)
labels = model.____(X_scaled) # TODO: correct method (fit_predict)
score = silhouette_score(X_scaled, labels)
print(f"K={k}, Silhouette Score={score:.3f}")
# ---------------------------------
# Part C: Final Model
# ---------------------------------
final_k = ____ # TODO: fill in your chosen K
model = KMeans(n_clusters=final_k, random_state=42, n_init=10)
df["Cluster"] = model.fit_predict(X_scaled)
centers_scaled = model.cluster_centers_
centers_original = scaler.____(centers_scaled) # TODO: correct method (inverse_transform)
centers_df = pd.DataFrame(centers_original, columns=X.columns)
print(centers_df)
# ---------------------------------
# Part D: Visualization
# ---------------------------------
plt.scatter(df["HoursWatched"], df["QuizzesAttempted"], c=df["Cluster"], cmap="viridis")
plt.scatter(centers_df["HoursWatched"], centers_df["QuizzesAttempted"],
c="red", marker="X", s=200, label="Centers")
plt.xlabel("Hours Watched")
plt.ylabel("Quizzes Attempted")
plt.title("Student Engagement Clusters")
plt.legend()
plt.show()
# ---------------------------------
# Part E: Predict New Student
# ---------------------------------
new_student = [[10, 7]]
new_student_scaled = scaler.____(new_student) # TODO: correct method (transform, NOT fit_transform)
prediction = model.predict(new_student_scaled)
print("Predicted Cluster:", prediction[0])
6. Deliverables
Submit a single notebook/script + short write-up containing:
| Item | Description |
|---|---|
| Code | Fully working K-Means pipeline (Parts A–E) |
| Elbow plot | Screenshot/plot image |
| Silhouette scores | Table or printed output for K=2–10 |
| Final cluster scatter plot | With labeled centers |
| Cluster interpretation | Plain-English label + recommendation per cluster |
| Guiding question answers | 5 short written answers |
7. Grading Rubric (100 points)
| Criteria | Points |
|---|---|
| Correct data preparation & scaling | 15 |
| Elbow Method implemented correctly | 15 |
| Silhouette Score implemented correctly | 15 |
| Justified choice of K | 10 |
| Final model + cluster centers (correctly un-scaled) | 15 |
| Visualization (clear, labeled, centers shown) | 10 |
| Business interpretation of each cluster | 10 |
| Guiding questions answered thoughtfully | 10 |
8. Submission Checklist
- [ ] Data loaded and explored
- [ ] Features scaled
- [ ] Elbow method plotted
- [ ] Silhouette scores computed and compared
- [ ] K chosen with justification
- [ ] Final model trained
- [ ] Cluster centers un-scaled and printed
- [ ] Scatter plot with clusters + centers
- [ ] New student prediction made
- [ ] Business interpretation written per cluster
- [ ] All 5 guiding questions answered
0 Comments