0% found this document useful (0 votes)
17 views40 pages

Data Analysis with KNN and Decision Trees

Uploaded by

karthiky
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views40 pages

Data Analysis with KNN and Decision Trees

Uploaded by

karthiky
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Compute Central Tendency Measures: Mean, Median, Mode Measure of Dispersion:


Variance, Standard Deviation.

PROGRAM:

import numpy as np

import statistics as stats

# Example data

data = [12, 15, 11, 18, 14, 16, 12, 19, 20, 15, 14, 16]

# Central Tendency Measures:

mean = [Link](data) # Mean

median = [Link](data) # Median

mode = [Link](data) # Mode

# Dispersion Measures:

variance = [Link](data) # Variance

std_dev = [Link](data) # Standard Deviation

# Output results

print(f"Mean: {mean}")

print(f"Median: {median}")

print(f"Mode: {mode}")

print(f"Variance: {variance}")

print(f"Standard Deviation: {std_dev}")

OUTPUT:

Mean: 15.0

Median: 15.0

Mode: 12

Variance: 7.736363636363637

Standard Deviation: 2.7827450531799757


2. Apply the following Pre-processing techniques for a given dataset.

a. Attribute selection

b. Handling Missing Values

c. Discretization

d. Elimination of Outliers

PROGRAM:

import pandas as pd

import numpy as np

from [Link] import SimpleImputer

from [Link] import KBinsDiscretizer

# Sample dataset

data = {

'Age': [25, 30, 35, [Link], 40, 50, 60, 22, [Link], 28],

'Income': [50000, 60000, 80000, 90000, [Link], 120000, 140000, 65000, 70000, 50000],

'Gender': ['M', 'F', 'M', 'F', 'M', 'M', 'F', 'M', 'M', 'F'],

'Score': [88, 92, 95, 88, 75, 84, 80, 91, 89, 70]

# Create a DataFrame

df = [Link](data)

print("Original Data:")

print(df)

# a. Attribute Selection (Selecting only relevant features)

# For simplicity, let's assume we are interested in 'Age' and 'Income' only

df_selected = df[['Age', 'Income']]

print("\nSelected Attributes:")

print(df_selected)
# b. Handling Missing Values

# Impute missing values using the mean strategy for numerical columns

imputer = SimpleImputer(strategy='mean')

# We can use 'median' or 'most_frequent' as well

df_selected_imputed = [Link](imputer.fit_transform(df_selected),
columns=df_selected.columns)

print("\nData after Handling Missing Values:")

print(df_selected_imputed)

# c. Discretization (Binning continuous variables like 'Age' and 'Income')

# We will use KBinsDiscretizer to convert continuous variables into discrete bins

discretizer = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')

df_discretized = [Link](discretizer.fit_transform(df_selected_imputed),
columns=df_selected_imputed.columns)

print("\nData after Discretization:")

print(df_discretized)

# d. Elimination of Outliers

# We'll use the IQR method to detect and remove outliers for the 'Age' and 'Income'
columns

# Calculate IQR

Q1 = df_selected_imputed.quantile(0.25)

Q3 = df_selected_imputed.quantile(0.75)

IQR = Q3 - Q1

# Define outlier conditions

outlier_condition = ((df_selected_imputed < (Q1 - 1.5 * IQR)) | (df_selected_imputed > (Q3


+ 1.5 * IQR)))

# Remove rows with outliers

df_no_outliers = df_selected_imputed[~outlier_condition.any(axis=1)]
print("\nData after Eliminating Outliers:")

print(df_no_outliers)

OUTPUT:

Original Data:

Age Income Gender Score

0 25.0 50000 M 88

1 30.0 60000 F 92

2 35.0 80000 M 95

3 NaN 90000 F 88

4 40.0 NaN M 75

5 50.0 120000 M 84

6 60.0 140000 F 80

7 22.0 65000 M 91

8 NaN 70000 M 89

9 28.0 50000 F 70

Selected Attributes:

Age Income

0 25.0 50000

1 30.0 60000

2 35.0 80000

3 NaN 90000

4 40.0 NaN

5 50.0 120000

6 60.0 140000

7 22.0 65000
8 NaN 70000

9 28.0 50000

Data after Handling Missing Values:

Age Income

0 25.0 50000.0

1 30.0 60000.0

2 35.0 80000.0

3 35.6 90000.0

4 40.0 83571.4

5 50.0 120000.0

6 60.0 140000.0

7 22.0 65000.0

8 35.6 70000.0

9 28.0 50000.0

Data after Discretization:

Age Income

0 0 0

1 1 0

2 2 1

3 1 1

4 2 1

5 2 2

6 2 2

7 0 1

8 1 1
9 0 0

Data after Eliminating Outliers:

Age Income

0 25.0 50000.0

1 30.0 60000.0

2 35.0 80000.0

3 35.6 90000.0

5 50.0 120000.0

6 60.0 140000.0

7 22.0 65000.0

8 35.6 70000.0

9 28.0 50000.0

3. Apply KNN algorithm for classification and regression


PROGRAM:

KNN for Classification:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import KNeighborsClassifier

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link]

