#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.
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.
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.
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.
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
- #182 Linear Regression From Scratch, Then scikit-learn
- #183 Precision, Recall, F1: Which Metric Matters?
- #184 Overfitting You Can See: Train vs Validation Error
- #185 Cross-Validation Done Right: The Leakage Mistake
- #186 Data Leakage: The Bug That Fakes Accuracy, Reproduced
- #187 Feature Engineering Beat 500 Trees, on Made-Up Data
Related videos
- #182 Linear Regression From Scratch, Then scikit-learn Machine learning and data science
- #186 Data Leakage: The Bug That Fakes Accuracy, Reproduced Machine learning and data science
- #187 Feature Engineering Beat 500 Trees, on Made-Up Data Machine learning and data science
- #183 Precision, Recall, F1: Which Metric Matters? Machine learning and data science
