Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
Comment

PREVIOUS NEXT
Code Example
Python :: python convert string to list of dictionaries 
Python :: datetime from float python 
Python :: finding random numbers python 
Python :: read image and resize 
Python :: add a column with fixed value pandas 
Python :: executing curl commands in python 
Python :: flask api 
Python :: random letters generator python 
Python :: pytest temp directory 
Python :: numpy multiply element wise 
Python :: count non nan values in column 
Python :: How to Get the Union of Sets in Python 
Python :: python suppress warnings in function 
Python :: Add PostgreSQL Settings in Django 
Python :: console-based animation-simple 
Python :: terminal commands for install python on cpanel 
Python :: python trim 
Python :: how to update data in csv file using python 
Python :: python flask models user 
Python :: django validators import 
Python :: pandas filter on two columns 
Python :: python plus 
Python :: numpy concatenation 
Python :: make_response is not defined django 
Python :: python input for competitive programming 
Python :: python list input print 
Python :: how ro have a incresing variable in python 
Python :: load png to python 
Python :: remove word from string in python 
Python :: get key from dict python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =