1.Cluster centres
IntermediateCluster the points [[1, 2], [1.5, 1.8], [1, 1], [8, 8], [9, 9.5], [8.5, 9]] into 2 groups with KMeans(n_clusters=2, n_init=10, random_state=0). Print the labels and the cluster centres rounded to 2 decimals, sorted by their first coordinate.
Show hint
kmeans.labels_ and kmeans.cluster_centers_; sort the centres with sorted(..., key=lambda c: c[0]).
Show solution
import numpy as np
from sklearn.cluster import KMeans
points = np.array([[1, 2], [1.5, 1.8], [1, 1], [8, 8], [9, 9.5], [8.5, 9]])
kmeans = KMeans(n_clusters=2, n_init=10, random_state=0).fit(points)
print(kmeans.labels_)
print(sorted(kmeans.cluster_centers_.round(2).tolist(), key=lambda c: c[0]))[1 1 1 0 0 0]
[[1.17, 1.6], [8.5, 8.83]]