Horje
K-Means Clustering in Python – 3 clusters Code Example
K-Means Clustering in Python – 3 clusters
from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

Data = {'x': [25,34,22,27,33,33,31,22,35,34,67,54,57,43,50,57,59,52,65,47,49,48,35,33,44,45,38,43,51,46],
        'y': [79,51,53,78,59,74,73,57,69,75,51,32,40,47,53,36,35,58,59,50,25,20,14,12,20,5,29,27,8,7]
       }
  
df = DataFrame(Data,columns=['x','y'])
  
kmeans = KMeans(n_clusters=3).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)

plt.scatter(df['x'], df['y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.show()




Python

Related
python catch output to terminal Code Example python catch output to terminal Code Example
for loop only for first 10 python Code Example for loop only for first 10 python Code Example
compute tree height Code Example compute tree height Code Example
% operatior in python print Code Example % operatior in python print Code Example
libraries used in ANN with Keras Sequential Model Code Example libraries used in ANN with Keras Sequential Model Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10