When utilizing the sklearn class sklearn.cluster
for K-means clustering, a fitted k-means object has 3 attributes, including a numpy array of cluster centers (centers x features) named cluster_centers_
. However, these centers don't have an attached label.
My question is: are the centers (rows) in cluster_centers_
ordered by the label value? That is, does row 1 correspond to the center for the cluster labeled 1? Or are they placed in the array randomly? A pointer to any documentation would be more than sufficient.
Thanks.
kmeans = KMeans(n_clusters = k) y_pred = kmeans.fit_predict(X) Each instance is assigned to one of the five clusters. It receives a label as the index of the cluster it gets assigned to. We can see these labels: y_pred.
You can draw the points and the centers via matplotlib's scatter function. Colors can be assigned depending on the group calculated via kmeans . Here is an example (the kmeans function now also return the centroids). Here is an attempt to show the given target names together with the kmeans approximation.
I couldn't find the documentation but yes it is ordered by cluster.
So:
kmeans.cluster_centers_[0] = centroid of cluster 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With