Vikhyath_11

a9

Dec 12th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #program 9
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import scipy.cluster.hierarchy as sch
  6. from sklearn.cluster import AgglomerativeClustering
  7. # Importing the dataset
  8. dataset = pd.read_csv('P9Data.csv')
  9. X = dataset.iloc[:, [3,4]].values
  10. # Dendrogram
  11. sch.dendrogram(sch.linkage(X, method='ward'))
  12. plt.title('Dendrogram')
  13. plt.xlabel('Customers')
  14. plt.ylabel('Euclidean distances')
  15. plt.show()
  16. # Fitting Hierarchical Clustering
  17. hc = AgglomerativeClustering(n_clusters=5, linkage='ward')
  18. y_hc = hc.fit_predict(X)
  19. # Visualizing the clusters
  20. colors = ['r', 'b', 'g', 'c', 'm']
  21. for i in range(5):
  22. plt.scatter(X[y_hc == i, 0], X[y_hc == i, 1], s=100, c=colors[i],
  23. label=f'Cluster {i+1}')
  24.  
  25. plt.title('Clusters of customers')
  26. plt.xlabel('Annual Income (k$)')
  27. plt.ylabel('Spending Score (1-100)')
  28. plt.legend()
  29. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment