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

ML[1]

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

ML[1]

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

Machine Learning Lab

(CIE‐421P)

Faculty name: Prof. (Dr.) Sachin Gupta Student Name: Ansh Kaushik
Roll No. : 13414802721
Semester : 7th
Group : CSE-FSD-I

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi – 110085

1
MAHARAJA AGRASEN INSTITUTE OF TECHNOLOGY

VISION

To attain global excellence through education, innovation, research, and work ethics with the commitment to
serve humanity.

MISSION

M1. To promote diversification by adopting advancement in science, technology, management, and allied
discipline through continuous learning

M2. To foster moral values in students and equip them for developing sustainable solutions to serve both
national and global needs in society and industry.

M3. To digitize educational resources and process for enhanced teaching and effective learning.

M4. To cultivate an environment supporting incubation, product development, technology transfer,


capacity building and entrepreneurship.

M5. To encourage faculty-student networking with alumni, industry, institutions, and


other stakeholders for collective engagement.

2
Department of Computer Science and Engineering

VISION

To establish a center of excellence promoting Information Technology related education and research for
preparing technocrats and entrepreneurs with ethical values.

MISSION

M1: To excel in the field by imparting quality education and skills for software development and
applications.

M2: To establish a conducive environment that promotes intellectual growth and research.

M3: To facilitate students to acquire entrepreneurial skills for innovation and product development.

M4: To encourage students to participate in competitive events and industry interaction with a focus on
continuous learning.

3
1.

CO-PO Mapping

Course Objectives :
1. To understand the need of machine learning
2. To learn about regression and feature selection.
3. To understand about classification algorithms
4. To learn clustering algorithms
Course Outcomes (CO)
CO 1 To formulate machine learning problems
CO 2 Learn about regression and feature selection techniques
CO 3 Apply machine learning techniques such as classification to practical applications
CO 4 Apply clustering algorithms
Course Outcomes (CO) to Programme Outcomes (PO) mapping (scale 1: low, 2: Medium, 3: High)
PO01 PO02 PO03 PO04 PO05 PO06 PO07 PO08 PO09 PO10 PO11 PO12
CO 1 3 3 3 3 3 2 2 - - - - 2
CO 2 3 3 3 3 3 2 2 - - - - 2
CO 3 3 3 3 3 3 2 2 - - - - 2
CO 4 3 3 3 3 3 2 2 - - - - 2

4
Rubrics Evaluation

5
PROGRAMMING IN PYTHON LAB

PRACTICAL RECORD

PAPER CODE : CIE‐421P

Name of the student : Ansh Kaushik

University Roll No. : 13414802721

Branch : Computer science and engineering (CSE)

Section/ Group : CSE-FSD-I

PRACTICAL DETAILS

a) Experiments according to the list provided by GGSIPU

E.No Page Date Experiment Name Marks (0-3) Total Signature


. No Marks
(15)
R1 R2 R3 R4 R5
Introduction to JUPYTER
1. 09 IDE and its libraries Pandas
and NumPy

Program to demonstrate
2. 11 Simple Linear Regression

Program to demonstrate
3. 13 Logistic Regression

Program to demonstrate
4. 15 Decision Tree – ID3
Algorithm
Program to demonstrate k-
5. 18 Nearest Neighbor flowers
classification
Program to demonstrate
6. 20 Naïve- Bayes Classifier

Program to demonstrate
7. 22 PCA and LDA on Iris
dataset
Program to demonstrate
8. 24 DBSCAN clustering
algorithm
Program to demonstrate
9. 26
K-Medoid clustering
6
algorithm

Program to demonstrate K-
10. 28 Means Clustering Algorithm
on Handwritten Dataset

b) Experiments beyond curriculum

E. Page Date Experiment Name Marks (0-3) Total Signature


No. No Marks
(15)
R1 R2 R3 R4 R5
Exploring Google Colab
1. 31

A program for linear


regression model using
2. 33
scikit-learn but no machine
learning
A program without using
scikit-learn or machine
3 34
learning for Linear
regression
Create a decision tree
without using sklearn
library, just using the
concept of entropy and
4. 35 choose what node will be
the root, and subsequent
decision parameters based
on information gain. Show
all calculations.
Show how decision trees
can be used for both
classification and
5. 37 regression using a program
with sample dataset. Also
visualize the decision tree
obtained
Write your own
implementation for knn
6. 39 with 4 different distance
metrics and check on Iris
dataset
Apply your own
implementation of Knn to
any instance of Pima
7. 41 Indians Diabetes Database
from Kaggle or Github and
submit notebooks for all 3
exercises.

7
Explain the difference
8. 41
between two approaches

Use colab examples given


in the classroom group for
applying PCA and LDA to
9. 44 different datasets, and
share an EXPLANATION
about how do you interpret
the results.
Compare K-means
clustering and DBSCAN
for IRIS based on
10. 46
performance metrics.
Which approach is better?
Why?
Apply SVM on IRIS data
11. 48
set from sklearn for
classification

12.

13.

14.

15.

8
Experiment – 1

AIM: Introduction to JUPYTER IDE and its libraries Pandas and NumPy

THEORY:
1. Jupyter Notebook:
 Jupyter Notebook is an interactive computing environment that allows for the creation and sharing of
documents containing live code, equations, visualizations, and narrative text.
 It supports various programming languages, with Python being the most commonly used.
 Jupyter Notebooks are comprised of cells, which can contain code, Markdown text, equations, or raw
