learning-it by example

#193 Machine learning and data science 6.2 min

Clustering: k-means and When It Fails, in Python

k-means matched 299 of 300 points on round clouds, but turned a small cloud of 40 into a cluster of 102.

1. What does k-means do after it picks k centres?
2. On three round clouds of 100 made-up points each, with k set to 3, how many of the 300 points did k-means put in the right group?
3. What does the scikit-learn documentation say k-means assumes about the shape of clusters?
4. A small tight cloud of 40 points sat next to a wide cloud of 400, and k-means was run with k set to 2. What cluster sizes did it give?
5. Two rings share one centre and k-means mixed them. Which single column fixed it in the video?
6. Why does the matched function try every renaming of the cluster labels?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Read how matched scores a clustering

    Copy the code folder first. Open check.py and read the matched function. Then answer in one sentence each: what does permutations(range(k)) produce, and why does the function keep the best total instead of just comparing labels with the truth?

    A hint

    Cluster names are arbitrary, so label 0 from k-means may be the truth's group 2.

  2. Change the random_state and predict

    In check.py change random_state=0 to random_state=1, then to 2, and run python run.py each time. Before you run, predict whether the three-cloud sizes and the matched count will change. Write down what you see and compare it with the first run.

    A hint

    The video says k-means starts from random centres and can stop in a local minimum, which is why it is run several times.

  3. Move the small cloud further away

    In data.py, in the uneven function, change the small cloud's centre from [5, 0] to [9, 0]. Predict whether the small cluster will still take about 60 points from the big cloud, then run python run.py and look at the last line.

    A hint

    Compare the new sizes and the matched count with 102 and 338, and 378 of 440.

  4. Make the rings further apart

    In data.py, in the rings function, change the radii [1.0, 3.0] to [1.0, 8.0]. Predict two things: whether each k-means cluster still mixes both rings, and whether the radius-only fix still matches 300 of 300. Run python run.py and compare.

    A hint

    The video says that in side runs the cut moved between seeds, so treat one run as one run.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Made-up two-dimensional points with a known answer; choosing k, scaling and other clustering methods are not shown.

Up next in Machine learning and data science

More in Machine learning and data science

All Machine learning and data science videos

Related videos