y = [Link]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and fit the KNN Classifier

knn_classifier = KNeighborsClassifier(n_neighbors=5)

# Use 5 nearest neighbors

knn_classifier.fit(X_train, y_train)

# Make predictions

y_pred = knn_classifier.predict(X_test)

# Evaluate the model

print("Classification Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))

OUTPUT:
Classification Accuracy: 1.0

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

accuracy 1.00 45

macro avg 1.00 1.00 1.00 45

weighted avg 1.00 1.00 1.00 45

KNN for Regression:


# Import necessary libraries

from [Link] import load_diabetes

from sklearn.model_selection import train_test_split

from [Link] import KNeighborsRegressor

from [Link] import mean_squared_error, r2_score

# Load the Diabetes dataset

diabetes = load_diabetes()

X = [Link]

y = [Link]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and fit the KNN Regressor

knn_regressor = KNeighborsRegressor(n_neighbors=5)

# Use 5 nearest neighbors

knn_regressor.fit(X_train, y_train)

# Make predictions

y_pred = knn_regressor.predict(X_test)

# Evaluate the model

print("Mean Squared Error:", mean_squared_error(y_test, y_pred))

print("R² Score:", r2_score(y_test, y_pred))

OUTPUT:

Mean Squared Error: 3222.117894736842

R² Score: 0.4031244536507893
4. Demonstrate decision tree algorithm for a classification problem and perform
parameter tuning for better results

PROGRAM:

Decision Tree for Classification:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import DecisionTreeClassifier

from [Link] import accuracy_score, classification_report

from [Link] import plot_tree

import [Link] as plt

# Load the Iris dataset

iris = load_iris()

X = [Link]

y = [Link]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and train the Decision Tree Classifier

dt_classifier = DecisionTreeClassifier(random_state=42)

dt_classifier.fit(X_train, y_train)

# Make predictions

y_pred = dt_classifier.predict(X_test)

# Evaluate the model

print("Initial Model Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))

# Visualize the Decision Tree

[Link](figsize=(12, 8))
plot_tree(dt_classifier, feature_names=iris.feature_names, class_names=iris.target_names,
filled=True)

[Link]()

OUTPUT:

Initial Model Accuracy: 1.0

Classification Report:

PRECISION RECALL F1-SCORE SUPPORT

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

ACCURACY 1.00 45

MACRO AVG 1.00 1.00 1.00 45

WEIGHTED AVG 1.00 1.00 1.00 45


Hyperparameter Tuning:

# Import GridSearchCV for hyperparameter tuning

from sklearn.model_selection import GridSearchCV

# Define the parameter grid

param_grid = {

'criterion': ['gini', 'entropy'],

'max_depth': [None, 3, 5, 10],

'min_samples_split': [2, 5, 10],

'min_samples_leaf': [1, 2, 4],

# Initialize the Decision Tree Classifier

dt_classifier = DecisionTreeClassifier(random_state=42)

# Perform Grid Search with Cross Validation

grid_search = GridSearchCV(estimator=dt_classifier, param_grid=param_grid, cv=5,


scoring='accuracy', n_jobs=-1)

grid_search.fit(X_train, y_train)

# Print the best parameters and best cross-validation score

print("Best Parameters:", grid_search.best_params_)

print("Best Cross-Validation Accuracy:", grid_search.best_score_)

# Evaluate the best model on the test set

best_model = grid_search.best_estimator_

y_pred_best = best_model.predict(X_test)

print("Test Accuracy with Best Model:", accuracy_score(y_test, y_pred_best))

print("\nClassification Report with Best Model:\n", classification_report(y_test,


y_pred_best))

# Visualize the tuned Decision Tree

[Link](figsize=(12, 8))
plot_tree(best_model, feature_names=iris.feature_names, class_names=iris.target_names,
filled=True)

[Link]()

OUTPUT:

Best Parameters: { 'criterion': ['gini', 'entropy'],'max_depth': [None, 3, 5, 10],


'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4]}

Best Cross-Validation Accuracy: 0.9428571428571428

Test Accuracy with Best Model: 1.0

Classification Report with Best Model:

PRECISION RECALL F1-SCORE SUPPORT

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

ACCURACY 1.00 45

MACRO AVG 1.00 1.00 1.00 45

WEIGHTED AVG 1.00 1.00 1.00 45


[Link] decision tree algorithm for a regression problem

PROGRAM:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import DecisionTreeRegressor

from [Link] import mean_squared_error, r2_score

import [Link] as plt

from [Link] import plot_tree

# Load the Iris dataset

iris = load_iris()

X = [Link] # All features

y = [Link][:, 2]

# Use 'petal length' as the target variable for regression

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and train the Decision Tree Regressor

dt_regressor = DecisionTreeRegressor(random_state=42)

dt_regressor.fit(X_train, y_train)

# Make predictions on the test set

y_pred = dt_regressor.predict(X_test)

# Evaluate the model

mse = mean_squared_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)

print("Mean Squared Error (MSE):", mse)

print("R² Score:", r2)


# Visualize the Decision Tree

[Link](figsize=(12, 8))

plot_tree(dt_regressor, feature_names=iris.feature_names, filled=True, fontsize=10)

[Link]()

OUTPUT:

Mean Squared Error (MSE): 0.002444444444444451

R² Score: 0.9992750715409197
6. Apply Random Forest algorithm for classification and regression

PROGRAMS:

Random Forest for Classification

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import RandomForestClassifier

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link]

y = [Link]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and train the Random Forest Classifier

rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

rf_classifier.fit(X_train, y_train)

# Make predictions on the test set

y_pred = rf_classifier.predict(X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))


OUTPUT:

Accuracy: 1.0

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

accuracy 1.00 45

macro avg 1.00 1.00 1.00 45

weighted avg 1.00 1.00 1.00 45


Random Forest for Regression

# Import necessary libraries

from [Link] import fetch_california_housing

from sklearn.model_selection import train_test_split

from [Link] import RandomForestRegressor

from [Link] import mean_squared_error, r2_score

# Load the California Housing dataset

data = fetch_california_housing()

X = [Link]

y = [Link]

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and train the Random Forest Regressor

rf_regressor = RandomForestRegressor(n_estimators=100, random_state=42)

rf_regressor.fit(X_train, y_train)

# Make predictions on the test set

y_pred = rf_regressor.predict(X_test)

# Evaluate the model

mse = mean_squared_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)

