0
$\begingroup$

I read here how to show the number of clusters over $n$ columns.

I would like to know how to get in a table, the values of the clusters centers. Could someone help me with this?

$\endgroup$
1
  • $\begingroup$ The KMeans class stores the cluster centers in the cluster_centers_ attribute, which returns an array of shape (n_clusters, n_features) (see also the documentation). $\endgroup$
    – Oxbowerce
    Sep 10, 2022 at 15:42

1 Answer 1

0
$\begingroup$

sklearn.clusters.KMeans has an attribute cluster_centers_, which stores the array of cluster centers.

You can add them to the dataframe as new columns this way:

clusters = KMeans(n_clusters = n)
predict = clusters.fit_predict(data)
centers = pd.DataFrame(clusters.cluster_centers_[predict, :])
centers.index = data.index
data = pd.concat([data, centers], axis=1)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.