When K-Means Fails
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 14 of 51.
K-means pictures every cluster as a round blob around its center. Darts make a bullseye with a ring around it, and k-means cuts them in half:
Each point joins its nearest center, so the border between two clusters is a straight line halfway between the centers. No straight line separates a ring from its middle.
Long, curved or very uneven groups break k-means the same way. It still returns clusters, so always look at the picture.
Challenge
MediumA drone spotted 200 seals on two curved sandbars. The starter draws the truth on the left panel.
- Cluster the seals with k-means, k = 2.
- On the
rightpanel, scatter each cluster, labelledcluster 0orcluster 1. - Mark the centers as black
"X"markers labelledcenters. Add a legend, then show.
Coddy describes your chart and counts seals kept with their own sandbar. Like this, for two crowds:
Try it yourself
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
from sklearn.cluster import KMeans
# 200 seals resting on two curved sandbars, seen from a drone (positions in 100 m)
X, sandbar = make_moons(n_samples=200, noise=0.07, random_state=4)
# Draws the true sandbars on ax
def draw_truth(ax):
ax.scatter(X[sandbar == 0, 0], X[sandbar == 0, 1], s=12, label="north bar")
ax.scatter(X[sandbar == 1, 0], X[sandbar == 1, 1], s=12, label="south bar")
ax.set_title("The truth")
ax.legend()
fig, (left, right) = plt.subplots(1, 2, figsize=(9, 4))
draw_truth(left)
right.set_title("K-means, k = 2")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd Ones3Reading Clusters
Profiling ClustersCenters in Real UnitsClusters vs Known LabelsWhen K-Means FailsRecap: Tea TastingPractice on your own: Online Python compiler