fork download
  1. from sklearn.cluster import KMeans
  2. from sklearn.mixture import GaussianMixture
  3. import sklearn.metrics as metrics
  4. import pandas as pd
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7.  
  8. names = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width', 'Class']
  9.  
  10. dataset = pd.read_csv("8-dataset.csv", names=names)
  11.  
  12. X = dataset.iloc[:, :-1]
  13. # until last column
  14.  
  15. label = {'Iris-setosa': 0,'Iris-versicolor': 1, 'Iris-virginica': 2}
  16.  
  17. y = [label[c] for c in dataset.iloc[:, -1]]
  18.  
  19. # Set the size of the plot
  20.  
  21. plt.figure(figsize=(14,7))
  22.  
  23. # Create a colormap
  24.  
  25. colormap=np.array(['red','lime','black'])
  26.  
  27. # REAL PLOT
  28. # Plot the Original Classifications
  29.  
  30. plt.subplot(1,3,1)
  31. # The subplot() function takes three arguments that describes the layout of the figure.
  32. #The layout is organized in rows and columns, which are represented by the first and second argument.
  33. # The third argument represents the index of the current plot.
  34.  
  35. plt.title('Real')
  36. plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y])
  37.  
  38. # Based on petal data compare with target data
  39.  
  40. # K-PLOT
  41. model=KMeans(n_clusters=3, random_state=0).fit(X)
  42. plt.subplot(1,3,2)
  43. plt.title('KMeans')
  44. plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_])
  45. #  c=colormap[model.labels_] for the predicted classess.
  46.  
  47.  
  48.  
  49.  
  50. print('The accuracy score of K-Mean: ',metrics.accuracy_score(y, model.labels_))
  51. print('The Confusion matrixof K-Mean:\n',metrics.confusion_matrix(y, model.labels_))
  52.  
  53. # GMM PLOT
  54. # Expectation-Maximization (EM) algorithm is based on Gaussian mixture model
  55. gmm=GaussianMixture(n_components=3, random_state=0).fit(X)
  56. y_cluster_gmm=gmm.predict(X)
  57. plt.subplot(1,3,3)
  58. plt.title('GMM Classification')
  59. plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm])
  60.  
  61. print('The accuracy score of EM: ',metrics.accuracy_score(y, y_cluster_gmm))
  62. print('The Confusion matrix of EM:\n ',metrics.confusion_matrix(y, y_cluster_gmm))
  63.  
Success #stdin #stdout 0.02s 26148KB
stdin
Standard input is empty
stdout
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
import sklearn.metrics as metrics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

names = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width', 'Class']

dataset = pd.read_csv("8-dataset.csv", names=names)

X = dataset.iloc[:, :-1] 
# until last column

label = {'Iris-setosa': 0,'Iris-versicolor': 1, 'Iris-virginica': 2} 

y = [label[c] for c in dataset.iloc[:, -1]]

# Set the size of the plot

plt.figure(figsize=(14,7))

# Create a colormap

colormap=np.array(['red','lime','black'])

# REAL PLOT
# Plot the Original Classifications

plt.subplot(1,3,1) 
# The subplot() function takes three arguments that describes the layout of the figure.
#The layout is organized in rows and columns, which are represented by the first and second argument.
# The third argument represents the index of the current plot.

plt.title('Real')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y])

# Based on petal data compare with target data

# K-PLOT
model=KMeans(n_clusters=3, random_state=0).fit(X)
plt.subplot(1,3,2)
plt.title('KMeans')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[model.labels_])
#  c=colormap[model.labels_] for the predicted classess.




print('The accuracy score of K-Mean: ',metrics.accuracy_score(y, model.labels_))
print('The Confusion matrixof K-Mean:\n',metrics.confusion_matrix(y, model.labels_))

# GMM PLOT 
# Expectation-Maximization (EM) algorithm is based on Gaussian mixture model 
gmm=GaussianMixture(n_components=3, random_state=0).fit(X)
y_cluster_gmm=gmm.predict(X)
plt.subplot(1,3,3)
plt.title('GMM Classification')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm])

print('The accuracy score of EM: ',metrics.accuracy_score(y, y_cluster_gmm))
print('The Confusion matrix of EM:\n ',metrics.confusion_matrix(y, y_cluster_gmm))