Reputation: 171
I have data points represented on a 2D graph which is shown as below.
1,1
1,2
2,1
2,2
3,3
8,8
9,8
8,9
9,9
EDIT:
Now, if I have two initial given clusters, let's say (1,1) and (2,1), I want to use K means to decide the clusters based on Euclidean Distances with termination condition of 3 iterations.
My approach was to create a pandas dataframe, apply kmeans with 2 clusters and note the centroids.
import pandas as pd
df = pd.DataFrame({'x': [1,1,2,2,3,8,9,8,9],
'y': [1,2,1,23,8,8,9,9]})
from sklearn.cluster import KMeans
kmeans = KMeans(2).fit(df.values)
But, I am stuck. How can i define my own customise function for the same.
I want to iterate it to three next iterations and note the calculations to reach the exact clusters in those three iterations.
Thanks
Upvotes: 0
Views: 66
Reputation: 2995
I understand your problem that you want to start your iterations by specifying custom centroids and also number of iterations ahead.
you can specify cluster centers using: KMeans.cluster_centers_
with initial clusters, unfortunately, you have to fit it once then, proceed to override cluster centers
Start with: you can specify computationally cheap values of n_init=1, max_iter=1 for 1st run, then run the iterations again after overriding those cluster centers by KMeans.fit(max_iter=3)
.
Upvotes: 1