AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
AVANTHI’S
ST. THERESSA INSTITUTE OF ENGG. & TECH
GARIVIDI
Vizianagaram Dist (AP)
Estd 2001
MACHINE LEARNING
R-23 CSE(AI&ML) II-II
Bachelor of Technology
IN
Computer Science Engineering
Prepared By
1
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
[Link]
AVANTHI’S
ST. THERESSA INSTITUTE OF ENGG. & TECH
GARIVIDI
Vizianagaram Dist (AP)
Estd 2001
CERTIFICATE
This is to certify that , it is the bonafide record of the work done in
……………………....... ………………………………………... laboratory by
Mr./Ms……................................. ………………………bearing [Link]./roll
no………………………… of……….........
………………..............................course during………………….
…………………….
Total Number of Total Number of
Experiments held: ………….. Experiments Done: …………
2
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
LAB INCHARGE HEAD OF THE DEPARTMENT
INDEX
EXERCISE TITLE PAGE
NO
EXPERIMENT-1 Compute Central Tendency Measures: Mean, Median, Mode
Measure of Dispersion: Variance, Standard Deviation.
EXPERIMENT-2 Apply the following Pre-processing techniques for a given dataset.
a. Attribute selection
b. Handling Missing Values
c. Discretization
d. Elimination of Outliers
EXPERIMENT-3 Apply KNN algorithm for classification and regression
EXPERIMENT-4 Demonstrate decision tree algorithm for a classification problem
and perform parameter tuning for better results
EXPERIMENT-5 Demonstrate decision tree algorithm for a regression problem
EXPERIMENT-6 Apply Random Forest algorithm for classification and regression
EXPERIMENT-7 Demonstrate Naïve Bayes Classification algorithm.
EXPERIMENT-8 Apply Support Vector algorithm for classification
EXPERIMENT-9 Demonstrate simple linear regression algorithm for a regression
problem
EXPERIMENT-10 Apply Logistic regression algorithm for a classification problem
EXPERIMENT-11 Demonstrate Multi-layer Perceptron algorithm for a classification
problem
EXPERIMENT-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.
EXPERIMENT-13 Demonstrate the use of Fuzzy C-Means Clustering
EXPERIMENT-14 Demonstrate the use of Expectation Maximization based clustering
algorithm.
3
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-1
[Link] Central Tendency Measures: Mean, Median, Mode Measure of
Dispersion: Variance, Standard Deviation.
AIM: To Compute Central Tendency Measures: Mean, Median, Mode Measure of
Dispersion: Variance, Standard Deviation.
SOURCE CODE:
import numpy as np
from scipy import stats
# Sample data
data = [10, 12, 14, 16, 18, 20, 20, 22, 24, 24, 24]
# Central Tendency Measures
mean = [Link](data) # Mean
median = [Link](data) # Median
mode = [Link](data) # Mode
# Measures of Dispersion
variance = [Link](data) # Variance
std_deviation = [Link](data) # Standard Deviation
# Output results
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {[Link][0]} (Frequency: {[Link][0]})")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_deviation}")
OUTPUT:
Mean: 18.545454545454547
Median: 20.0
Mode: 24 (Frequency: 3)
Variance: 22.61157024793388
Standard Deviation: 4.755162483862552
4
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-2
[Link] the following Pre-processing techniques for a given dataset.
a. Attribute selection
b. Handling Missing Values
c. Discretization
d. Elimination of Outliers
AIM: To Apply the following Pre-processing techniques for a given dataset.
a. Attribute selection
b. Handling Missing Values
c. Discretization
d. Elimination of Outliers
SOURCE CODE:
import pandas as pd
import numpy as np
# Sample Student Data with Additional NaN Values
# Create DataFrame
df = pd.read_csv("[Link]")
print("Original Data with NaN Values:")
print(df)
# a. Attribute Selection (Keep only relevant columns)
selected_columns = ["Student ID", "Age", "Math Score", "Science Score",
"Attendance"]
df = df[selected_columns]
print("\nAfter Attribute Selection:")
print(df)
# b. Handling Missing Values (Replace with mean for numeric columns)
[Link]([Link](), inplace=True)
print("\nAfter Handling Missing Values:")
print(df)
5
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# c. Discretization (Convert scores into categories)
bins = [0, 70, 85, 100]
labels = ["Low", "Medium", "High"]
df["Math Category"] = [Link](df["Math Score"], bins=bins, labels=labels)
df["Science Category"] = [Link](df["Science Score"], bins=bins,
labels=labels)
print("\nAfter Discretization:")
print(df)
# d. Elimination of Outliers (Using IQR Method)
def remove_outliers(df, column):
Q1 = df[column].quantile(0.25)
Q3 = df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]
df = remove_outliers(df, "Math Score")
df = remove_outliers(df, "Science Score")
print("\nAfter Eliminating Outliers:")
print(df)
# Save the processed data
df.to_csv("[Link]", index=False)
print("\nPre-processing completed and saved to
processed_student_scores.csv")
INPUT:
Math Science Math Science
Student ID Age Attendance
Score Score Category Category
1 16 85 90 95 Medium High
2 17 78 90.125 90 Medium High
4 18 92 High
8 17 82 88 93 Medium High
9 16 83.125 93 97 Medium High
10 16.875 77 90.125 91 Medium High
11 22 66 High Medium
12 33 44 55 77 Medium High
OUTPUT:
Original Data with NaN Values:
6
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
Student ID Age Math Score Science Score Attendance Math Category \
0 1 16.000 85.000000 90.000 95.0 Medium
1 2 17.000 78.000000 90.125 90.0 Medium
2 4 18.000 74.854167 92.000 87.0 Medium
3 8 17.000 82.000000 88.000 93.0 Medium
4 9 16.000 83.125000 93.000 97.0 Medium
5 10 16.875 77.000000 90.125 91.0 Medium
Science Category
0 High
1 High
2 High
3 High
4 High
5 High
After Attribute Selection:
Student ID Age Math Score Science Score Attendance
0 1 16.000 85.000000 90.000 95.0
1 2 17.000 78.000000 90.125 90.0
2 4 18.000 74.854167 92.000 87.0
3 8 17.000 82.000000 88.000 93.0
4 9 16.000 83.125000 93.000 97.0
5 10 16.875 77.000000 90.125 91.0
After Handling Missing Values:
Student ID Age Math Score Science Score Attendance
0 1 16.000 85.000000 90.000 95.0
1 2 17.000 78.000000 90.125 90.0
2 4 18.000 74.854167 92.000 87.0
3 8 17.000 82.000000 88.000 93.0
4 9 16.000 83.125000 93.000 97.0
5 10 16.875 77.000000 90.125 91.0
After Discretization:
Student ID Age Math Score Science Score Attendance Math Category \
0 1 16.000 85.000000 90.000 95.0 Medium
1 2 17.000 78.000000 90.125 90.0 Medium
2 4 18.000 74.854167 92.000 87.0 Medium
3 8 17.000 82.000000 88.000 93.0 Medium
4 9 16.000 83.125000 93.000 97.0 Medium
7
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
5 10 16.875 77.000000 90.125 91.0 Medium
Science Category
0 High
1 High
2 High
3 High
4 High
5 High
After Eliminating Outliers:
Student ID Age Math Score Science Score Attendance Math Category \
0 1 16.000 85.000000 90.000 95.0 Medium
1 2 17.000 78.000000 90.125 90.0 Medium
2 4 18.000 74.854167 92.000 87.0 Medium
3 8 17.000 82.000000 88.000 93.0 Medium
4 9 16.000 83.125000 93.000 97.0 Medium
5 10 16.875 77.000000 90.125 91.0 Medium
Science Category
0 High
1 High
2 High
3 High
4 High
5 High
Pre-processing completed and saved to processed_student_scores.csv
8
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-3
3Apply KNN algorithm for classification and regression
AIM: To Apply KNN algorithm for classification and regression
SOURCE CODE:
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, classification_report
from [Link] import load_iris
# 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.2,
random_state=42)
# Standardize features by removing the mean and scaling to
# unit variance
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Initialize the KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
# Train the KNN classifier
[Link](X_train, y_train)
#Make predictions on the test set
y_pred = [Link](X_test)
9
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# Evaluate the model's performance
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")
print(classification_report(y_test, y_pred))
#KNN Regression
# Import necessary libraries
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsRegressor
from [Link] import mean_squared_error, r2_score
from [Link] import make_regression
# Generate a regression dataset
X, y = make_regression(n_samples=100, n_features=1, noise=10,
random_state=42)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Standardize features by removing the mean and scaling to unit variance
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Initialize the KNN regressor
knn = KNeighborsRegressor(n_neighbors=5)
# Train the KNN regressor
[Link](X_train, y_train)
#Make predictions on the test set
y_pred = [Link](X_test)
# Evaluate the model's performance
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
10
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared: {r2:.2f}")
OUTPUT:
Accuracy: 1.00
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 1.00 1.00 1.00 9
2 1.00 1.00 1.00 11
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
Mean Squared Error: 108.14
R-squared: 0.94
11
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-4
[Link] decision tree algorithm for a classification problem and perform
parameter tuning for better results
AIM: To Demonstrate decision tree algorithm for a classification problem and
perform parameter tuning for better results
SOURCE CODE:
#Import necessary libraries
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score, classification_report
from sklearn.model_selection import GridSearchCV
# Load the dataset (e.g., Iris dataset)
from [Link] import load_iris
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.2, random_state=42)
# Initialize the Decision Tree classifier
clf = DecisionTreeClassifier(random_state=42)
# Define hyperparameter tuning space
param_grid = {
'max_depth': [3, 5, 10, None],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 5, 10],
'criterion': ['gini', 'entropy']
}
# Perform grid search for hyperparameter tuning
grid_search = GridSearchCV(estimator=clf, param_grid=param_grid, cv=5)
grid_search.fit(X_train, y_train)
# Print the best parameters and the corresponding accuracy
print("Best Parameters:", grid_search.best_params_)
print("Best Accuracy:", grid_search.best_score_)
12
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# Train a new Decision Tree classifier using the best parameters
best_clf = grid_search.best_estimator_
best_clf.fit(X_train, y_train)
#Evaluate the best classifier on the test set
y_pred = best_clf.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:")
print(classification_report(y_test, y_pred))
OUTPUT:
Best Parameters: {'criterion': 'gini', 'max_depth': 3, 'min_samples_leaf': 5,
'min_samples_split': 2}
Best Accuracy: 0.95
Accuracy: 1.0
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 1.00 1.00 1.00 9
2 1.00 1.00 1.00 11
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
13
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-5
[Link] decision tree algorithm for a regression problem
AIM: To Demonstrate decision tree algorithm for a regression problem
SOURCE CODE:
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeRegressor
from [Link] import mean_squared_error, r2_score
from [Link] import make_regression
import numpy as np
import [Link] as plt
# Generate a regression dataset
X, y = make_regression(n_samples=100, n_features=1, noise=10, random_state=42)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the Decision Tree regressor
dtr = DecisionTreeRegressor(random_state=42)
# Train the Decision Tree regressor
[Link](X_train, y_train)
# Make predictions on the test set
y_pred = [Link](X_test)
# Evaluate the model's performance
mse = mean_squared_error(y_test, y_pred)
14
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared: {r2:.2f}")
# Plot the data and the predicted values
[Link](X_test, y_test, label="Actual values")
[Link](X_test, y_pred, label="Predicted values")
[Link]()
[Link]()
OUTPUT:
Mean Squared Error: 226.23
R-squared: 0.86
15
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-6
6. Apply Random Forest algorithm for classification and regression .
AIM: ToApply Random Forest algorithm for classification and regression .
Source code:
Random forest classifier
# Import 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
data = load_iris()
X = [Link] # Features
y = [Link] # Labels
# Split the data 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 Random Forest Classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# Train the model
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the model
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
16
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
Out put:
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
Source code:
Random forest Regressor
# Import 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] # Features
y = [Link] # Target variable (median house price)
# Split the data 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 Random Forest Regressor
reg = RandomForestRegressor(n_estimators=100, random_state=42)
# Train the model
[Link](X_train, y_train)
17
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the model
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R^2 Score:", r2_score(y_test, y_pred))
Out put:
Mean Squared Error: 0.25650512920799395
R^2 Score: 0.8045734925119942
18
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-7
[Link] Naïve Bayes Classification algorithm.
AIM: To Demonstrate Naïve Bayes Classification algorithm.
Source code:
# Import 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,
confusion_matrix
# Load the Iris dataset
data = load_iris()
X = [Link] # Features (sepal length, sepal width, petal length, petal
width)
y = [Link] # Labels (0: setosa, 1: versicolor, 2: virginica)
# Split the data 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 Naïve Bayes classifier
nb_classifier = GaussianNB()
# Train the model
nb_classifier.fit(X_train, y_train)
# Make predictions
y_pred = nb_classifier.predict(X_test)
# Evaluate the model
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
19
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
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
Confusion Matrix:
[[19 0 0]
[ 0 12 1]
[ 0 0 13]]
20
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-8
7. Apply Support Vector algorithm for classification
AIM: To Apply Support Vector algorithm for classification
Source code:
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import SVC
from [Link] import accuracy_score, classification_report,
confusion_matrix
# Step 2: Load Dataset
iris = datasets.load_iris()
X = [Link][:, :2] # Selecting first two features for visualization
y = [Link]
# Step 3: Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Step 4: Standardize the Data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Step 5: Train SVM Classifier
svm_classifier = SVC(kernel='linear', C=1.0) # Using linear kernel
svm_classifier.fit(X_train, y_train)
21
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# Step 6: Make Predictions
y_pred = svm_classifier.predict(X_test)
# Step 7: Evaluate the Model
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
# Step 8: Confusion Matrix
conf_matrix = confusion_matrix(y_test, y_pred)
[Link](conf_matrix, annot=True, cmap="Blues", fmt="d")
[Link]("Predicted")
[Link]("Actual")
[Link]("Confusion Matrix")
[Link]()
Out put:
Accuracy: 0.9
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 0.88 0.78 0.82 9
2 0.83 0.91 0.87 11
accuracy 0.90 30
macro avg 0.90 0.90 0.90 30
weighted avg 0.90 0.90 0.90 30
22
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-9
8. Demonstrate simple linear regression algorithm for a regression problem
AIM: To Demonstrate simple linear regression algorithm for a regression problem
Source code:
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# Step 2: Generate Sample Data
[Link](42)
X = 2 * [Link](100, 1) # Independent variable
y = 4 + 3 * X + [Link](100, 1) # Dependent variable with noise
# Step 3: Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Step 4: Train the Model
model = LinearRegression()
[Link](X_train, y_train)
# Step 5: Make Predictions
y_pred = [Link](X_test)
# Step 6: Evaluate the Model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared Score: {r2:.2f}")
# Step 7: Visualize the Regression Line
23
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
[Link](X_test, y_test, color='blue', label="Actual Data")
[Link](X_test, y_pred, color='red', linewidth=2, label="Regression Line")
[Link]("X (Independent Variable)")
[Link]("y (Dependent Variable)")
[Link]("Simple Linear Regression")
[Link]()
[Link]()
Output:
Mean Squared Error: 0.65
R-squared Score: 0.81
24
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-10
9. Apply Logistic regression algorithm for a classification problem
AIM: To Apply Logistic regression algorithm for a classification problem
Source code :
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report,
confusion_matrix
# Step 2: Load Dataset
iris = datasets.load_iris()
X = [Link][:, :2] # Selecting first two features for visualization
y = ([Link] != 0).astype(int) # Converting to a binary classification
problem (Class 0 vs Others)
# Step 3: Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Step 4: Feature Scaling (Recommended for Logistic Regression)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Step 5: Train the Logistic Regression Model
model = LogisticRegression()
25
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
[Link](X_train, y_train)
# Step 6: Make Predictions
y_pred = [Link](X_test)
# Step 7: Evaluate the Model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("Confusion Matrix:\n", conf_matrix)
print("Classification Report:\n", report)
# Step 8: Visualize Decision Boundary
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, 100), [Link](y_min,
y_max, 100))
Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])
[Link](xx, yy, Z, alpha=0.3, cmap="coolwarm")
[Link](X_test[:, 0], X_test[:, 1], c=y_test, edgecolors="k",
cmap="coolwarm")
[Link]("Feature 1 (Standardized)")
[Link]("Feature 2 (Standardized)")
[Link]("Logistic Regression Decision Boundary")
[Link]()
Out put:
Accuracy: 1.00
Confusion Matrix:
[[10 0]
[ 0 20]]
26
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 1.00 1.00 1.00 20
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
27
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
EXPERIMENT-10
10. Demonstrate Multi-layer Perceptron algorithm for a classification problem
11. AIM: To Demonstrate Multi-layer Perceptron algorithm for a classification problem
Source code:
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score, classification_report,
confusion_matrix
# Step 2: Load Dataset
iris = datasets.load_iris()
X = [Link][:, :2] # Selecting first two features for visualization
y = [Link]
# Step 3: Split Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Step 4: Feature Scaling (Recommended for Neural Networks)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Step 5: Train the MLP Classifier Model (Updated Parameters)
mlp = MLPClassifier(hidden_layer_sizes=(20, 20), activation='relu',
solver='adam',
max_iter=1000, learning_rate_init=0.0005, random_state=42)
28
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
[Link](X_train, y_train)
# Step 6: Make Predictions
y_pred = [Link](X_test)
# Step 7: Evaluate the Model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("Confusion Matrix:\n", conf_matrix)
print("Classification Report:\n", report)
# Step 8: Visualize Decision Boundary
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, 100), [Link](y_min,
y_max, 100))
Z = [Link](np.c_[[Link](), [Link]()])
Z = [Link]([Link])
[Link](xx, yy, Z, alpha=0.3, cmap="coolwarm")
[Link](X_test[:, 0], X_test[:, 1], c=y_test, edgecolors="k",
cmap="coolwarm")
[Link]("Feature 1 (Standardized)")
[Link]("Feature 2 (Standardized)")
[Link]("MLP Classifier Decision Boundary")
[Link]()
Out put :
Accuracy: 0.90
Confusion Matrix:
[[10 0 0]
[ 0 7 2]
29
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
[ 0 1 10]]
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 10
1 0.88 0.78 0.82 9
2 0.83 0.91 0.87 11
accuracy 0.90 30
macro avg 0.90 0.90 0.90 30
weighted avg 0.90 0.90 0.90 30
13. . Demonstrate the use of Fuzzy C-Means Clustering
Source code:
pip install scikit-fuzzy –timeout=120
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn import datasets
from [Link] import StandardScaler
import skfuzzy as fuzz # Fuzzy Clustering package
# Step 2: Load Dataset
iris = datasets.load_iris()
X = [Link][:, :2] # Selecting first two features for easy visualization
# Step 3: Feature Scaling
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 4: Apply Fuzzy C-Means Clustering
n_clusters = 3 # Number of clusters
cntr, u, u0, d, jm, p, fpc = [Link](X_scaled.T, c=n_clusters,
m=2, error=0.005, maxiter=1000)
# Step 5: Assign clusters based on maximum membership
labels = [Link](u, axis=0)
30
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
# Step 6: Visualize the Clusters
[Link](figsize=(8, 6))
[Link](X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis',
edgecolors='k', s=100, alpha=0.7)
[Link](f"Fuzzy C-Means Clustering - {n_clusters} Clusters")
[Link]("Feature 1 (Standardized)")
[Link]("Feature 2 (Standardized)")
# Plot cluster centers
for i in range(n_clusters):
[Link](cntr[i, 0], cntr[i, 1], marker='x', s=200, c='red', label=f'Cluster
{i+1} Center')
[Link]()
# Step 7: Print Fuzzy C-Means results
print("Fuzzy C-Means Centers:\n", cntr)
print("Fuzzy Partition Coefficient (FPC):", fpc)
Out put:
Fuzzy C-Means Centers:
[[-0.15745269 -0.90559601]
[ 1.04562093 0.03393875]
[-0.92813921 0.93333038]]
Fuzzy Partition Coefficient (FPC): 0.6767395496811561
14. Demonstrate the use of Expectation Maximization based clustering
algorithm
source code:
# Step 1: Import Libraries
import numpy as np
import [Link] as plt
from sklearn import datasets
from [Link] import GaussianMixture
from [Link] import StandardScaler
import os
31
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
import warnings
# Suppress Memory Leak Warning related to KMeans
[Link]("ignore", message=".*memory leak.*",
category=UserWarning)
# Optionally, limit threads if you want to avoid memory leaks in KMeans
(if it's used anywhere in your environment)
[Link]["OMP_NUM_THREADS"] = "1" # To avoid memory leak
warnings in KMeans
# Step 2: Load Dataset (Iris Dataset)
iris = datasets.load_iris()
X = [Link][:, :2] # Selecting first two features for easy visualization
# Step 3: Feature Scaling
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Step 4: Apply Gaussian Mixture Model (GMM)
n_clusters = 3 # Number of clusters (in this case, the number of iris
species)
gmm = GaussianMixture(n_components=n_clusters,
covariance_type='full', random_state=42)
[Link](X_scaled)
# Step 5: Predict cluster labels
labels = [Link](X_scaled)
# Step 6: Visualize the Clusters
[Link](figsize=(8, 6))
[Link](X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis',
edgecolors='k', s=100, alpha=0.7)
[Link](f"Expectation Maximization (GMM) Clustering - {n_clusters}
Clusters")
[Link]("Feature 1 (Standardized)")
[Link]("Feature 2 (Standardized)")
# Plot the GMM centers
32
AVANTHI’S ST THERESSA ENG & TECH R23 CSE(AI&ML)
centers = gmm.means_
for i in range(n_clusters):
[Link](centers[i, 0], centers[i, 1], marker='x', s=200, c='red',
label=f'Cluster {i+1} Center')
[Link]()
[Link]()
# Step 7: Print the GMM results
print("Cluster Centers:\n", centers)
print("Covariance Matrices:\n", gmm.covariances_)
print("Weights of Each Component:\n", gmm.weights_)
Output:
Cluster Centers:
[[ 1.01009763 -0.03099225]
[ 0.05876606 -0.75803909]
[-1.0038089 0.90771236]]
Covariance Matrices:
[[[0.52745679 0.12205945]
[0.12205945 0.43834273]]
[[0.40115983 0.23224156]
[0.23224156 0.48141623]]
[[0.17537594 0.24664977]
[0.24664977 0.63045924]]]
Weights of Each Component:
[0.30017673 0.37576739 0.32405588]
33