text. This allows for a combination of code execution, data visualization, and documentation within a
single document.
 The ability to execute code interactively and visualize results immediately makes Jupyter Notebooks
an ideal tool for data exploration, analysis, and sharing of research findings.
2. Pandas:
 Pandas is a Python library built on top of NumPy that provides high-performance, easy-to-use data
structures and data analysis tools.
 The primary data structures in Pandas are Series (one-dimensional labeled arrays) and DataFrame
(two-dimensional labeled data structures with columns of potentially different types).
 Pandas excels at handling structured data, such as tabular data, time series, and heterogeneously-typed
data.
 It offers functionalities for data manipulation, including indexing, slicing, merging, reshaping, and
pivoting, as well as data cleaning, transformation, and analysis.
 Pandas' integration with other libraries like Matplotlib and Seaborn makes it convenient for data
visualization and exploration.
3. NumPy:
 NumPy (Numerical Python) is a fundamental package for numerical computing in Python.
 Its core feature is the ndarray, a multi-dimensional array object that provides efficient storage and
manipulation of homogeneous data.
 NumPy arrays support various mathematical operations and broadcasting, enabling vectorized
computations and efficient handling of large datasets.
 It provides a wide range of mathematical functions for linear algebra, Fourier analysis, random
number generation, and more.
 NumPy arrays are the building blocks for many other libraries in the Python scientific ecosystem,
including Pandas, Matplotlib, and SciPy.

CODE:

import pandas as pd
import numpy as np
# Creating a NumPy array
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Creating a DataFrame from the NumPy array


df = pd.DataFrame(data, columns=['A', 'B', 'C'])

# Displaying the DataFrame


df

# Selecting a specific column


print(df['B'],"\n")

9
# Filtering rows based on a condition
print(df[df['A'] > 3], "\n")

# Applying a function to a column


df['D'] = df['C'].apply(lambda x: x * 2)
print(df)

OUTPUT:

10
Experiment – 2

AIM: I Program to demonstrate Simple Linear Regression

THEORY:

Simple Linear Regression is a linear regression model with a single explanatory variable (independent variable) to
predict the value of a dependent variable. The relationship between the two variables is assumed to be linear, i.e., a
straight line.
The equation of a simple linear regression model is given by:

𝑦=𝛽0+𝛽1⋅𝑥+𝜖y=β0+β1⋅x+ϵ

𝑦y is the dependent variable (the variable we want to predict).


Where:

𝑥x is the independent variable (the variable used to make predictions).


𝛽0β0 is the intercept (the value of 𝑦y when 𝑥=0x=0).


𝛽1β1 is the slope (the change in 𝑦y for a unit change in 𝑥x).


𝜖ϵ represents the error term (the difference between the observed and predicted values).

CODE:

# Importing necessary libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Generating some sample data


np.random.seed(0)
X = 2 * np.random.rand(100, 1) # Independent variable
y = 4 + 3 * X + np.random.randn(100, 1) # Dependent variable with some noise

# Creating a Linear Regression model


model = LinearRegression()

# Fitting the model to the data


model.fit(X, y)

# Getting the model parameters


intercept = model.intercept_[0]
slope = model.coef_[0][0]

# Predicting values using the model


y_pred = model.predict(X)

# Plotting the data and the regression line


plt.scatter(X, y, color='blue', label='Original data')
plt.plot(X, y_pred, color='red', label='Fitted line')
plt.title('Simple Linear Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

11
# Displaying the model parameters
print("Intercept (beta0):", intercept)
print("Slope (beta1):", slope)

OUTPUT:

12
Experiment – 3

AIM: Program to demonstrate Logistic Regression

THEORY:

Logistic Regression is a supervised learning algorithm used for binary classification tasks. It models the probability
that a given input belongs to a particular class. The logistic function (sigmoid function) is used to map the output of
the linear combination of input features to a probability score between 0 and 1.
The logistic function is defined as:

𝜎(𝑧)=11+𝑒−𝑧σ(z)=1+e−z1

𝑧=𝛽0+𝛽1𝑥1+𝛽2𝑥2+...+𝛽𝑛𝑥𝑛z=β0+β1x1+β2x2+...+βnxn is the linear combination of input features.


Where:

𝛽0,𝛽1,𝛽2,...,𝛽𝑛β0,β1,β2,...,βn are the coefficients (parameters) of the model.


𝑥1,𝑥2,...,𝑥𝑛x1,x2,...,xn are the input features.


𝜎(𝑧)σ(z) is the logistic function, which returns a probability score between 0 and 1.

CODE:

# Importing necessary libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset


iris = load_iris()
X = iris.data[:, :2] # Selecting only the first two features (sepal length and sepal width)
y = (iris.target == 0).astype(int) # Binary classification: setosa or not

# Splitting the 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)

# Creating and training a Logistic Regression model


model = LogisticRegression()
model.fit(X_train, y_train)

# Making predictions on the test set


y_pred = model.predict(X_test)

# Evaluating the model


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

# Generating a meshgrid for visualization


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02))

13
# Plotting the decision boundary and the data points
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.title('Logistic Regression for Iris Dataset (Setosa vs. Non-Setosa)')
plt.show()

# Displaying classification report


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

OUTPUT:

14
Experiment – 4

