Machine Learning
Centroid
A centroid is the center point of a cluster.
The centroid
The centroid is the cluster's "typical example": the feature mean of the examples in the cluster. In segmentation, clusters are named by interpreting the centroids: "high income, shops little" and so on.
for k, m in enumerate(km.cluster_centers_):
print(k, m.round(2))
# to read a centroid on scaled data:
print(sc.inverse_transform(km.cluster_centers_).round(0))
If clustering was done on scaled data, converting the centroids to the original unit with inverse_transform makes interpretation easier.
Distance to the centres
transform gives each example's distance to all the centres. The smallest distance shows the assigned cluster; a large one shows examples far even from their own cluster (possible anomalies):
D = km.transform(X) # (example, cluster) distance matrix
en_yakin = D.min(axis=1)
print(np.argsort(en_yakin)[-3:]) # the 3 examples farthest from their centre
The distance matrix can also be given to another model as a new set of features.
Exercise Centroid = mean
For the trained km, compute the mean of cluster 0's examples with X[km.labels_ == 0].mean(axis=0) and print with np.allclose that it equals cluster_centers_[0]. Then print the index of the example farthest from its centre with km.transform(X).min(axis=1).argmax().
Show example solution
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
X, y_gercek = make_blobs(n_samples=300, centers=4, cluster_std=0.8, random_state=0)
km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(X)
ort = X[km.labels_ == 0].mean(axis=0)
print(np.allclose(ort, km.cluster_centers_[0]))
print(km.transform(X).min(axis=1).argmax())
Test yourself
-
After KMeans training ends, what is a centroid equal to?
- The first example in the cluster
- The median
- A random point
- The mean of the examples in its cluster
Show answer
The mean of the examples in its cluster — At convergence the update step doesn't move the centre; the centre is the cluster's mean.
-
Get each example's distance to all centres.
D = km.____(X)
Show answer
D = km.transform(X)
-
Match the expressions.
- cluster_centers_
- Centroid coordinates
- km.transform(X)
- Distance matrix
- D.min(axis=1)
- Distance to own centre
- inverse_transform
- Centroid in original unit
Next lesson: Elbow method