print("Mean Squared Error:", mse)

print("R² Score:", r2)

OUTPUT:

Mean Squared Error: 0.25650512920799395

R² Score: 0.8045734925119942
7. Demonstrate Naïve Bayes Classification algorithm

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features

y = [Link] # Target labels

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the Naive Bayes classifier (GaussianNB)

nb_classifier = GaussianNB()

# Train the model

nb_classifier.fit(X_train, y_train)

# Make predictions on the test set

y_pred = nb_classifier.predict(X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))


OUTPUT:

Accuracy: 0.9777777777777777

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 0.92 0.96 13

2 0.93 1.00 0.96 13

accuracy 0.98 45

macro avg 0.98 0.97 0.97 45

weighted avg 0.98 0.98 0.98 45


[Link] Support Vector algorithm for classification

PROGRAM:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import SVC

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features

y = [Link] # Target labels

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the Support Vector Classifier (SVC)

svm_classifier = SVC(kernel='linear', random_state=42) # Using a linear kernel

# Train the model

svm_classifier.fit(X_train, y_train)

# Make predictions on the test set

y_pred = svm_classifier.predict(X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))


OUTPUT:

Accuracy:

1.0

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

accuracy 1.00 45

macro avg 1.00 1.00 1.00 45

weighted avg 1.00 1.00 1.00 45