AIM: Program to demonstrate Decision Tree – ID3 Algorithm

THEORY:
The ID3 (Iterative Dichotomiser 3) algorithm is a classic decision tree algorithm used for building classification
models. It recursively selects the best attribute to split the data based on Information Gain, a measure of the reduction
in entropy (or increase in purity) achieved by splitting the data on a particular attribute.
The steps of the ID3 algorithm are as follows:
1. Select the best attribute: Iterate over all attributes and calculate the Information Gain for each attribute.
Choose the attribute with the highest Information Gain as the best attribute to split the data.
2. Split the data: Split the data into subsets based on the values of the chosen attribute.
3. Repeat recursively: Recursively apply steps 1 and 2 to each subset until one of the following
conditions is met:
 All instances in the subset belong to the same class.
 There are no more attributes left to split.
 Stopping criteria (e.g., maximum depth or minimum number of instances) are met.
4. Build the decision tree: Build the decision tree by assigning each attribute as a node and recursively
adding child nodes based on the splits.
5. Prune the tree (optional): Pruning is the process of removing sections of the tree that are not
statistically significant to reduce overfitting and improve generalization.

CODE:

import numpy as np

# Define the ID3 algorithm function


def id3(X, y, attribute_names):
# If all instances belong to the same class, return a leaf node with that class
if len(np.unique(y)) == 1:
return {'class': y[0]}

# If there are no more attributes left, return a leaf node with the majority class
if len(attribute_names) == 0:
return {'class': np.bincount(y).argmax()}

# Calculate the information gain for each attribute


information_gains = []
for i in range(X.shape[1]):
information_gain = calculate_information_gain(X, y, i)
information_gains.append(information_gain)

# Select the attribute with the highest information gain


best_attribute_index = np.argmax(information_gains)
best_attribute_name = attribute_names[best_attribute_index]

# Split the data based on the best attribute


values = np.unique(X[:, best_attribute_index])
subsets = {}
for value in values:
subset_indices = np.where(X[:, best_attribute_index] == value)[0]
if len(np.unique(X[subset_indices, best_attribute_index])) == 1:
# If only one unique value for the attribute in the subset, return a leaf node
subsets[value] = {'class': np.bincount(y[subset_indices]).argmax()}
15
else:
subsets[value] = id3(X[subset_indices], y[subset_indices],
attribute_names[:best_attribute_index] + attribute_names[best_attribute_index+1:])

return {'attribute': best_attribute_name, 'subsets': subsets}

# Function to calculate information gain


def calculate_information_gain(X, y, attribute_index):
# Calculate entropy of the parent node
parent_entropy = calculate_entropy(y)

# Calculate entropy of each child node and weighted average entropy


values, counts = np.unique(X[:, attribute_index], return_counts=True)
weighted_child_entropy = 0
for value, count in zip(values, counts):
subset_indices = np.where(X[:, attribute_index] == value)[0]
weighted_child_entropy += (count / len(y)) * calculate_entropy(y[subset_indices])

# Calculate information gain


information_gain = parent_entropy - weighted_child_entropy
return information_gain

# Function to calculate entropy


def calculate_entropy(y):
_, counts = np.unique(y, return_counts=True)
probabilities = counts / len(y)
entropy = -np.sum(probabilities * np.log2(probabilities))
return entropy

# Example usage
X = np.array([
['Sunny', 'Hot', 'High', 'Weak'],
['Sunny', 'Hot', 'High', 'Strong'],
['Overcast', 'Hot', 'High', 'Weak'],
['Rain', 'Mild', 'High', 'Weak'],
['Rain', 'Cool', 'Normal', 'Weak'],
['Rain', 'Cool', 'Normal', 'Strong'],
['Overcast', 'Cool', 'Normal', 'Strong'],
['Sunny', 'Mild', 'High', 'Weak'],
['Sunny', 'Cool', 'Normal', 'Weak'],
['Rain', 'Mild', 'Normal', 'Weak'],
['Sunny', 'Mild', 'Normal', 'Strong'],
['Overcast', 'Mild', 'High', 'Strong'],
['Overcast', 'Hot', 'Normal', 'Weak'],
['Rain', 'Mild', 'High', 'Strong']
])

y = np.array(['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No'])

attribute_names = ['Outlook', 'Temperature', 'Humidity', 'Wind']

# Convert class labels to integer values


class_mapping = {label: idx for idx, label in enumerate(np.unique(y))}
y_int = np.array([class_mapping[label] for label in y])

# Example usage
tree = id3(X, y_int, attribute_names)
print(tree)
def print_tree(node, depth=0):
if 'attribute' in node:
16
print(' ' * depth + node['attribute'] + ' ?')
for value, subtree in node['subsets'].items():
print(' ' * (depth + 1) + value + ' :')
print_tree(subtree, depth + 2)
else:
print(' ' * (depth + 1) + 'Class:', node['class'])
print("\n")
# Example usage
print_tree(tree)

OUTPUT:

17
Experiment – 5

AIM: Program to demonstrate k-Nearest Neighbor flowers classification

THEORY:

The k-NN algorithm is a simple and intuitive algorithm used for classification and regression tasks. It works by find-
ing the k closest data points in the training set to a new data point that needs to be classified or predicted.

For classification tasks, the algorithm assigns the new data point to the class that is most common among its k nearest
neighbors. For regression tasks, it predicts the target value as the mean or median of the target values of the k nearest
neighbors.

