Vikhyath_11

a7

Dec 12th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #program 7
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.cluster import KMeans
  5. # Data
  6. x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8])
  7. x2 = np.array([5, 4, 6, 6, 5, 8, 6, 7, 6, 7, 1, 2, 1, 2, 3, 2, 3])
  8. # Plot original data
  9. plt.scatter(x1, x2)
  10. plt.xlim([0, 10])
  11. plt.ylim([0, 10])
  12. plt.title('Dataset')
  13. plt.show()
  14. # KMeans clustering
  15. X = np.column_stack((x1, x2))
  16. kmeans = KMeans(n_clusters=3).fit(X)
  17. # Plot clustered data
  18. for i, label in enumerate(kmeans.labels_):
  19. plt.plot(x1[i], x2[i], color=['b', 'g', 'r'][label], marker=['o',
  20. 'v', 's'][label], ls='None')
  21. plt.xlim([0, 10])
  22. plt.ylim([0, 10])
  23. plt.show()
  24.  
  25.  
Advertisement
Add Comment
Please, Sign In to add comment