[Link] simple linear regression algorithm for a regression problem

PROGRAM:

# Import necessary libraries

from [Link] import fetch_california_housing

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from [Link] import mean_squared_error, r2_score

import [Link] as plt

# Load the California Housing dataset

data = fetch_california_housing()

X = [Link][:, 3]

# Selecting 'AveRooms' as the feature (index 3)

y = [Link]

# Target: median house value

# Reshape the feature array to match the input shape expected by the model

X = [Link](-1, 1)

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the Linear Regression model

lr_model = LinearRegression()

# Train the model

lr_model.fit(X_train, y_train)

# Make predictions on the test set

y_pred = lr_model.predict(X_test)

# Evaluate the model

mse = mean_squared_error(y_test, y_pred)

r2 = r2_score(y_test, y_pred)
print("Mean Squared Error (MSE):", mse)

print("R² Score:", r2)

# Visualize the regression line

[Link](figsize=(8, 6))

[Link](X_test, y_test, color='blue', label='Actual data') # Scatter plot of actual data

[Link](X_test, y_pred, color='red', label='Regression line') # Plot the regression line

[Link]('Average Rooms')

[Link]('Median House Value')

[Link]('Simple Linear Regression: House Value vs Average Rooms')

[Link]()

[Link]()

OUTPUT:

Mean Squared Error (MSE): 1.3103066154025542

R² Score: 0.0017016565040660625
10. Apply Logistic regression algorithm for a classification problem

PROGRAM:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features: sepal length, sepal width, petal length, petal width

y = [Link] # Target: species of the iris flower

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the Logistic Regression model

log_reg = LogisticRegression(max_iter=200)

# Train the model

log_reg.fit(X_train, y_train)

# Make predictions on the test set

y_pred = log_reg.predict(X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))


OUTPUT:

Accuracy: 1.0

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

accuracy 1.00 45

macro avg 1.00 1.00 1.00 45

weighted avg 1.00 1.00


11. Demonstrate Multi-layer Perceptron algorithm for a classification problem

PROGRAM:

# Import necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from sklearn.neural_network import MLPClassifier

from [Link] import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features: sepal length, sepal width, petal length, petal width

y = [Link] # Target: species of the iris flower

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the MLPClassifier

mlp = MLPClassifier(hidden_layer_sizes=(100,), max_iter=1000, random_state=42)

# Train the model

[Link](X_train, y_train)

# Make predictions on the test set

y_pred = [Link](X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))


OUTPUT:

Accuracy:1.0

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 19

1 1.00 1.00 1.00 13

2 1.00 1.00 1.00 13

accuracy 1.00 45

macro avg 1.00 1.00 1.00 45

weighted avg 1.00 1.00 1.00 45


12. Implement the K-means algorithm and apply it to the data you selected. Evaluate
performance by measuring the sum of the Euclidean distance of each example from its
class center. Test the performance of the algorithm as a function of the parameters K.

PROGRAM:

# Import necessary libraries

from [Link] import load_iris

from [Link] import KMeans

import numpy as np

import [Link] as plt

from [Link] import pairwise_distances_argmin_min

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features

y = [Link] # True class labels (not used in unsupervised learning)

# Function to calculate the sum of Euclidean distances of each example from its class
center

def calculate_sum_of_distances(X, kmeans):