The key components of the k-NN algorithm are:

1. A distance metric (e.g., Euclidean distance) to calculate the distances between data points.
2. The value of k, which is the number of nearest neighbors to consider.
3. A method for assigning class labels (classification) or predicting target values (regression) based on the k
nearest neighbors.

The choice of k and the distance metric can significantly impact the performance of the algorithm. k-NN is a non-
parametric, lazy learning algorithm that does not build a model during training but instead stores the entire training
data and performs computations when a new data point needs to be classified or predicted.

CODE:

# Importing necessary libraries


import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# 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)

# Create and train a k-Nearest Neighbor classifier


k = 3 # Choose the number of neighbors
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

# Make predictions on the test set


y_pred = knn.predict(X_test)

# Evaluate the model


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

# Display classification report


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

OUTPUT:

19
Experiment – 6

AIM: Program to demonstrate Naïve- Bayes Classifier

THEORY:

The Naïve Bayes classifier is a probabilistic machine learning algorithm based on Bayes' theorem. It assumes that
features are conditionally independent given the class label, which is why it's called "naïve". Despite this simplifying
assumption, Naïve Bayes classifiers often perform well in practice, especially for text classification tasks.
Bayes' theorem states:

P(A|B) = (P(B|A) * P(A)) / P(B)

Where:
 P(A|B) is the probability of event A occurring given that event B has occurred (posterior probabil-
ity)
 P(B|A) is the probability of event B occurring given that event A has occurred (likelihood)
 P(A) is the probability of event A occurring (prior probability)
 P(B) is the probability of event B occurring (evidence)

CODE:

# Importing necessary libraries


import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# 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)

# Create and train a Naïve Bayes classifier


nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)

# Make predictions on the test set


y_pred = nb_classifier.predict(X_test)

# Evaluate the model


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

# Display classification report


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

20
OUTPUT:

21
Experiment – 7

AIM: Program to demonstrate PCA and LDA on Iris dataset

THEORY:

Principal Component Analysis (PCA): PCA is an unsupervised dimensionality reduction technique that is used to
transform a high-dimensional dataset into a lower-dimensional space while retaining as much of the original variance
as possible. It does this by finding the directions (principal components) that maximize the variance in the data.

The key steps in PCA are:

1. Compute the covariance matrix of the data


2. Calculate the eigenvalues and eigenvectors of the covariance matrix
3. Select the eigenvectors with the largest eigenvalues as the principal components
4. Project the data onto the principal components to obtain the transformed data

PCA is useful for data visualization, noise reduction, and feature extraction.

Linear Discriminant Analysis (LDA): LDA is a supervised dimensionality reduction technique that is used for clas-
sification problems. It finds the directions (linear discriminants) that maximize the separation between different
classes while minimizing the variance within each class.

The key steps in LDA are:

1. Compute the mean of each class


2. Compute the within-class and between-class scatter matrices
3. Calculate the eigenvalues and eigenvectors of the scatter matrices
4. Select the eigenvectors with the largest eigenvalues as the linear discriminants
5. Project the data onto the linear discriminants to obtain the transformed data

LDA is useful for classification tasks, where the goal is to find the directions that best separate the classes.

CODE:

# Importing necessary libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Standardize the features


X_std = StandardScaler().fit_transform(X)

# Apply PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_std)
22
# Apply LDA
lda = LinearDiscriminantAnalysis(n_components=2)
X_lda = lda.fit_transform(X_std, y)

# Plot the results


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
for target, label in zip(range(3), ['Setosa', 'Versicolor', 'Virginica']):
plt.scatter(X_pca[y == target, 0], X_pca[y == target, 1], label=label)
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA on Iris dataset')
plt.legend()

plt.subplot(1, 2, 2)
for target, label in zip(range(3), ['Setosa', 'Versicolor', 'Virginica']):
plt.scatter(X_lda[y == target, 0], X_lda[y == target, 1], label=label)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.title('LDA on Iris dataset')
plt.legend()

plt.tight_layout()
plt.show()

OUTPUT:

23
Experiment – 8

AIM: Program to demonstrate DBSCAN clustering algorithm

THEORY:

The DBSCAN (Density-Based Spatial Clustering of Applications with Noise) algorithm is a density-based clustering
algorithm that can discover clusters of arbitrary shape and identify noise or outlier data points in a dataset. It groups
together data points that are closely packed (high density), while marking outliers as noise.

The DBSCAN algorithm works based on two key parameters:

1. eps (epsilon): The maximum distance between two points for them to be considered neighbors.
2. min_samples: The minimum number of points required to form a dense region or cluster.

The algorithm works as follows:

1. For each point p in the dataset, find the number of points that are within a distance eps from p. These points
are called the neighbors of p.
2. If the number of neighbors of p is greater than or equal to min_samples, then p is considered a core point,
and a new cluster is formed with p and its neighbors.
3. If the number of neighbors of p is less than min_samples, then p is considered a border point if it is a
neighbor of a core point, or a noise point otherwise.
4. All points that are reachable from a core point through a chain of core points and border points are added to
the same cluster.
5. The process continues until all points in the dataset are either assigned to a cluster or marked as noise.

CODE:

import numpy as np
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler

# Generate sample data


centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4,
random_state=0)

X = StandardScaler().fit_transform(X)

