Python Program to Implement the K-Means and Estimation & MAximization Algorithm
Exp. No. 8. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering using the k-Means algorithm. Compare the results of these two algorithms and comment on the quality of clustering. You can add Java/Python ML library classes/API in the program.
Python Program to Implement and Demonstrate K-Means and EM Algorithm Machine Learning
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] 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(['red','lime','black']) # REAL PLOT plt.subplot(1,3,1) plt.title('Real') plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y]) # 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_]) 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 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))
Output
The accuracy score of K-Mean: 0.24
The Confusion matrixof K-Mean:
[[ 0 50 0]
[48 0 2]
[14 0 36]]
The accuracy score of EM: 0.36666666666666664
The Confusion matrix of EM:
[[50 0 0]
[ 0 5 45]
[ 0 50 0]]
Click Here to Download Iris Dataset
Summary
This tutorial discusses how to Implement and demonstrate the K-Means and EM Algorithm in Python. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.