Guest User

Untitled

a guest
Feb 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.cluster import KMeans
  4.  
  5. # Generate data
  6. class1 = np.random.randn(1000, 2)
  7.  
  8. class2 = np.random.randn(1000, 2)
  9. class2[:,0] = 4+class2[:,0]
  10. class2[:,1] = 4+class2[:,1]
  11.  
  12. class3 = np.random.randn(1000, 2)
  13. class3[:,0] = -4+class3[:,0]
  14. class3[:,1] = 4+class3[:,1]
  15.  
  16. data = np.append( class1, class2, axis= 0)
  17. data = np.append( data, class3, axis= 0)
  18. print(data.shape)
  19.  
  20. # Plot the data
  21. plt.scatter(data[:,0], data[:,1])
  22. plt.show()
  23.  
  24. # Cluster
  25. kmeans = KMeans(n_clusters=1, random_state=0, verbose = 1).fit(data)
  26.  
  27. # Plot clustered results
  28. plt.scatter(data[:,0], data[:,1], c=kmeans.labels_)
  29. plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], c = 'r')
  30. plt.show()
  31.  
  32. # Show the cluster centers
  33. print(kmeans.cluster_centers_)
Add Comment
Please, Sign In to add comment