# Compute DBSCAN
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.


n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)

print('Estimated number of clusters: %d' % n_clusters_)


print('Estimated number of noise points: %d' % n_noise_)
24
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
print("Adjusted Rand Index: %0.3f"
% metrics.adjusted_rand_score(labels_true, labels))
print("Adjusted Mutual Information: %0.3f"
% metrics.adjusted_mutual_info_score(labels_true, labels))
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels))

# Plot result
import matplotlib.pyplot as plt
%matplotlib inline

# Black removed and is used for noise instead.


unique_labels = set(labels)
colors = [plt.cm.Spectral(each)
for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = [0, 0, 0, 1]

class_member_mask = (labels == k)

xy = X[class_member_mask & core_samples_mask]


plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
markeredgecolor='k', markersize=14)

xy = X[class_member_mask & ~core_samples_mask]


plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
markeredgecolor='k', markersize=6)

plt.title('Estimated number of clusters: %d' % n_clusters_)


plt.show()

OUTPUT:

25
Experiment – 9

AIM: Program to demonstrate K-Medoid clustering algorithm

THEORY:

The K-Medoids clustering algorithm is a partitioning technique similar to the K-Means algorithm but is more robust to
outliers and noise in the data. Instead of using the mean of the data points as the cluster centroid, K-Medoids uses
actual data points as the cluster centers, called medoids. This makes K-Medoids more robust to outliers since it
minimizes the sum of pairwise dissimilarities instead of minimizing the squared Euclidean distances.

The K-Medoids algorithm works as follows:

1. Initialize: Randomly select k data points as the initial medoids.


2. Assignment: Assign each remaining data point to the cluster with the nearest medoid.
3. Update: For each cluster, find the new medoid that minimizes the sum of dissimilarities (distances) between it
and the other points in the cluster. Replace the old medoid with the new medoid.
4. Repeat steps 2 and 3 until the medoids stop changing or a maximum number of iterations is reached.

The most commonly used algorithm for K-Medoids is the Partitioning Around Medoids (PAM) algorithm, which is
implemented in scikit-learn as KMedoids.

CODE:

# Importing necessary libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn_extra.cluster import KMedoids

# Generating synthetic data (blobs dataset)


X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.6, random_state=42)

# Applying K-Medoids
kmedoids = KMedoids(n_clusters=4, random_state=42)
y_kmedoids = kmedoids.fit_predict(X)

# Plotting the results


plt.figure(figsize=(8, 6))
for i in range(4):
plt.scatter(X[y_kmedoids == i, 0], X[y_kmedoids == i, 1], label=f'Cluster {i+1}')
plt.scatter(kmedoids.cluster_centers_[:, 0], kmedoids.cluster_centers_[:, 1], marker='o', color='black', label='Medoids')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-Medoids Clustering')
plt.legend()
plt.show()

26
OUTPUT:

27
Experiment – 10

AIM: Program to demonstrate K-Means Clustering Algorithm on Handwritten Dataset

THEORY:

K-Means clustering is an unsupervised machine learning algorithm used to partition a dataset into K distinct clusters.
The algorithm aims to find the centroids (means) of the clusters and assign each data point to the cluster with the
nearest centroid.

The K-Means algorithm works as follows:

1. Initialization: Randomly select K initial centroids from the dataset.


2. Assignment: Assign each data point to the cluster with the nearest centroid based on a distance metric (e.g.,
Euclidean distance).
3. Update: Calculate the new centroids by taking the mean of all data points assigned to each cluster.
4. Repeat: Repeat steps 2 and 3 until the centroids stop changing or a maximum number of iterations is reached.

The objective function of K-Means is to minimize the sum of squared distances between each data point and its as-
signed centroid, also known as the inertia or within-cluster sum of squares (WCSS).

The number of clusters, K, is a hyperparameter that needs to be specified beforehand. There are various techniques to
determine the optimal value of K, such as the elbow method, silhouette analysis, or gap statistic .

CODE:

# Importing necessary libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

# Load the Handwritten Digits dataset


digits = load_digits()
X = digits.data
y = digits.target

# Apply PCA for visualization


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# Apply K-Means
kmeans = KMeans(n_clusters=10, n_init=10, random_state=42)

y_kmeans = kmeans.fit_predict(X)

# Plotting the results


plt.figure(figsize=(12, 6))

# Plot original data


plt.subplot(1, 2, 1)
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis', s=10)
28
plt.title('Original Data')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')

# Plot clustered data


plt.subplot(1, 2, 2)
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_kmeans, cmap='viridis', s=10)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], marker='o', color='red', s=100,
label='Centroids')
plt.title('K-Means Clustering')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend()

plt.tight_layout()
plt.show()

OUTPUT:

29
30
Experiment – 1

AIM: Exploring Google Colab.

Google Colab is a free cloud-based platform provided by Google that allows you to write and execute
Python code in a Jupyter notebook-like environment. It's a great tool for experimenting with machine
learning models, data analysis, and collaborative coding. Here's a simple example to get you started with
Google Colab:
Accessing Google Colab:
1. Go to Google Colab and sign in with your Google account.
 You'll be redirected to the Colab dashboard where you can create new notebooks or open
existing ones.
 Creating a New Notebook:
2. Click on "New Notebook" to create a new notebook.
 You can also upload an existing notebook or open a notebook from Google Drive or GitHub.
 Running Code:
