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("D:\iris.csv",names=names)
X=dataset.iloc[:,:-1]
label={'Iris-setosa':0,'Iris-versicolor':1,'Iris-virginica':2}
y=[label[c] for c in dataset.iloc[:,-1]]
plt.figure(figsize=(14,7))
colormap=np.array(['yellow','red','purple'])
plt.subplot(1,3,1)
plt.title('real')
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y])
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_])
print('The accuracy score of K-mean:',metrics.accuracy_score(y,model.labels_))
print('the confusion matrix of k-mean:\n',metrics.confusion_matrix(y,model.labels_))
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))