How to get inertia value for each cluster of k-means using scikit-learn?

I use scikit for clustering (k-mean). When I run the code with the advanced option, it prints inertia for each iteration.

As soon as the algorithm is finished, I would like to get the inertia for each cluster formed (inertia values ​​k). How can i achieve this?

+5
source share
1 answer

From http://scikit-learn.org/stable/modules/clustering.html#k-means the K-dimensional algorithm aims to select centroids that minimize inertia or the intra-cluster sum of the square of the criterion.

So, for each iteration of kilometers there is only one value of inertia. Are you trying to get k centroids values?

from sklearn import datasets from sklearn.cluster import KMeans iris = datasets.load_iris() X = iris.data Y = iris.target kmeans = KMeans(n_clusters=3, random_state=0,verbose=0).fit(X) print(kmeans.inertia_) print(kmeans.cluster_centers_) #Each row is one centroid, so 3 clusters and 3 centroids 
0
source

Source: https://habr.com/ru/post/1258961/


All Articles