3. In a code cell, you can write and execute Python code. For example, you can print "Hello, World!" by
typing print("Hello, World!") in a cell and pressing Shift+Enter to execute it.
 Colab provides access to a wide range of libraries and frameworks, including NumPy, pandas,
TensorFlow, and PyTorch.
 Using Markdown Cells:
4. You can add text, headings, and formatted content using Markdown cells. Simply change the cell type to
"Markdown" and start typing your Markdown content.
5. Saving and Sharing Notebooks:
 Colab automatically saves your notebook to Google Drive. You can also download it as a .ipynb file or
save it to GitHub.
 You can share your notebook with others by clicking on the "Share" button in the top right corner.
You can share it with specific people or make it accessible to anyone with the link.

CODE:

import numpy as np
import matplotlib.pyplot as plt

# Generate some random data


x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plot the data


plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Plot of sin(x)')
plt.grid(True)
plt.show()

31
OUTPUT:

32
Experiment – 2

AIM: A program for linear regression model using scikit-learn but no machine learning

CODE:

import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# Generate some synthetic data


np.random.seed(0)
X = np.random.rand(100, 1) * 10 # Feature (independent variable)
y = 2 * X + 3 + np.random.randn(100, 1) * 2 # Target (dependent variable)

# Fit linear regression model


model = LinearRegression()
model.fit(X, y)

# Get the slope (coefficient) and intercept of the linear regression line
slope = model.coef_[0][0]
intercept = model.intercept_[0]

# Generate predictions using the model


y_pred = model.predict(X)

# Plot the data and the linear regression line


plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label=f'Linear Regression (y = {slope:.2f}x + {intercept:.2f})')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression')
plt.legend()
plt.grid(True)
plt.show()

OUTPUT:

33
Experiment – 3

AIM: P A program without using scikit-learn or machine learning for Linear regression.

CODE:

import numpy as np
import matplotlib.pyplot as plt

# Generate some synthetic data


np.random.seed(0)
X = np.random.rand(100, 1) * 10 # Feature (independent variable)
y = 2 * X + 3 + np.random.randn(100, 1) * 2 # Target (dependent variable)

# Calculate the slope (coefficient) and intercept of the linear regression line using ordinary least squares (OLS)
X_mean = np.mean(X)
y_mean = np.mean(y)
numerator = np.sum((X - X_mean) * (y - y_mean))
denominator = np.sum((X - X_mean) ** 2)
slope = numerator / denominator
intercept = y_mean - slope * X_mean

# Generate predictions using the linear regression model


y_pred = slope * X + intercept

# Plot the data and the linear regression line


plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label=f'Linear Regression (y = {slope:.2f}x + {intercept:.2f})')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression without Scikit-learn')
plt.legend()
plt.grid(True)
plt.show()

OUTPUT:

34
Experiment – 4

AIM: Create a decision tree without using sklearn library, just using the concept of entropy and choose what
node will be the root, and subsequent decision parameters based on information gain. Show all calculations.

CODE:
import numpy as np

class Node:
def __init__(self, feature=None, threshold=None, left=None, right=None, value=None):
self.feature = feature # Feature index
self.threshold = threshold # Threshold value for binary splitting
self.left = left # Left child node
self.right = right # Right child node
self.value = value # Value (class) for leaf nodes

def entropy(y):
_, counts = np.unique(y, return_counts=True)
probabilities = counts / len(y)
return -np.sum(probabilities * np.log2(probabilities + 1e-10))

def information_gain(X, y, feature, threshold):


left_mask = X[:, feature] <= threshold
right_mask = ~left_mask
left_entropy = entropy(y[left_mask])
right_entropy = entropy(y[right_mask])
parent_entropy = entropy(y)
left_weight = np.sum(left_mask) / len(y)
right_weight = np.sum(right_mask) / len(y)
return parent_entropy - (left_weight * left_entropy + right_weight * right_entropy)

def find_best_split(X, y):


best_gain = 0
best_feature = None
best_threshold = None
n_features = X.shape[1]
for feature in range(n_features):
thresholds = np.unique(X[:, feature])
for threshold in thresholds:
gain = information_gain(X, y, feature, threshold)
if gain > best_gain:
best_gain = gain
best_feature = feature
best_threshold = threshold
return best_feature, best_threshold

def build_tree(X, y, max_depth=None):


if max_depth == 0 or len(np.unique(y)) == 1:
return Node(value=np.argmax(np.bincount(y)))
feature, threshold = find_best_split(X, y)
if feature is None:
return Node(value=np.argmax(np.bincount(y)))
left_mask = X[:, feature] <= threshold
right_mask = ~left_mask
left_subtree = build_tree(X[left_mask], y[left_mask], max_depth - 1 if max_depth else None)

35
right_subtree = build_tree(X[right_mask], y[right_mask], max_depth - 1 if max_depth else None)
return Node(feature=feature, threshold=threshold, left=left_subtree, right=right_subtree)

def predict_one(tree, sample):


if tree.value is not None:
return tree.value
if sample[tree.feature] <= tree.threshold:
return predict_one(tree.left, sample)
else:
return predict_one(tree.right, sample)

def predict(tree, X):


return [predict_one(tree, sample) for sample in X]

# Example dataset
X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1],
[1, 1]])
y = np.array([0, 0, 1, 1, 0])

# Build the decision tree


