gmm1
gmm1
NO:
DATE:
AIM:
To write a python program to implement Expectation Maximization algorithm for
clustering a given dataset using Gaussian Mixture Models (GMM).
ALGORITHM:
Use Matplotlib to plot the real class labels based on Petal Length vs. Petal Width.
Assign different colors to each class using a colormap.
Define n_clusters = 3.
Fit the K-Means model on the dataset.
Assign each data point to a cluster and visualize the 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.