Apply EM algorithm to cluster a set of data stored in a .CSV file.
Use the same data set for
clustering using k-Means algorithm. Compare the results of these two algorithms and
comment on the quality of clustering. You can add Python ML library API in the program.
from [Link] import load_iris
import pandas as pd
iris = load_iris()
df = [Link]([Link], columns=iris.feature_names)
df.to_csv("[Link]", index=False)
print("[Link] file created successfully")
# ============================================================
# EM Algorithm (Gaussian Mixture Model) vs K-Means Clustering
# ============================================================
# This program:
# 1. Loads data from a CSV file
# 2. Applies:
# a) EM Algorithm using Gaussian Mixture Model (GMM)
# b) K-Means Clustering
# 3. Compares clustering quality using:
# - Silhouette Score
# - Davies-Bouldin Index
# 4. Visualizes clustering results
# ------------------------------------------------------------
# Install required libraries if needed:
# pip install pandas scikit-learn matplotlib seaborn
# ============================================================
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import KMeans
from [Link] import GaussianMixture
from [Link] import StandardScaler
from [Link] import silhouette_score, davies_bouldin_score
from [Link] import PCA
# ============================================================
# STEP 1: LOAD DATASET
# ============================================================
# Replace with your CSV file path
# Example:
# data = pd.read_csv("[Link]")
data = pd.read_csv("[Link]")
print("\nFirst 5 Rows of Dataset:")
print([Link]())
# ============================================================
# STEP 2: PREPROCESSING
# ============================================================
# Keep only numerical columns
X = data.select_dtypes(include=[[Link]])
# Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# ============================================================
# STEP 3: APPLY K-MEANS CLUSTERING
# ============================================================
k = 3 # Number of clusters
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans_labels = kmeans.fit_predict(X_scaled)
# ============================================================
# STEP 4: APPLY EM ALGORITHM (GMM)
# ============================================================
gmm = GaussianMixture(n_components=k, random_state=42)
gmm_labels = gmm.fit_predict(X_scaled)
# ============================================================
# STEP 5: EVALUATION METRICS
# ============================================================
# -------- K-Means Metrics --------
kmeans_silhouette = silhouette_score(X_scaled, kmeans_labels)
kmeans_db = davies_bouldin_score(X_scaled, kmeans_labels)
# -------- GMM Metrics --------
gmm_silhouette = silhouette_score(X_scaled, gmm_labels)
gmm_db = davies_bouldin_score(X_scaled, gmm_labels)
# ============================================================
# STEP 6: DISPLAY RESULTS
# ============================================================
print("\n================ K-MEANS RESULTS ================")
print("Silhouette Score :", round(kmeans_silhouette, 4))
print("Davies-Bouldin Index :", round(kmeans_db, 4))
print("\n================ EM (GMM) RESULTS ================")
print("Silhouette Score :", round(gmm_silhouette, 4))
print("Davies-Bouldin Index :", round(gmm_db, 4))
# ============================================================
# STEP 7: VISUALIZATION USING PCA
# ============================================================
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
[Link](figsize=(12, 5))
# ---------- K-Means Plot ----------
[Link](1, 2, 1)
[Link](X_pca[:, 0], X_pca[:, 1],
c=kmeans_labels, cmap='viridis')
[Link]("K-Means Clustering")
[Link]("PCA Component 1")
[Link]("PCA Component 2")
# ---------- GMM Plot ----------
[Link](1, 2, 2)
[Link](X_pca[:, 0], X_pca[:, 1],
c=gmm_labels, cmap='viridis')
[Link]("EM Algorithm (GMM) Clustering")
[Link]("PCA Component 1")
[Link]("PCA Component 2")
plt.tight_layout()
[Link]()
# ============================================================
# STEP 8: COMPARISON AND COMMENTS
# ============================================================
print("\n================ COMPARISON ================")
if gmm_silhouette > kmeans_silhouette:
print("EM (GMM) produced better cluster separation "
"based on Silhouette Score.")
else:
print("K-Means produced better cluster separation "
"based on Silhouette Score.")
if gmm_db < kmeans_db:
print("EM (GMM) produced more compact clusters "
"based on Davies-Bouldin Index.")
else:
print("K-Means produced more compact clusters "
"based on Davies-Bouldin Index.")
print("\nGeneral Observation:")
print("""
1. K-Means:
- Works well for spherical and equally sized clusters.
- Faster and computationally efficient.
- Hard clustering: each point belongs to one cluster only.
2. EM Algorithm (GMM):
- Uses probability-based soft clustering.
- Handles overlapping and elliptical clusters better.
- More flexible but computationally expensive.
3. Clustering Quality:
- Higher Silhouette Score indicates better separation.
- Lower Davies-Bouldin Index indicates better clustering quality.
""")
Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set. Print
both correct and wrong predictions. Java/Python ML library classes can be used for this problem.
# ============================================================
# k-Nearest Neighbour (k-NN) Algorithm on Iris Dataset
# ============================================================
# This program:
# 1. Loads the Iris dataset
# 2. Splits dataset into training and testing sets
# 3. Applies k-NN classification
# 4. Prints:
# - Correct Predictions
# - Wrong Predictions
# 5. Displays Accuracy
#
# ------------------------------------------------------------
# Install required libraries if needed:
# pip install pandas scikit-learn
# ============================================================
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score
# ============================================================
# STEP 1: LOAD IRIS DATASET
# ============================================================
iris = load_iris()
X = [Link]
y = [Link]
target_names = iris.target_names
# Create DataFrame (optional)
df = [Link](X, columns=iris.feature_names)
df['target'] = y
print("First 5 Rows of Dataset:")
print([Link]())
# ============================================================
# STEP 2: SPLIT DATASET
# ============================================================
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.3,
random_state=42
# ============================================================
# STEP 3: APPLY k-NN ALGORITHM
# ============================================================
k=3
knn = KNeighborsClassifier(n_neighbors=k)
# Train the model
[Link](X_train, y_train)
# Predict on test data
y_pred = [Link](X_test)
# ============================================================
# STEP 4: PRINT CORRECT AND WRONG PREDICTIONS
# ============================================================
print("\n================ PREDICTION RESULTS ================\n")
correct = 0
wrong = 0
for i in range(len(y_test)):
actual = target_names[y_test[i]]
predicted = target_names[y_pred[i]]
if y_test[i] == y_pred[i]:
correct += 1
print(f"Correct Prediction --> "
f"Actual: {actual} | Predicted: {predicted}")
else:
wrong += 1
print(f"Wrong Prediction --> "
f"Actual: {actual} | Predicted: {predicted}")
# ============================================================
# STEP 5: DISPLAY ACCURACY
# ============================================================
accuracy = accuracy_score(y_test, y_pred)
print("\n===================================================")
print("Total Correct Predictions :", correct)
print("Total Wrong Predictions :", wrong)
print("Accuracy :", round(accuracy * 100, 2), "%")
print("===================================================")
Q) Implement the non-parametric Locally Weighted Regression algorithm in order to fit data
points. Select appropriate data set for your experiment and draw graphs.
# ============================================================
# Locally Weighted Regression (LWR / LOWESS)
# ============================================================
# This program:
# 1. Generates sample data
# 2. Implements Locally Weighted Regression
# 3. Predicts values using weighted linear regression
# 4. Draws graph for original data and fitted curve
# ------------------------------------------------------------
# Required Libraries:
# pip install numpy matplotlib
# ============================================================
import numpy as np
import [Link] as plt
# ============================================================
# STEP 1: GENERATE SAMPLE DATASET
# ============================================================
# Input data
X = [Link](-3, 3, 100)
# Non-linear function with noise
y = [Link](X) + [Link](0, 0.2, len(X))
# ============================================================
# STEP 2: DEFINE LOCALLY WEIGHTED REGRESSION FUNCTION
# ============================================================
def locally_weighted_regression(X, y, tau):
m = len(X)
# Add intercept term
X_mat = [Link](([Link](m), X)).T
predictions = []
# Predict for each point
for x_query in X:
# Compute weights using Gaussian kernel
weights = [Link](-(X - x_query)**2 / (2 * tau**2))
# Create diagonal weight matrix
W = [Link](weights)
# Theta calculation:
# theta = (X^T W X)^(-1) X^T W y
theta = [Link](X_mat.T @ W @ X_mat) @ (X_mat.T @ W @ y)
# Prediction
x_vec = [Link]([1, x_query])
y_pred = x_vec @ theta
[Link](y_pred)
return [Link](predictions)
# ============================================================
# STEP 3: APPLY LWR
# ============================================================
tau = 0.5 # Bandwidth parameter
y_pred = locally_weighted_regression(X, y, tau)
# ============================================================
# STEP 4: PLOT RESULTS
# ============================================================
[Link](figsize=(10, 6))
# Original data points
[Link](X, y, color='blue', label='Original Data')
# LWR fitted curve
[Link](X, y_pred, color='red', linewidth=3,
label='Locally Weighted Regression')
[Link]("Locally Weighted Regression")
[Link]("X")
[Link]("Y")
[Link]()
[Link](True)
[Link]()
# ============================================================
# STEP 5: DISPLAY OBSERVATION
# ============================================================
print("\n===================================================")
print("Locally Weighted Regression Completed Successfully")
print("Bandwidth (tau) =", tau)
print("===================================================")
print("""
Observation:
1. LWR is a non-parametric regression algorithm.
2. It fits a local model around every query point.
3. Nearby points get higher weights.
4. Smaller tau:
-> More flexible curve
-> May overfit
5. Larger tau:
-> Smoother curve
-> May underfit
""")