tree = build_tree(X, y)

# Predictions
print("Predictions:", predict(tree, X))

OUTPUT:

36
Experiment – 5

AIM: Show how decision trees can be used for both classification and regression using a program with
sample dataset. Also visualize the decision tree obtained

CODE:

Classification Example:

from sklearn.datasets import load_iris


from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

# Load the Iris dataset


iris = load_iris()
X_cls = iris.data[:, 2:] # Use petal length and width as features
y_cls = iris.target

# Build and visualize the decision tree for classification


clf = DecisionTreeClassifier()
clf.fit(X_cls, y_cls)

plt.figure(figsize=(12, 8))
plot_tree(clf, filled=True, feature_names=iris.feature_names[2:],
class_names=iris.target_names)
plt.show()

37
Regression Example:

import numpy as np
from sklearn.tree import DecisionTreeRegressor

# Generate sample regression data


np.random.seed(0)
X_reg = np.linspace(0, 10, 100).reshape(-1, 1)
y_reg = 2 * X_reg[:, 0] + np.random.randn(100) # Linear relationship with some
noise

# Build and visualize the decision tree for regression


reg = DecisionTreeRegressor()
reg.fit(X_reg, y_reg)

plt.figure(figsize=(12, 8))
plot_tree(reg, filled=True)
plt.show()

38
Experiment – 6

AIM: Write your own implementation for knn with 4 different distance metrics and check on Iris dataset

CODE:
import numpy as np
from collections import Counter
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

class KNN:
def __init__(self, k=3, metric='euclidean'):
self.k = k
self.metric = metric

def euclidean_distance(self, x1, x2):


return np.sqrt(np.sum((x1 - x2)**2))

def manhattan_distance(self, x1, x2):


return np.sum(np.abs(x1 - x2))

def chebyshev_distance(self, x1, x2):


return np.max(np.abs(x1 - x2))

def minkowski_distance(self, x1, x2, p=2):


return np.sum(np.abs(x1 - x2) ** p) ** (1 / p)

def get_distance(self, x1, x2):