# Get the cluster centers

cluster_centers = kmeans.cluster_centers_

# Get the closest data points to their respective cluster centers

closest, _ = pairwise_distances_argmin_min(X, cluster_centers)

# Calculate the sum of Euclidean distances of each example from its center

distances = [Link](X - cluster_centers[closest], axis=1)

return [Link](distances)

# Testing performance for different values of K (1 to 10 clusters)

k_values = range(1, 11)

sum_of_distances = []

for k in k_values:
kmeans = KMeans(n_clusters=k, random_state=42)

[Link](X)

# Calculate the sum of distances for the current value of K

sum_dist = calculate_sum_of_distances(X, kmeans)

sum_of_distances.append(sum_dist)

# Plot the sum of distances as a function of K

[Link](figsize=(8, 6))

[Link](k_values, sum_of_distances, marker='o', linestyle='-', color='b')

[Link]('Sum of Euclidean Distances as a Function of K')

[Link]('Number of Clusters (K)')

[Link]('Sum of Euclidean Distances')

[Link](k_values)

[Link](True)

[Link]()

# Print the sum of distances for each K value

for k, sum_dist in zip(k_values, sum_of_distances):

print(f"Sum of distances for K={k}: {sum_dist:.2f}")

OUTPUT:

Sum of distances for K=1: 291.61


Sum of distances for K=2: 128.34

Sum of distances for K=3: 97.22

Sum of distances for K=4: 84.69

Sum of distances for K=5: 76.32

Sum of distances for K=6: 69.93

Sum of distances for K=7: 65.42

Sum of distances for K=8: 62.19

Sum of distances for K=9: 61.41

Sum of distances for K=10: 59.89

for K = 1 to 10

Sum of distances for K=1: 873.75

Sum of distances for K=2: 625.88

Sum of distances for K=3: 448.12

Sum of distances for K=4: 351.18

Sum of distances for K=5: 300.24

Sum of distances for K=6: 259.13

Sum of distances for K=7: 231.27

Sum of distances for K=8: 211.12

Sum of distances for K=9: 193.86

Sum of distances for K=10: 180.91

13. Demonstrate the use of Fuzzy C-Means Clustering


PROGRAM:

import numpy as np

import [Link] as plt

# Fuzzy C-Means Clustering Function

def fuzzy_c_means(data, n_clusters, m, max_iter=100, epsilon=1e-5):

"""

Implements Fuzzy C-Means Clustering.

Parameters:

data (ndarray): Data points as a 2D array (n_samples, n_features).

n_clusters (int): Number of clusters.

m (float): Fuzziness parameter, must be > 1.

max_iter (int): Maximum number of iterations.

epsilon (float): Convergence threshold.

Returns:

centers (ndarray): Final cluster centers.

membership (ndarray): Final membership matrix.

"""

n_samples, n_features = [Link]

# Step 1: Initialize membership matrix with random values

[Link](42)

membership = [Link]([Link](n_clusters), size=n_samples).T

for _ in range(max_iter):

# Step 2: Compute cluster centers

centers = [Link](membership ** m, data) / [Link](membership ** m, axis=1,


keepdims=True)

# Step 3: Update distance matrix

\] distances = [Link]((n_clusters, n_samples))


for i in range(n_clusters):

distances[i] = [Link](data - centers[i], axis=1)

# Step 4: Update membership matrix

new_membership = 1.0 / (distances + 1e-10) ** (2 / (m - 1))

new_membership /= [Link](new_membership, axis=0, keepdims=True)

# Step 5: Check for convergence

if [Link](new_membership - membership) < epsilon:

break

membership = new_membership

return centers, membership

# Generate synthetic data

[Link](42)

data1 = [Link](5, 1.5, (100, 2))

data2 = [Link](15, 1.5, (100, 2))

data3 = [Link](25, 1.5, (100, 2))

data = [Link]((data1, data2, data3))

# Number of clusters and fuzziness parameter

n_clusters = 3

m = 2.0

# Perform Fuzzy C-Means clustering

