Vikhyath_11

a8

Dec 12th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #program 8
  2. from sklearn.cluster import KMeans
  3. from sklearn.mixture import GaussianMixture
  4. import sklearn.metrics as metrics
  5. import pandas as pd
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. dataset = pd.read_csv("P5Data.csv")
  9. X = dataset.iloc[:, :-1]
  10. label = {'Setosa': 0,'Versicolor': 1, 'Virginica': 2}
  11. y = [label[c] for c in dataset.iloc[:, -1]]
  12. plt.figure(figsize=(14,7))
  13. colormap=np.array(['red','lime','black'])
  14. # REAL PLOT
  15. plt.subplot(1,3,1)
  16. plt.title('Real')
  17. plt.scatter(X.petal_length,X.petal_width,c=colormap[y])
  18. # K-PLOT
  19. model=KMeans(n_clusters=3, random_state=3425).fit(X)
  20. plt.subplot(1,3,2)
  21. plt.title('KMeans')
  22. plt.scatter(X.petal_length,X.petal_width,c=colormap[model.labels_])
  23. print('The accuracy score of K-Mean: ',metrics.accuracy_score(y,
  24. model.labels_))
  25. print('The Confusion matrixof K-Mean:\n',metrics.confusion_matrix(y,
  26. model.labels_))
  27. # GMM PLOT
  28. gmm=GaussianMixture(n_components=3, random_state=3425).fit(X)
  29. y_cluster_gmm=gmm.predict(X)
  30. plt.subplot(1,3,3)
  31. plt.title('GMM Classification')
  32. plt.scatter(X.petal_length,X.petal_width,c=colormap[y_cluster_gmm])
  33. print('The accuracy score of EM: ',metrics.accuracy_score(y,
  34. y_cluster_gmm))
  35. print('The Confusion matrix of EM:\n ',metrics.confusion_matrix(y,
  36. y_cluster_gmm))
Advertisement
Add Comment
Please, Sign In to add comment