VTU Update 9-3-2021 | Reopening of Even Semester
Step by Step Procedure to apply for a scholarship in State Scholarship Portal – SSP – 2021
Step 1: How to create an account in State Scholarship Portal SSP 2021
Step 2: Upload documents in SSP portal for e-Attestation
Step 3: Log in to your account and apply for the scholarship
List of documents to be uploaded in e-attestation portal
Subscribe to Youtube and Telegram Channel for Regular Updates
Python for Beginners Video tutorial | |
Big Data Analytics Video Tutorial | Placement Video Tutorial |
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.
Python Sample Programs for Placement Preparation
- Python program to find the sum of elements of List
- Solution to Python Application Programming Question Paper Jan 2019 15CS664
- Python program to accept a string find the longest word and its length
- Python program to swap numbers using temporary variable
- Solution to Python Application Programming Question Paper July 2018