Program 7-EM Algorithm-K Means Algorithm
Program 7-EM Algorithm-K Means Algorithm
id
Chaibna. M
8) Apply EM algorithm to cluster a set of data stored in a CSV file.
Use the same data set for
clustering using k-Mcans 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. n
Mhi ubray (Usés Sattes rLes
bau a u h , Shg r a m d
import matplotlib.pvplot as plt i e CeAK e
from sklearn import datasets
from sklearn.cluster import KMeans
-climcnSien
import pandas as pd DatoFrame a
import numpy as np
dlata Sruture ie dkta us
allgnud n a tabulal shia e
vaiables
v aiabes as dots
aas dots
d o s ha Stalaicabhip bethoan
Sadte pod each a&ible
en dmenSicnd Ce ausfan ,
caltec
catte lo
SrattelPlOE_mahia
1 ngth
Pee Re Biala data
ylahet Vi
n
OASicou)
dundi
e n T t oJ a b e l
e t s l Length
,l2
x.Aabel
K-Means Clustenng
Stcukd
CLEiation=|
Petal Length
SrmetimA doke pornt Li be O, O-meen
i.e data pstnta
ho&mase ull be
#General EM for GMM Sakeled/
from sklearn import preprocessin8 data.
# transform your data such that its distribution will have a
# mean value 0 and standard deviation of 1.
scaler.fit(X Salingo
xsa = scaler.transform(XFF hanbtn he data
xS =pd.DataFrame(7Sa, columns =X.coumns dod ekoen 0 o
from sklearn.mixture import GaussianMixture h alla estimale ka
Bmm =GaussianMixture(n_components-3) T CWam
ansm mürhua Ushbubivn
gmm.fit(xs) PaamataS f UaLmcun du
Standotd
maan
Salast(*) CCrmule a
e upd alea
8 calca. by Coniei J
ron(x) pein tardendiZLicn
GMM Clusternng
Clac
2 6
Petal Length
based clustering matched the true
Observation: The GMM using EM algorithm
labels more closely than the Kmeans