centers, membership = fuzzy_c_means(data, n_clusters, m)

# Convert fuzzy memberships to hard cluster labels

cluster_labels = [Link](membership, axis=0)

# Visualize the clustering result

[Link](figsize=(8, 6))

# Plot each cluster's data points


for i in range(n_clusters):

[Link](data[cluster_labels == i, 0], data[cluster_labels == i, 1], label=f"Cluster {i+1}")

# Plot cluster centers

[Link](centers[:, 0], centers[:, 1], c='red', marker='x', s=200, label='Centers')

[Link]()

[Link]("Fuzzy C-Means Clustering")

[Link]("Feature 1")

[Link]("Feature 2")

[Link]()

[Link]()

# Print results

print("Cluster Centers:\n", centers)

print("\nFuzzy Memberships (first 5 points):\n", membership[:, :5])

OUTPUT

Cluster Centers:
[[24.93489432 24.80394895]

[ 4.82145503 5.03776051]

[15.19904146 15.05697084]]

Fuzzy Memberships (first 5 points):

[[1.18099350e-03 9.07403901e-03 2.21245407e-04 1.10362342e-02

1.11097713e-03]

[9.94157108e-01 9.49374005e-01 9.98955014e-01 9.37442547e-01

9.94611875e-01]

[4.66189858e-03 4.15519557e-02 8.23740689e-04 5.15212190e-02

4.27714820e-03]]
14. Demonstrate the use of Expectation Maximization based clustering algorithm
PROGRAM:

# Import necessary libraries

import numpy as np

import [Link] as plt

from [Link] import load_iris

from [Link] import GaussianMixture

from [Link] import PCA

# Load the Iris dataset

iris = load_iris()

X = [Link] # Features: sepal length, sepal width, petal length, petal width

# Apply Gaussian Mixture Model (EM Clustering)

gmm = GaussianMixture(n_components=3, covariance_type='full', random_state=42)

# Fit the GMM model to the data

[Link](X)

# Predict the cluster labels for each data point

predicted_labels = [Link](X)

# Get the cluster centers (means of the Gaussian components)

cluster_centers = gmm.means_

# Get the membership probabilities for each data point (soft clustering)

membership_probabilities = gmm.predict_proba(X)

# Visualizing the results using PCA (reduce to 2D for visualization)

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X)

[Link](figsize=(8, 6))

[Link](X_pca[:, 0], X_pca[:, 1], c=predicted_labels, cmap='viridis', marker='o',


label='Data points')
[Link](cluster_centers[:, 0], cluster_centers[:, 1], c='red', marker='X', s=200,
label='Cluster Centers')

[Link]('Expectation Maximization (EM) Clustering (2D PCA projection)')

[Link]('Principal Component 1')

[Link]('Principal Component 2')

[Link]()

[Link](label='Cluster Labels')

[Link]()

# Displaying the cluster centers (Gaussian means)

print("Cluster Centers (means of Gaussian components):")

print(cluster_centers)

# Displaying the predicted cluster labels

print("\nPredicted Cluster Labels:")

print(predicted_labels)

# Displaying the membership probabilities (soft assignment)

print("\nMembership Probabilities for the first 5 samples:")

print(membership_probabilities[:5])

OUTPUT:
Cluster Centers (means of Gaussian components):

[[6.54639415 2.94946365 5.48364578 1.98726565]

[5.006 3.428 1.462 0.246 ]

[5.9170732 2.77804839 4.20540364 1.29848217]]

Predicted Cluster Labels:

[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1111111111111222222222222222222020202

2220222220222222222222222200000000000

0000000000000000000000000000000000000

0 0]

Membership Probabilities for the first 5 samples:

[[6.06216336e-35 1.00000000e+00 1.01178227e-43]

[2.47801094e-28 1.00000000e+00 9.23008233e-31]

[4.01248422e-30 1.00000000e+00 1.02746863e-35]

[2.59713998e-26 1.00000000e+00 1.59307037e-31]

[2.54371410e-35 1.00000000e+00 3.78557282e-46]]

You might also like