Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. from sklearn.cluster import KMeans
  2. from sklearn import metrics
  3. from scipy.spatial.distance import cdist
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6.  
  7. x1 = [15, 19, 15, 5, 13, 17, 15, 12, 8, 6, 9, 13]
  8. x2 = [13, 16, 17, 6, 17, 14, 15, 13, 7, 6, 4, 12]
  9.  
  10. X = np.array(list(zip(x1, x2)))
  11.  
  12. distortions = []
  13. K = range(1,8)
  14. for i in K:
  15. model = KMeans(n_clusters=i)
  16. model.fit(X)
  17. distortions.append(sum(np.min(cdist(X, model.cluster_centers_, 'euclidean'), axis=1)) / X.shape[0])
  18.  
  19. plt.plot()
  20. plt.plot(K, distortions, 'bx-')
  21. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement