Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K means clustering in scikit learn

I am trying to do k means clustering in scikit learn. Code:

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters = 10)
x = df.values
kmeans.fit(x.reshape(-1, 1))

If the parameter n_init = random, it chooses random initial centroids. Is there a way to fetch the initial centroids used?

like image 514
data_person Avatar asked Feb 20 '26 12:02

data_person


1 Answers

You can only get your cluster centers after fitting the KMeans object to your data.

Little trick !

So what you can do is set the parameter max_iter to 1. By default it is set to 300 and then centers may change at each iteration.

If you use only one iteration the algorithm will assign each sample to an initial center and then it will stop, not updating the centers.

Thus calling .cluster_centers_ will give back the initial centroids !

like image 98
MMF Avatar answered Feb 23 '26 01:02

MMF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!