Centroid
- Machine Learning course
- Intermediate
- Lesson 49/112
- 5 min
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 centreThe distance matrix can also be given to another model as a new set of features.
Practice 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().
Starter code
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)
# buraya yazSample 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())Expected output
True 205
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
Explanation: At convergence the update step doesn't move the centre; the centre is the cluster's mean.
2 Get each example's distance to all centres.
D = km.____(X)
D = km.transform(X)
Explanation: KMeans.transform returns the distance matrix.
3 Match the expressions.
| Term | Meaning |
|---|---|
cluster_centers_ |
Centroid coordinates |
km.transform(X) |
Distance matrix |
D.min(axis=1) |
Distance to own centre |
inverse_transform |
Centroid in original unit |