if self.metric == 'euclidean':
return self.euclidean_distance(x1, x2)
elif self.metric == 'manhattan':
return self.manhattan_distance(x1, x2)
elif self.metric == 'chebyshev':
return self.chebyshev_distance(x1, x2)
elif self.metric == 'minkowski':
return self.minkowski_distance(x1, x2)
else:
raise ValueError("Invalid distance metric. Choose from 'euclidean',
'manhattan', 'chebyshev', or 'minkowski'.")

def fit(self, X_train, y_train):


self.X_train = X_train
self.y_train = y_train

def predict(self, X_test):


predictions = []
for x_test in X_test:
distances = []
for i, x_train in enumerate(self.X_train):
distance = self.get_distance(x_test, x_train)

39
distances.append((distance, self.y_train[i]))
distances.sort(key=lambda x: x[0])
neighbors = distances[:self.k]
labels = [neighbor[1] for neighbor in neighbors]
prediction = Counter(labels).most_common(1)[0][0]
predictions.append(prediction)
return predictions

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Split the dataset into train and test sets


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

# Define KNN with different distance metrics


distance_metrics = ['euclidean', 'manhattan', 'chebyshev', 'minkowski']
for metric in distance_metrics:
knn = KNN(k=3, metric=metric)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy using {metric} distance metric: {accuracy:.4f}")

OUTPUT:

40
Experiment – 7

AIM: Apply your own implementation of Knn to any instance of Pima Indians Diabetes Database from
Kaggle or Github and submit notebooks for all 3 exercises.

CODE:

import csv
import math

# Load the data from the CSV file


data = []
with open('pima-indians-diabetes.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip the header row
for row in reader:
data.append([float(x) for x in row[:8]] + [int(row[8])])

# Split the data into features (X) and target (y)


X = [row[:-1] for row in data]
y = [row[-1] for row in data]

# Function to calculate the Euclidean distance between two instances


def euclidean_distance(instance1, instance2):
distance = 0
for i in range(len(instance1)):
distance += pow((instance1[i] - instance2[i]), 2)
return math.sqrt(distance)

# Function to get the k nearest neighbors


def get_neighbors(X_train, X_test_instance, y_train, k):
distances = []
for i in range(len(X_train)):
distance = euclidean_distance(X_train[i], X_test_instance)
distances.append((distance, y_train[i]))
distances.sort(key=lambda x: x[0])
neighbors = [distances[i][1] for i in range(k)]
return neighbors

# Function to make a prediction


def predict(X_test_instance, X_train, y_train, k):
neighbors = get_neighbors(X_train, X_test_instance, y_train, k)
class_counts = {}
for neighbor in neighbors:
if neighbor in class_counts:
class_counts[neighbor] += 1
else:
class_counts[neighbor] = 1
sorted_class_counts = sorted(class_counts.items(), key=lambda x: x[1], re-
verse=True)
prediction = sorted_class_counts[0][0]
return prediction
41
# Function to evaluate the model
def evaluate(X, y, k):
correct = 0
for i in range(len(X)):
X_train = X[:i] + X[i+1:]
y_train = y[:i] + y[i+1:]
X_test = [X[i]]
y_test = y[i]
prediction = predict(X_test[0], X_train, y_train, k)
if prediction == y_test:
correct += 1
accuracy = correct / len(X)
return accuracy

# Test the model with different values of k


for k in [1, 3, 5, 7, 9]:
accuracy = evaluate(X, y, k)
print(f"Accuracy for k={k}: {accuracy:.2f}")

OUTPUT:

Accuracy for k=1: 0.73


Accuracy for k=3: 0.71
Accuracy for k=5: 0.74
Accuracy for k=7: 0.72
Accuracy for k=9: 0.71

42
Experiment – 8

AIM: Explain the difference between two approaches

THEORY:

Principal Component Analysis (PCA) and Linear Discriminant Analysis (LDA) are both dimensionality re-
duction techniques used in machine learning, but they differ in their underlying principles and the way they
approach the problem.

Principal Component Analysis (PCA): PCA is an unsupervised technique that aims to find the directions
(principal components) that maximize the variance in the data. It does this by creating new uncorrelated fea-
tures (principal components) that are linear combinations of the original features. These new features are
ordered by the amount of variance they capture in the data.

The key aspects of PCA are:

1. Unsupervised: PCA is an unsupervised technique, meaning it does not consider the target variable
or class labels during the transformation.
2. Maximize Variance: PCA seeks to find the directions that capture the maximum variance in the
data, regardless of the class labels.
3. Dimensionality Reduction: PCA can be used for dimensionality reduction by selecting the top prin-
cipal components that capture most of the variance in the data while discarding the less important
components.
4. Feature Extraction: PCA creates new features (principal components) that are linear combinations
of the original features.

Linear Discriminant Analysis (LDA): LDA, on the other hand, is a supervised technique that aims to find
the directions (linear discriminants) that maximize the separation between classes or categories in the data. It
does this by creating new features (linear discriminants) that are linear combinations of the original features,
but unlike PCA, it takes into account the class labels during the transformation.

The key aspects of LDA are:

1. Supervised: LDA is a supervised technique that considers the target variable or class labels during
the transformation.
2. Maximize Class Separability: LDA seeks to find the directions that maximize the separability
between classes or categories in the data.
3. Dimensionality Reduction: LDA can also be used for dimensionality reduction by selecting the top
linear discriminants that capture most of the class separability.
4. Feature Extraction: LDA creates new features (linear discriminants) that are linear combinations of
the original features, optimized for class separation.

The main difference between PCA and LDA lies in their objectives. PCA aims to maximize the variance in
the data, regardless of the class labels, while LDA aims to maximize the separability between classes or cat-
egories.

43
Experiment – 9

AIM: Use colab examples given in the classroom group for applying PCA and LDA to different datasets,
and share an EXPLANATION about how do you interpret the results.

CODE:

# Import necessary libraries


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.preprocessing import StandardScaler

# Load data (replace 'data.csv' with your dataset filename)


data = pd.read_csv('data.csv')

# Separate features and target variable


X = data.drop('target', axis=1)
y = data['target']

# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Apply PCA
pca = PCA(n_components=2) # Specify number of components to keep
X_pca = pca.fit_transform(X_scaled)

# Apply LDA
lda = LinearDiscriminantAnalysis(n_components=2)
X_lda = lda.fit_transform(X_scaled, y)

# Visualize PCA and LDA results


plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis')
plt.title('PCA')

plt.subplot(1, 2, 2)
plt.scatter(X_lda[:, 0], X_lda[:, 1], c=y, cmap='viridis')
plt.title('LDA')

plt.show()

44
OUTPUT:

45
Experiment – 10

AIM: Compare K-means clustering and DBSCAN for IRIS based on performance metrics. Which approach
is better? Why?

CODE:

from sklearn.cluster import KMeans, DBSCAN


from sklearn.metrics import silhouette_score, davies_bouldin_score,
completeness_score
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# K-means clustering
kmeans = KMeans(n_clusters=3, n_init=10, random_state=42)

kmeans_labels = kmeans.fit_predict(X_scaled)

# DBSCAN clustering
dbscan = DBSCAN(eps=0.5, min_samples=5)
dbscan_labels = dbscan.fit_predict(X_scaled)

# Evaluate performance metrics


silhouette_kmeans = silhouette_score(X_scaled, kmeans_labels)
silhouette_dbscan = silhouette_score(X_scaled, dbscan_labels)
davies_bouldin_kmeans = davies_bouldin_score(X_scaled, kmeans_labels)
davies_bouldin_dbscan = davies_bouldin_score(X_scaled, dbscan_labels)
completeness_kmeans = completeness_score(y, kmeans_labels)
completeness_dbscan = completeness_score(y, dbscan_labels)

print("K-means clustering:")
print("Silhouette score:", silhouette_kmeans)
print("Davies-Bouldin index:", davies_bouldin_kmeans)
print("Completeness score:", completeness_kmeans)

print("\nDBSCAN clustering:")
print("Silhouette score:", silhouette_dbscan)
print("Davies-Bouldin index:", davies_bouldin_dbscan)
print("Completeness score:", completeness_dbscan)

46
OUTPUT:

47
Experiment – 11

AIM: Apply SVM on IRIS data set from sklearn for classification

CODE:

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Split the dataset into train and test sets


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

# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Create and train the SVM classifier


svm = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
svm.fit(X_train_scaled, y_train)

# Make predictions on the test set


y_pred = svm.predict(X_test_scaled)

# Evaluate the classifier


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

# Classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))

48
OUTPUT:

49

You might also like