Chapter 4 · Section 3 practice

K-Means: Assign, Average, Repeat

Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.

Submitted: 0 / 10

1. Count labels

Warm-up. Print the number of fitted labels.

Write your answer, then run it.

2. Read centre shape

Warm-up. Print the centre array shape.

Write your answer, then run it.

3. Count group sizes

Warm-up. Print sorted cluster membership counts.

Write your answer, then run it.

4. Calculate a manual centroid

Build your skills. Print the coordinate means for A,B,C.

Write your answer, then run it.

5. Read the objective

Build your skills. Print inertia rounded to three decimals.

Write your answer, then run it.

6. Assign a new nearby point

Build your skills. Transform (1.5,1.5) with the fitted scaler, then predict its cluster.

Write your answer, then run it.

7. Check a new point manually

Build your skills. Transform (1.5,1.5) with the fitted scaler. Calculate its squared distances to both standardised centres, then print the nearest-centre index.

Write your answer, then run it.

8. Verify group means

Challenge. Print whether the original-coordinate group means equal the inverse-transformed centres in label order.

Write your answer, then run it.

9. Reconstruct WCSS

Challenge. Use each standardised row in X and its assigned centre to reconstruct WCSS.

Write your answer, then run it.

10. Distinguish assignment and refitting

Challenge. Save centres, predict a new point, and print whether centres stayed unchanged.

Write your answer, then run it.

Reference answers are locked until all 10 attempts are submitted.