0% found this document useful (0 votes)
3 views

gmm1

Uploaded by

2317061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

gmm1

Uploaded by

2317061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.

NO:
DATE:

AIM:
To write a python program to implement Expectation Maximization algorithm for
clustering a given dataset using Gaussian Mixture Models (GMM).

ALGORITHM:

Load & Prepare the Data

 Load the Iris dataset from sklearn.datasets.


 Convert the data into a Pandas DataFrame with feature names.
 Store target labels in a separate DataFrame.

Visualize the Real Data Distribution

 Use Matplotlib to plot the real class labels based on Petal Length vs. Petal Width.
 Assign different colors to each class using a colormap.

Apply K-Means Clustering

 Define n_clusters = 3.
 Fit the K-Means model on the dataset.
 Assign each data point to a cluster and visualize the results.

Apply Gaussian Mixture Model (GMM) Clustering

 Normalize the dataset using StandardScaler.


 Apply Gaussian Mixture Model (GMM) with n_components = 3.
 Predict cluster assignments and visualize the results.

Compare & Analyze Results

 Display the real class labels, K-Means clusters, and GMM clusters side by side.
 Observe how different clustering techniques group the data.
PROGRAM:
from sklearn.cluster import KMeans
from sklearn import preprocessing
from sklearn.mixture import GaussianMixture
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = load_iris()
X = pd.DataFrame(dataset.data, columns=['Sepal_Length', 'Sepal_Width', 'Petal_Length',
'Petal_Width'])
y = pd.DataFrame(dataset.target, columns=['Targets'])

plt.figure(figsize=(14, 7))
colormap = np.array(['red', 'lime', 'black'])

plt.subplot(1, 3, 1)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40)
plt.title('Real Classification')

plt.subplot(1, 3, 2)
kmeans_model = KMeans(n_clusters=3, random_state=42)
kmeans_model.fit(X)
pred_kmeans = np.choose(kmeans_model.labels_, [0, 1, 2]).astype(np.int64)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[pred_kmeans], s=40)
plt.title('K-Means Clustering')

scaler = preprocessing.StandardScaler()
X_scaled = scaler.fit_transform(X)
gmm = GaussianMixture(n_components=3, random_state=42)
gmm.fit(X_scaled)
y_cluster_gmm = gmm.predict(X_scaled)
plt.subplot(1, 3, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_cluster_gmm], s=40)
plt.title('GMM Classification')
plt.show()

OUTPUT:

RESULT:
Thus the python program to implement Expectation Maximization algorithm for clustering
a given dataset using Gaussian Mixture Models (GMM) is executed and implemented
successfully.

You might also like