Guest User

Untitled

a guest
Jun 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. > from copy import deepcopy
  2. import numpy as np
  3. import matplotlib.pyplot as plot
  4. import pandas as pd
  5. from sklearn.cluster import KMeans
  6. #importing Dataset
  7. dataset = pd.read_csv('csvProva2.csv')
  8. X = dataset.iloc[:, [10,11]].values #colonne che mi interessano
  9.  
  10. #Find the number of clusters
  11. wcss = []
  12.  
  13. for i in range (1,16): #15 cluster
  14. kmeans = KMeans(n_clusters = i, init='k-means++', random_state=0)
  15. kmeans.fit(X)
  16. wcss.append(kmeans.inertia_)
  17.  
  18. plot.plot(range(1,16),wcss)
  19. plot.title('Elbow Method')
  20. plot.xlabel('Number of clusters')
  21. plot.ylabel('wcss')
  22. plot.show()
  23.  
  24. #KMeans clustering
  25. kmeans= KMeans(n_clusters=4,init='k-means++', random_state=0)
  26. y=kmeans.fit_predict(X)
  27.  
  28. plot.scatter(X[y == 0,0], X[y==0,1], s=25, c='red', label='Cluster 1')
  29. plot.scatter(X[y == 1,0], X[y==1,1], s=25, c='blue', label='Cluster 2')
  30. plot.scatter(X[y == 2,0], X[y==2,1], s=25, c='magenta', label='Cluster 3')
  31. plot.scatter(X[y == 3,0], X[y==3,1], s=25, c='cyan', label='Cluster 4')
  32.  
  33. plot.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=25, c='yellow', label='Centroid')
  34. plot.title('KMeans Clustering')
  35. plot.xlabel('Acousticness')
  36. plot.ylabel('Danceability')
  37. plot.legend()
  38. plot.show()
Add Comment
Please, Sign In to add comment