Chapter 4 · Section 4 practice

Choosing K and Checking the Result

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. Read feature shape

Warm-up. Print X.shape.

Write your answer, then run it.

2. Fit a candidate K

Warm-up. Fit K=3 and print inertia.

Write your answer, then run it.

3. Count a candidate’s groups

Warm-up. Fit K=3 and print membership counts.

Write your answer, then run it.

4. Compare WCSS at two K values

Build your skills. Print WCSS for K=3 and K=5.

Write your answer, then run it.

5. Create elbow values

Build your skills. Print WCSS for K=1,2,3,4.

Write your answer, then run it.

6. Calculate a silhouette

Build your skills. Fit K=5 and print the mean silhouette score.

Write your answer, then run it.

7. Compare seeds

Build your skills. Print K=5 inertia with seeds 42 and 7.

Write your answer, then run it.

8. Inspect a small group

Challenge. Fit K=6 and print the smallest cluster size.

Write your answer, then run it.

9. Compare scaling choices

Challenge. Print K=5 inertia on raw and standardised features, then explain why the two numbers cannot rank scaling choices.

Write your answer, then run it.

10. Avoid a trivial K decision

Challenge. Fit K=1 and K equal to the number of distinct feature rows; print their WCSS. Explain why minimum WCSS alone is insufficient.

Write your answer, then run it.

Reference answers are locked until all 10 attempts are submitted.