0% found this document useful (0 votes)
6 views66 pages

ML Lab

Uploaded by

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

ML Lab

Uploaded by

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

Jyothy Charitable Trust ®

JYOTHY INSTITUTE OF TECHNOLOGY


Tataguni, off Kanakapura road, Bengaluru-560082
Approved by The All India Council for Technical Education (AICTE) - New Delhi
Affiliated to Visvesvaraya Technological University(VTU), Belagavi

Department of Computer Science & Engineering


Accredited by National Board of Accreditation (NBA) - New Delhi.

LAB MANUAL

“Machine Learning Laboratory”


BCSL606
Semester: 6th CSE Scheme: 2022

Prepared & Compiled By:

[Link] R Dr. Ramya B N Mr. Srihari A


Assistant Professor, Associate Professor, Assistant Professor,
Department of CSE, Department of CSE, Department of CSE,
JIT – Bangalore JIT – Bangalore JIT – Bangalore

Verified and Approved by

DAEC HoD-CSE
INSTITUTION VISION & MISSION

VISION

To be an institution of excellence in Engineering education, Innovation and Research and work


towards evolving great leaders for the country’s future and meeting global needs.

MISSION

The Institution aims at providing a vibrant, intellectually and emotionally rich teaching learning
environment with state of art infrastructure and recognizing and nurturing the potential of each
individual to evolve into one’s own self and contribute to the welfare of all.

DEPARTMENT OF CSE VISION & MISSION

VISION

To be a centre of excellence in Computer Science and Engineering education, focus on research,


innovation and entrepreneurial skill development with professional competency.

MISSION

M1: To provide state-of-the-art ICT infrastructure and innovative, research oriented teaching learning
environment and motivation for self-learning & problem-solving abilities by recruiting
committed faculty.

M2: To Encourage Industry Institute Interaction & multi-disciplinary approach to problem-


solving and adapt to the ever-changing global IT trends.

M3: To Imbibe awareness on societal responsibility and leadership qualities with professional
competency and ethics.
PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)
PEO1: Graduates of Computer Science and Engineering will be able to utilize
mathematics, science, engineering fundamentals, theoretical as well as laboratory
based experiences to identify, formulate & solve engineering problems and
succeed in entry-level engineering positions in ITES or in advanced engineering.
PEO2: Graduates of Computer Science and Engineering will be prepared to communicate
and work effectively on individual & team based engineering projects while
practicing the ethics of their profession consistent with a sense of social
responsibility.
PEO3: Graduates of Computer Science and Engineering will be equipped to recognize the
importance of, and have the skills for, continuous learning to become experts in
their domain and enhance their professional attributes.

PROGRAMME SPECIFIC OUTCOMES (PSOs)


PSO1: Explore emerging technologies in the field of Computer Science and Engineering
using the knowledge and skills gained through science and engineering courses.
PSO2: Apply techniques in different domains to create innovative products and services
in the database/data analytics, network/web systems, embedded systems and
system software.
PSO3: Work on various platforms as an individual/team member to develop useful and
safe computer based systems for the society and nation.
PROGRAM OUTCOMES (POs)
PO1: Engineering knowledge: Apply the knowledge of mathematics, science,
engineering fundamentals, and an engineering specialization to the solution of
complex engineering problems.
PO2: Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.
PO3: Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified
needs with appropriate consideration for the public health and safety, and the
cultural, societal, and environmental considerations.
PO4: Conduct investigations of complex problems: Use research-based knowledge
and research methods including design of experiments, analysis and interpretation
of data, and synthesis of the information to provide valid conclusions.
PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modelling to
complex engineering activities with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual
knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a
member or leader in diverse teams, and in multidisciplinary settings.
PO10: Communication: Communicate effectively on complex engineering activities
with the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make
effective presentations, and give and receive clear instructions.
PO11: Project management and finance: Demonstrate knowledge and understanding
of the engineering and management principles and apply these to one‟s own
work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
PO12: Life-long learning: Recognize the need for, and have the preparation and ability
to engage in independent and life-long learning in the broadest context of
technological change.
Machine Learning Laboratory- BCSL606

Machine Learning lab Semester 6


Course Code BCSL606 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 0:0:2:0 SEE Marks 50
Credits 01 Exam Hours 100
Examination type (SEE) Practical
Course objectives:
• To become familiar with data and visualize univariate, bivariate, and multivariate data using
statistical techniques and dimensionality reduction.
• To understand various machine learning algorithms such as similarity-based learning, regression,
decision trees, and clustering.
• To familiarize with learning theories, probability-based models and developing the skills required for
decision- making in dynamic environments.
[Link] Experiments
1 Develop a program to create histograms for all numerical features and analyze the distribution of
each feature. Generate box plots for all numerical features and identify any outliers. Use California
Housing dataset.

Book 1: Chapter 2
2 Develop a program to Compute the correlation matrix to understand the relationships between
pairs of features. Visualize the correlation matrix using a heatmap to know which variables have
strong positive/negative correlations. Create a pair plot to visualize pairwise relationships
between features. Use California Housing dataset.

Book 1: Chapter 2
3 Develop a program to implement Principal Component Analysis (PCA) for reducing the
dimensionality of the Iris dataset from 4 features to 2.

Book 1: Chapter 2
4 For a given set of training data examples stored in a .CSV file, implement and demonstrate the Find-
S algorithm to output a description of the set of all hypotheses consistent with the training
examples.

Book 1: Chapter 3
5 Develop a program to implement k-Nearest Neighbour algorithm to classify the randomly
generated 100 values of x in the range of [0,1]. Perform the following based on dataset generated.

Label the first 50 points {x1,……,x50} as follows: if (xi ≤ 0.5), then xi Class1, else xi
Class1 Classify the remaining points, x51,……,x100 using KNN. Perform this for
k=1,2,3,4,5,20,30

Book 2: Chapter – 2
6 Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points.
Select appropriate data set for your experiment and draw graphs

Book 1: Chapter – 4
7 Develop a program to demonstrate the working of Linear Regression and Polynomial Regression.
Use Boston Housing Dataset for Linear Regression and Auto MPG Dataset (for vehicle fuel
efficiency prediction) for Polynomial Regression.

Book 1: Chapter – 5

Dept. of CSE, JIT-Bangalore Page 1


Machine Learning Laboratory- BCSL606

8 Develop a program to demonstrate the working of the decision tree algorithm. Use Breast Cancer
Data set for building the decision tree and apply this knowledge to classify a new sample.

Book 2: Chapter – 3
9 Develop a program to implement the Naive Bayesian classifier considering Olivetti Face Data
set for training. Compute the accuracy of the classifier, considering a few test data sets.

Book 2: Chapter – 4
10 Develop a program to implement k-means clustering using Wisconsin Breast Cancer data set and
visualize the clustering result.

Book 2: Chapter – 4
Course outcomes (Course Skill Set):
At the end of the course the student will be able to:
● Illustrate the principles of multivariate data and apply dimensionality reduction techniques.
● Demonstrate similarity-based learning methods and perform regression analysis.
● Develop decision trees for classification and regression problems, and Bayesian models for
probabilistic learning.
• Implement the clustering algorithms to share computing resources.

Dept. of CSE, JIT-Bangalore Page 2


Machine Learning Laboratory- BCSL606

EXPERIMENT 1

1. Develop a program to create histograms for all numerical features and analyze the
distribution of each feature. Generate box plots for all numerical features and
identify any outliers. Use California Housing dataset.

import pandas as pd
import [Link] as plt

# Load local dataset (replace filename if needed)


df = pd.read_csv("[Link]")

for col in df.select_dtypes(include=['float64','int64']).columns:


df[col].plot(kind='hist', bins=30, title=f'Histogram of {col}')
[Link]()

df[col].plot(kind='box', title=f'Boxplot of {col}')


[Link]()

Q1, Q3 = df[col].quantile([0.25, 0.75])


IQR = Q3 - Q1
outliers = df[(df[col] < Q1 - 1.5*IQR) | (df[col] > Q3 + 1.5*IQR)]
print(f"{col}: {len(outliers)} outliers")

Dept. of CSE, JIT-Bangalore Page 3


Machine Learning Laboratory- BCSL606

OUTPUT

Outliers Detection:
MedHouseVal: 1071 outliers
Total_rooms: 1287 outliers
Total_bedrooms: 1271 outliers
Population: 1196 outliers
Households: 1220 outliers
Median_income: 681 outliers

Dept. of CSE, JIT-Bangalore Page 4


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM-1

Develop a program to visualize skewness and distribution of numerical features using


density plots (KDE) and log-transformed histograms.

# Import required libraries


import pandas as pd
import [Link] as plt
import seaborn as sns
import numpy as np

from [Link] import load_iris

# -------------------------------
# Step 1: Load dataset (offline)
# -------------------------------
iris = load_iris(as_frame=True)
df = [Link]

print("Iris dataset loaded successfully")


print("Dataset shape:", [Link])

# -------------------------------
# Step 2: KDE Plot
# -------------------------------
[Link](figsize=(6, 4))
[Link](df['sepal length (cm)'], shade=True)
[Link]("KDE Plot of Sepal Length")
[Link]("Sepal Length (cm)")
[Link]("Density")
[Link](True)
[Link]()

# -------------------------------
# Step 3: Log Transformed Histogram
# -------------------------------
[Link](figsize=(6, 4))
[Link](np.log1p(df['sepal length (cm)']), bins=30)
[Link]("Log Transformed Histogram of Sepal Length")
[Link]("Log(Sepal Length)")
[Link]("Frequency")
[Link](True)

Dept. of CSE, JIT-Bangalore Page 5


Machine Learning Laboratory- BCSL606

OUTPUT

Dept. of CSE, JIT-Bangalore Page 6


Machine Learning Laboratory- BCSL606

EXPERIMENT 2

2. Develop a program to Compute the correlation matrix to understand the


relationships between pairs of features. Visualize the correlation matrix using a
heatmap to know which variables have strong positive/negative correlations.
Create a pair plot to visualize pairwise relationships between features. Use
California Housing dataset.

import pandas as pd
import [Link] as plt
from [Link] import scatter_matrix

# Load local dataset


df = pd.read_csv("[Link]")

# Keep numeric columns only


df_num = df.select_dtypes(include=['number'])

# Correlation matrix
corr = df_num.corr()
print(corr)

# Heatmap
[Link](corr, cmap='coolwarm', interpolation='nearest')
[Link](range(len(corr)), [Link], rotation=45, ha='right')
[Link](range(len(corr)), [Link])
[Link]()
[Link]()

# Pair plot
scatter_matrix(df_num, figsize=(12, 12), diagonal='hist')
[Link]()

Dept. of CSE, JIT-Bangalore Page 7


Machine Learning Laboratory- BCSL606

OUTPUT

longitude latitude housing_ median _age total_rooms \


longitude 1.000000 -0.924664 -0.108197 0.044568
latitude -0.924664 1.000000 0.011173 -0.036100
housing_median_age -0.108197 0.011173 1.000000 -0.361262
total_rooms 0.044568 -0.036100 -0.361262 1.000000
total_bedrooms 0.069608 -0.066983 -0.320451 0.930380
population 0.099773 -0.108785 -0.296244 0.857126
households 0.055310 -0.071035 -0.302916 0.918484
median_income -0.015176 -0.079809 -0.119034 0.198050
median_house_value -0.045967 -0.144160 0.105623 0.134153

total_bedrooms population households median_income \


longitude 0.069608 0.099773 0.055310 -0.015176
latitude -0.066983 -0.108785 -0.071035 -0.079809
housing_median_age -0.320451 -0.296244 -0.302916 -0.119034
total_rooms 0.930380 0.857126 0.918484 0.198050
total_bedrooms 1.000000 0.877747 0.979728 -0.007723
population 0.877747 1.000000 0.907222 0.004834
households 0.979728 0.907222 1.000000 0.013033
median_income -0.007723 0.004834 0.013033 1.000000
median_house_value 0.049686 -0.024650 0.065843 0.688075

median_house_value
longitude -0.045967
latitude -0.144160
housing_median_age 0.105623
total_rooms 0.134153
total_bedrooms 0.049686
population -0.024650
households 0.065843
median_income 0.688075
median_house_value 1.000000

Dept. of CSE, JIT-Bangalore Page 8


Machine Learning Laboratory- BCSL606

Dept. of CSE, JIT-Bangalore Page 9


Machine Learning Laboratory- BCSL606

Dept. of CSE, JIT-Bangalore Page 10


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 2
Write a Python program to compute the correlation matrix of a dataset and identify highly
correlated feature pairs.

import pandas as pd
from [Link] import load_breast_cancer

# -------------------------------
# Step 1: Load dataset (offline)
# -------------------------------
data = load_breast_cancer(as_frame=True)
df = [Link]

print("Dataset loaded successfully")


print("Dataset shape:", [Link])

# -------------------------------
# Step 2: Compute correlation matrix
# -------------------------------
corr = [Link]()

# -------------------------------
# Step 3: Find highly correlated pairs
# -------------------------------
print("\nHighly correlated pairs (|correlation| > 0.8):")
for i in range(len([Link])):
for j in range(i):
if abs([Link][i, j]) > 0.8:
print([Link][i], " & ", [Link][j], " : ", [Link][i, j])

Dept. of CSE, JIT-Bangalore Page 11


Machine Learning Laboratory- BCSL606

OUTPUT

Dept. of CSE, JIT-Bangalore Page 12


Machine Learning Laboratory- BCSL606

EXPERIMENT 3

3. Develop a program to implement Principal Component Analysis (PCA) for


reducing the dimensionality of the Iris dataset from 4 features to 2.

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from [Link] import PCA
from [Link] import StandardScaler

# Load local Iris dataset WITH header


df = pd.read_csv("[Link]") # change filename if needed

# Extract features and target


X = [Link][:, 0:4].values # numeric columns only
y = [Link][:, -1].values # species column

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

# Apply PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

# Create DataFrame for plotting


df_pca = [Link](X_pca, columns=['PC1', 'PC2'])
df_pca['Species'] = y

Dept. of CSE, JIT-Bangalore Page 13


Machine Learning Laboratory- BCSL606

# Plot
[Link](figsize=(8, 6))
[Link](x='PC1', y='PC2', hue='Species', palette='Set1', data=df_pca)
[Link]('PCA on Iris Dataset (4D → 2D)')
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link](title='Species')
[Link](True)
[Link]()

# Explained variance
print("Explained Variance Ratio:", pca.explained_variance_ratio_)
print("Total Variance Explained:", pca.explained_variance_ratio_.sum())

OUTPUT

Explained Variance Ratio: [0.70308855 0.22873259]


Total Variance Explained: 0.9318211437893085

Dept. of CSE, JIT-Bangalore Page 14


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 3

Write a Python program to apply PCA on the Iris dataset after standardizing the features.

from [Link] import load_iris


from [Link] import StandardScaler
from [Link] import PCA
import [Link] as plt

iris = load_iris()
X = [Link]

X_scaled = StandardScaler().fit_transform(X)

pca = PCA(0.95)
X_pca = pca.fit_transform(X_scaled)

print("Number of components:", pca.n_components_)

[Link](pca.explained_variance_ratio_.cumsum())
[Link]("Components")
[Link]("Cumulative Variance")
[Link]()

OUTPUT

Dept. of CSE, JIT-Bangalore Page 15


Machine Learning Laboratory- BCSL606

EXPERIMENT 4

4. For a given set of training data examples stored in a .CSV file, implement and
demonstrate the Find-S algorithm to output a description of the set of all
hypotheses consistent with the training examples.
import numpy as np
import pandas as pd
data = pd.read_csv('[Link]')

def train(concepts, target):


# Initialize specific hypothesis with first positive example
for i, val in enumerate(target):
if val == "Yes":
specific_h = concepts[i].copy()
break

# Update hypothesis
for i, h in enumerate(concepts):
if target[i] == "Yes":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = "?"

return specific_h

# Extract concepts and target


concepts = [Link]([Link][:, 0:-1])
target = [Link]([Link][:, -1])

# Train and print result


print(train(concepts, target))

OUTPUT
['Sunny' 'Warm' 'High' 'Strong' '?' '?']

Dept. of CSE, JIT-Bangalore Page 16


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 4

Implement the Find-S algorithm in Python to find the most specific hypothesis consistent with
the given training data.

import pandas as pd
import numpy as np

# -------------------------------
# Step 1: Create dataset (offline)
# -------------------------------
data = {
"Sky": ["Sunny", "Sunny", "Rainy", "Sunny"],
"AirTemp": ["Warm", "Warm", "Cold", "Warm"],
"Humidity": ["Normal", "High", "High", "High"],
"Wind": ["Strong", "Strong", "Strong", "Weak"],
"Water": ["Warm", "Warm", "Warm", "Warm"],
"Forecast": ["Same", "Same", "Change", "Same"],
"EnjoySport": ["Yes", "Yes", "No", "Yes"]
}

df = [Link](data)
print("Training Data:\n", df)

# -------------------------------
# Step 2: Separate concepts & target
# -------------------------------
concepts = [Link][:, :-1].values
target = [Link][:, -1].values

# -------------------------------
# Step 3: Initialize hypothesis
# -------------------------------
hypothesis = concepts[0].copy()

# -------------------------------
# Step 4: Find-S Algorithm
Dept. of CSE, JIT-Bangalore Page 17
Machine Learning Laboratory- BCSL606

# -------------------------------
for i, val in enumerate(target):
if val == "Yes":
for j in range(len(hypothesis)):
if hypothesis[j] != concepts[i][j]:
hypothesis[j] = "?"

# -------------------------------
# Step 5: Output
# -------------------------------
print("\nFinal hypothesis:")
print(hypothesis)

OUTPUT

Dept. of CSE, JIT-Bangalore Page 18


Machine Learning Laboratory- BCSL606

EXPERIMENT 5

5. Develop a program to implement k-Nearest Neighbour algorithm to classify the


randomly generated 100 values of x in the range of [0,1]. Perform the following
based on dataset generated.
Label the first 50 points {x1,……,x50} as follows: if (xi ≤ 0.5), then xi Class1, else
xi Class1 Classify the remaining points, x51,……,x100 using KNN. Perform this
for k=1,2,3,4,5,20,30

import numpy as np, [Link] as plt


from collections import Counter

# Data
[Link](0)
x=[Link](100)
train,test=x[:50],x[50:]
lab=["Class1" if i<=0.5 else "Class2" for i in train]

# KNN
def knn(p,k):
i=[Link](abs(train-p))[:k]
return Counter([lab[j] for j in i]).most_common(1)[0][0]

# Run
for k in [1,2,3,4,5,20,30]:
print(f"\nk={k}")
pred=[knn(p,k) for p in test]

# Classification output
for i,(v,c) in enumerate(zip(test,pred),51):
print(f"x{i}={v:.2f} -> {c}")

Dept. of CSE, JIT-Bangalore Page 19


Machine Learning Laboratory- BCSL606

# Visualization
[Link]()
[Link](train,[0]*50,c=['blue' if l=="Class1" else 'red' for l in lab])
[Link](test,[0.05]*50,marker='x',
c=['blue' if l=="Class1" else 'red' for l in pred])
[Link](f"k={k}")
[Link]([]); [Link](0,1)
[Link]()

OUTPUT

k=1
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1

Dept. of CSE, JIT-Bangalore Page 20


Machine Learning Laboratory- BCSL606

x81=0.32 -> Class1


x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

k=2
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2

Dept. of CSE, JIT-Bangalore Page 21


Machine Learning Laboratory- BCSL606

x75=0.74 -> Class2


x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

k=3
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1

Dept. of CSE, JIT-Bangalore Page 22


Machine Learning Laboratory- BCSL606

x69=0.84 -> Class2


x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

k=4
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1

Dept. of CSE, JIT-Bangalore Page 23


Machine Learning Laboratory- BCSL606

x63=0.66 -> Class2


x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

k=5
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1

Dept. of CSE, JIT-Bangalore Page 24


Machine Learning Laboratory- BCSL606

x57=0.65 -> Class2


x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

Dept. of CSE, JIT-Bangalore Page 25


Machine Learning Laboratory- BCSL606

k=20
x51=0.57 -> Class2
x52=0.44 -> Class1
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class1
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class1
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class1
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2
x90=0.93 -> Class2
x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1

Dept. of CSE, JIT-Bangalore Page 26


Machine Learning Laboratory- BCSL606

x96=0.18 -> Class1


x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

k=30
x51=0.57 -> Class2
x52=0.44 -> Class2
x53=0.99 -> Class2
x54=0.10 -> Class1
x55=0.21 -> Class1
x56=0.16 -> Class1
x57=0.65 -> Class2
x58=0.25 -> Class1
x59=0.47 -> Class2
x60=0.24 -> Class1
x61=0.16 -> Class1
x62=0.11 -> Class1
x63=0.66 -> Class2
x64=0.14 -> Class1
x65=0.20 -> Class1
x66=0.37 -> Class1
x67=0.82 -> Class2
x68=0.10 -> Class1
x69=0.84 -> Class2
x70=0.10 -> Class1
x71=0.98 -> Class2
x72=0.47 -> Class2
x73=0.98 -> Class2
x74=0.60 -> Class2
x75=0.74 -> Class2
x76=0.04 -> Class1
x77=0.28 -> Class1
x78=0.12 -> Class1
x79=0.30 -> Class1
x80=0.12 -> Class1
x81=0.32 -> Class1
x82=0.41 -> Class2
x83=0.06 -> Class1
x84=0.69 -> Class2
x85=0.57 -> Class2
x86=0.27 -> Class1
x87=0.52 -> Class2
x88=0.09 -> Class1
x89=0.58 -> Class2

Dept. of CSE, JIT-Bangalore Page 27


Machine Learning Laboratory- BCSL606

x90=0.93 -> Class2


x91=0.32 -> Class1
x92=0.67 -> Class2
x93=0.13 -> Class1
x94=0.72 -> Class2
x95=0.29 -> Class1
x96=0.18 -> Class1
x97=0.59 -> Class2
x98=0.02 -> Class1
x99=0.83 -> Class2
x100=0.00 -> Class1

Dept. of CSE, JIT-Bangalore Page 28


Machine Learning Laboratory- BCSL606

Dept. of CSE, JIT-Bangalore Page 29


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 5

Write a Python program to demonstrate the working of the KNN algorithm using the Iris
dataset loaded from an online source.

import pandas as pd
from [Link] import KNeighborsClassifier
from [Link] import load_iris

try:
# Try loading Iris dataset from internet
url = "[Link]
df = pd.read_csv(url)
print("Online Iris dataset loaded")

X = [Link][:, 0:2].values # sepal length & sepal width


y = [Link][:, -1].values
except:
# Fallback if internet is blocked
print("Internet blocked! Loading offline Iris dataset")
iris = load_iris()
X = [Link][:, :2]
y = [Link]

# Train KNN classifier


knn = KNeighborsClassifier(n_neighbors=5)
[Link](X, y)
# Prediction
print("Prediction:", [Link]([[5.5, 3.0]]))

OUTPUT

Dept. of CSE, JIT-Bangalore Page 30


Machine Learning Laboratory- BCSL606

EXPERIMENT 6

6. Implement the non-parametric Locally Weighted Regression algorithm in order to


fit data points. Select appropriate data set for your experiment and draw graphs
import numpy as np
import [Link] as plt

def lwr(x, X, y, tau):


w = [Link](-[Link]((X - x)**2, axis=1)/(2*tau**2))
theta = [Link](X.T @ [Link](w) @ X) @ X.T @ [Link](w) @ y
return x @ theta

[Link](0)
X = [Link](0, 2*[Link], 50)
y = [Link](X) + 0.1*[Link](50)
Xb, Xt = np.c_[[Link]([Link]), X], np.c_[[Link](100), [Link](0, 2*[Link], 100)]

tau = 0.4
yp = [Link]([lwr(x, Xb, y, tau) for x in Xt])

[Link](X, y, color='red')
[Link](Xt[:,1], yp, color='blue')
[Link]("Locally Weighted Regression")
[Link]()

Dept. of CSE, JIT-Bangalore Page 31


Machine Learning Laboratory- BCSL606

OUTPUT

Dept. of CSE, JIT-Bangalore Page 32


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 6

Implement Robust Linear Regression using RANSAC algorithm and compare it with
ordinary Linear Regression using graphical analysis

import numpy as np

import [Link] as plt

from sklearn.linear_model import LinearRegression, RANSACRegressor

# Step 1: Create dataset

[Link](0)

X = [Link](1, 21).reshape(-1, 1)

y = 3 * [Link]() + 5 + [Link](20)

# Add outliers

y[3] += 25

y[14] -= 30

# Step 2: Normal Linear Regression

lr = LinearRegression()

[Link](X, y)

y_pred_lr = [Link](X)

# Step 3: RANSAC Regression

ransac = RANSACRegressor(LinearRegression())

[Link](X, y)

y_pred_ransac = [Link](X)

Dept. of CSE, JIT-Bangalore Page 33


Machine Learning Laboratory- BCSL606

# Step 4: Plot results

[Link](X, y, label="Data Points")

[Link](X, y_pred_lr, label="Linear Regression")

[Link](X, y_pred_ransac, label="RANSAC Regression")

[Link]("X")

[Link]("y")

[Link]("Robust Linear Regression using RANSAC")

[Link]()

[Link]()

OUTPUT

Dept. of CSE, JIT-Bangalore Page 34


Machine Learning Laboratory- BCSL606

EXPERIMENT 7

7. Develop a program to demonstrate the working of Linear Regression and


Polynomial Regression. Use Boston Housing Dataset for Linear Regression and
Auto MPG Dataset (for vehicle fuel efficiency prediction) for Polynomial
Regression.
import pandas as pd
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 PolynomialFeatures
from [Link] import mean_squared_error

#
# 1) Linear Regression - Boston Housing
#
boston = pd.read_csv("[Link]")
Xb, yb = boston[['rm']], boston['medv']
Xb_train, Xb_test, yb_train, yb_test = train_test_split(Xb, yb, test_size=0.2,
random_state=42)

lr = LinearRegression()
[Link](Xb_train, yb_train)
yb_pred = [Link](Xb_test)

[Link](Xb_test, yb_test, color='blue')


[Link](Xb_test, yb_pred, color='red')
[Link]("Linear Regression - Boston")
[Link]()

Dept. of CSE, JIT-Bangalore Page 35


Machine Learning Laboratory- BCSL606

#
# 2) Polynomial Regression - Auto MPG
#
auto = pd.read_csv("[Link]").replace('?', [Link])
auto['horsepower'] = auto['horsepower'].astype(float)
auto = [Link]()
Xa, ya = auto[['horsepower']].values, auto['mpg'].values
Xa_train, Xa_test, ya_train, ya_test = train_test_split(Xa, ya, test_size=0.2,
random_state=42)

poly = PolynomialFeatures(degree=2)
Xa_train_poly, Xa_test_poly = poly.fit_transform(Xa_train), [Link](Xa_test)

poly_model = LinearRegression()
poly_model.fit(Xa_train_poly, ya_train)
ya_pred = poly_model.predict(Xa_test_poly)

[Link](Xa_test, ya_test, color='blue')


[Link](Xa_test, ya_pred, color='red', alpha=0.6)
[Link]("Polynomial Regression - Auto MPG")
[Link]()

#
# RMSE
#
print("LR RMSE:", [Link](mean_squared_error(yb_test, yb_pred)))
print("Poly RMSE:", [Link](mean_squared_error(ya_test, ya_pred)))

Dept. of CSE, JIT-Bangalore Page 36


Machine Learning Laboratory- BCSL606

OUTPUT

LR RMSE: 6.792994578778734
Poly RMSE: 4.858761912005639

Dept. of CSE, JIT-Bangalore Page 37


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 7

Develop a program to analyze the effect of polynomial degree on regression performance


using cross-validation and mean squared error

import numpy as np

import [Link] as plt

from [Link] import PolynomialFeatures

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import cross_val_score

from [Link] import mean_squared_error

# Step 1: Generate dataset

[Link](42)

X = [Link](0, 10, 50).reshape(-1, 1)

y = 3 * X**2 + 2 * X + 5 + [Link](50, 1) * 10

y = [Link]()

# Step 2: Test polynomial degrees

degrees = [1, 2, 3, 4, 5]

mse_list = []

for d in degrees:

poly = PolynomialFeatures(degree=d)

X_poly = poly.fit_transform(X)

model = LinearRegression()

scores = -cross_val_score(

Dept. of CSE, JIT-Bangalore Page 38


Machine Learning Laboratory- BCSL606

model, X_poly, y,

scoring='neg_mean_squared_error',

cv=5

mse_list.append([Link]())

# Step 3: Plot Degree vs Error

[Link](degrees, mse_list, marker='o')

[Link]("Polynomial Degree")

[Link]("Mean Squared Error")

[Link]("Polynomial Degree Selection using Cross Validation")

[Link]()

OUTPUT

Dept. of CSE, JIT-Bangalore Page 39


Machine Learning Laboratory- BCSL606

EXPERIMENT 8

8. Develop a program to demonstrate the working of the decision tree algorithm. Use
Breast Cancer Data set for building the decision tree and apply this knowledge to
classify a new sample.

# Importing necessary libraries


import numpy as np
import [Link] as plt

from [Link] import load_breast_cancer


from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score
from sklearn import tree

# Load dataset (offline – built into sklearn)


data = load_breast_cancer()
X = [Link]
y = [Link]

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

# Train Decision Tree


clf = DecisionTreeClassifier(random_state=42)
[Link](X_train, y_train)

Dept. of CSE, JIT-Bangalore Page 40


Machine Learning Laboratory- BCSL606

# Test the model


y_pred = [Link](X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")

# Classify a new sample


new_sample = [Link]([X_test[0]])
prediction = [Link](new_sample)

prediction_class = "Benign" if prediction[0] == 1 else "Malignant"


print(f"Predicted Class for the new sample: {prediction_class}")

# Visualize Decision Tree


[Link](figsize=(12, 8))
tree.plot_tree(
clf,
filled=True,
feature_names=data.feature_names,
class_names=data.target_names
)
[Link]("Decision Tree - Breast Cancer Dataset")
[Link]()

OUTPUT

Model Accuracy: 94.74%


Predicted Class for the new sample: Benign

Dept. of CSE, JIT-Bangalore Page 41


Machine Learning Laboratory- BCSL606

Dept. of CSE, JIT-Bangalore Page 42


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 8

Develop a program to demonstrate Decision Tree pruning by controlling tree depth and
compare model accuracy

from [Link] import load_breast_cancer


from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score
import [Link] as plt

# Step 1: Load dataset


data = load_breast_cancer()
X = [Link]
y = [Link]

# Step 2: Train-test split


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

# Step 3: Train trees with different depths


depths = [1, 2, 3, 4, 5, 6]
accuracies = []

for d in depths:
model = DecisionTreeClassifier(max_depth=d, criterion='entropy')
[Link](X_train, y_train)
y_pred = [Link](X_test)
acc = accuracy_score(y_test, y_pred)
[Link](acc)

# Step 4: Display accuracy


for d, acc in zip(depths, accuracies):
print(f"Max Depth: {d} -> Accuracy: {acc}")

Dept. of CSE, JIT-Bangalore Page 43


Machine Learning Laboratory- BCSL606

# Step 5: Plot Depth vs Accuracy


[Link](depths, accuracies, marker='o')
[Link]("Tree Depth")
[Link]("Accuracy")
[Link]("Decision Tree Pruning using Max Depth")
[Link]()

OUTPUT

Max Depth: 1 -> Accuracy: 0.8947368421052632


Max Depth: 2 -> Accuracy: 0.9122807017543859
Max Depth: 3 -> Accuracy: 0.956140350877193
Max Depth: 4 -> Accuracy: 0.956140350877193
Max Depth: 5 -> Accuracy: 0.956140350877193
Max Depth: 6 -> Accuracy: 0.956140350877193

Dept. of CSE, JIT-Bangalore Page 44


Machine Learning Laboratory- BCSL606

EXPERIMENT 9

9. Develop a program to implement the Naive Bayesian classifier considering


Olivetti Face Data set for training. Compute the accuracy of the classifier,
considering a few test data sets.

For Downloading Datasets

from [Link] import fetch_olivetti_faces


import numpy as np

# Download dataset
data = fetch_olivetti_faces(shuffle=True, random_state=42)
X = [Link]
y = [Link]

# Save locally for offline use


[Link]('olivetti_faces.npz', X=X, y=y)
print("Dataset saved as olivetti_faces.npz")

// Dataset saved as olivetti_faces.npz

Dept. of CSE, JIT-Bangalore Page 45


Machine Learning Laboratory- BCSL606

import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, classification_report

data = [Link]('olivetti_faces.npz')
X, y = data['X'], data['y']

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


gnb = GaussianNB().fit(X_train, y_train)
y_pred = [Link](X_test)

print(f'Accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%')


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

fig, axes = [Link](3,5, figsize=(12,8))


for ax, img, label, pred in zip([Link](), X_test, y_test, y_pred):
[Link]([Link](64,64), cmap='gray'); ax.set_title(f"T:{label},P:{pred}");
[Link]('off')
plt.tight_layout(); [Link]()

OUTPUT
Accuracy: 80.83%

Classification Report:
precision recall f1-score support

0 0.67 1.00 0.80 2


1 1.00 1.00 1.00 2
2 0.33 0.67 0.44 3

Dept. of CSE, JIT-Bangalore Page 46


Machine Learning Laboratory- BCSL606

3 1.00 0.00 0.00 5


4 1.00 0.50 0.67 4
5 1.00 1.00 1.00 2
7 1.00 0.75 0.86 4
8 1.00 0.67 0.80 3
9 1.00 0.75 0.86 4
10 1.00 1.00 1.00 3
11 1.00 1.00 1.00 1
12 0.40 1.00 0.57 4
13 1.00 0.80 0.89 5
14 1.00 0.40 0.57 5
15 0.67 1.00 0.80 2
16 1.00 0.67 0.80 3
17 1.00 1.00 1.00 3
18 1.00 1.00 1.00 3
19 0.67 1.00 0.80 2
20 1.00 1.00 1.00 3
21 1.00 0.67 0.80 3
22 1.00 0.60 0.75 5
23 1.00 0.75 0.86 4
24 1.00 1.00 1.00 3
25 1.00 0.75 0.86 4
26 1.00 1.00 1.00 2
27 1.00 1.00 1.00 5
28 0.50 1.00 0.67 2
29 1.00 1.00 1.00 2
30 1.00 1.00 1.00 2
31 1.00 0.75 0.86 4
32 1.00 1.00 1.00 2
34 0.25 1.00 0.40 1
35 1.00 1.00 1.00 5

Dept. of CSE, JIT-Bangalore Page 47


Machine Learning Laboratory- BCSL606

36 1.00 1.00 1.00 3


37 1.00 1.00 1.00 1
38 1.00 0.75 0.86 4
39 0.50 1.00 0.67 5

accuracy 0.81 120


macro avg 0.89 0.85 0.83 120
weighted avg 0.91 0.81 0.81 120

Dept. of CSE, JIT-Bangalore Page 48


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 9

Develop a small program to predict whether to play outside or not using the Naive Bayes
classifier

from sklearn.naive_bayes import GaussianNB


import numpy as np

# Training data
X = [Link]([
[1, 0], # Sunny, Cold
[1, 1], # Sunny, Hot
[0, 1], # Rainy, Hot
[0, 0] # Rainy, Cold
])

y = [Link]([0, 0, 1, 1]) # 0 = No Play, 1 = Play

# Train model
model = GaussianNB()
[Link](X, y)

# Test new data: Sunny & Hot


test = [Link]([[1, 1]])
result = [Link](test)

if result[0] == 1:
print("Play Outside")
else:
print("Do Not Play")

OUTPUT

Do Not Play

Dept. of CSE, JIT-Bangalore Page 49


Machine Learning Laboratory- BCSL606

EXPERIMENT 10

10. Develop a program to implement k-means clustering using Wisconsin Breast Cancer
data set and visualize the clustering result.

import pandas as pd
import [Link] as plt
import seaborn as sns
from [Link] import load_breast_cancer
from [Link] import StandardScaler
from [Link] import KMeans
from [Link] import PCA
from [Link] import confusion_matrix, classification_report

# Load and scale data


data = load_breast_cancer()
X, y = [Link], [Link]
X_scaled = StandardScaler().fit_transform(X)

# K-Means clustering
y_kmeans = KMeans(n_clusters=2, random_state=42).fit_predict(X_scaled)

# Evaluation
print("Confusion Matrix:\n", confusion_matrix(y, y_kmeans))
print("\nClassification Report:\n", classification_report(y, y_kmeans))

# PCA for plotting


X_pca = PCA(n_components=2).fit_transform(X_scaled)
df = [Link](X_pca, columns=['PC1','PC2'])
df['Cluster'], df['True Label'] = y_kmeans, y

Dept. of CSE, JIT-Bangalore Page 50


Machine Learning Laboratory- BCSL606

# Function to plot scatter


def plot_scatter(hue, title, palette):
[Link](figsize=(8,6))
[Link](data=df, x='PC1', y='PC2', hue=hue, palette=palette,
s=100, edgecolor='black', alpha=0.7)
if hue == 'Cluster':
centers = PCA(n_components=2).fit(X_scaled).transform(KMeans(n_clusters=2,
random_state=42).fit(X_scaled).cluster_centers_)
[Link](centers[:,0], centers[:,1], s=200, c='red', marker='X', label='Centroids')
[Link](title)
[Link]('PC1')
[Link]('PC2')
[Link](title=hue)
[Link]()

# Plots
plot_scatter('Cluster', 'K-Means Clustering of Breast Cancer', 'Set1')
plot_scatter('True Label', 'True Labels of Breast Cancer', 'coolwarm')
plot_scatter('Cluster', 'K-Means Clustering with Centroids', 'Set1')

Dept. of CSE, JIT-Bangalore Page 51


Machine Learning Laboratory- BCSL606

OUTPUT

Confusion Matrix:
[[175 37]
[ 13 344]]

Classification Report:
precision recall f1-score support

0 0.93 0.83 0.88 212


1 0.90 0.96 0.93 357

accuracy 0.91 569


macro avg 0.92 0.89 0.90 569
weighted avg 0.91 0.91 0.91 569

Dept. of CSE, JIT-Bangalore Page 52


Machine Learning Laboratory- BCSL606

Dept. of CSE, JIT-Bangalore Page 53


Machine Learning Laboratory- BCSL606

ADDITIONAL PROGRAM 10

Group customers into clusters using the K-Means clustering algorithm.

from [Link] import KMeans


import numpy as np

# Sample customer data (Age, Annual Income)


X = [Link]([
[20, 15000],
[22, 18000],
[25, 20000],
[40, 60000],
[42, 65000],
[45, 70000]
])

# Apply K-Means
kmeans = KMeans(n_clusters=2, random_state=42)
[Link](X)

# Print cluster labels


print("Cluster Labels:", kmeans.labels_)

OUTPUT

Cluster Labels: [0 0 0 1 1 1]

Dept. of CSE, JIT-Bangalore Page 54


Machine Learning Laboratory- BCSL606

SAMPLE PROGRAMS

1. Program to transpose a matrix using a nested loop

X = [ [12, 7], [4, 5], [3, 8]]


# Initialize result matrix with dimensions [columns of X] x [rows of X]
result = [[0 for _ in range(len(X))] for _ in range(len(X[0]))]

# Transpose
for i in range(len(X)): # iterate over rows of X
for j in range(len(X[0])): # iterate over columns of X
result[j][i] = X[i][j]

# Print result
for r in result:
print(r)

OUTPUT
[12, 4, 3]
[7, 5, 8]

Dept. of CSE, JIT-Bangalore Page 55


Machine Learning Laboratory- BCSL606

2. Program to perform different set operations like in mathematics.

# Define two sets


E = {0, 2, 4, 6, 8}
N = {1, 2, 3, 4, 5}

# Set union
print("Union of E and N is", E | N)

# Set intersection
print("Intersection of E and N is", E & N)

# Set difference
print("Difference of E and N is", E - N)

# Set symmetric difference


print("Symmetric difference of E and N is", E ^ N)

OUTPUT:
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {0, 8, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

Dept. of CSE, JIT-Bangalore Page 56


Machine Learning Laboratory- BCSL606

3. Program to count the number of each vowels


# string of vowels
vowels = 'aeiou'

ip_str = 'Hello, have you tried our tutorial section yet?'

# make it suitable for caseless comparisons


ip_str = ip_str.casefold()

# make a dictionary with each vowel as key and 0 as initial value


count = {}.fromkeys(vowels, 0)

# count the vowels


for char in ip_str:
if char in count:
count[char] += 1

print(count)

OUTPUT
{'a': 2, 'e': 5, 'i': 3, 'o': 5, 'u': 3}

Dept. of CSE, JIT-Bangalore Page 57


Machine Learning Laboratory- BCSL606

4. Write a Python program to perform Sentiment Analysis using Naive Bayes


classifier
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from [Link] import accuracy_score

# Sample dataset
reviews = ["I love this product", "This is terrible", "Absolutely fantastic",
"Worst purchase ever", "Very happy with it", "I hate it"]
labels = [1, 0, 1, 0, 1, 0] # 1=positive, 0=negative

# Vectorize text
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(reviews)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.33,
random_state=42)

# Train Naive Bayes


model = MultinomialNB()
[Link](X_train, y_train)

# Predict and evaluate


y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

OUTPUT
Accuracy: 0.5

Dept. of CSE, JIT-Bangalore Page 58


Machine Learning Laboratory- BCSL606

5. Write a program to Predict Stock Price Direction with Logistic Regression


import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score

# Sample stock data: Yesterday's change vs today's up(1)/down(0)


data = {'Change': [0.5, -0.3, 0.2, -0.1, 0.4, -0.5, 0.1, -0.2],
'Up': [1,0,1,0,1,0,1,0]}
df = [Link](data)

X = df[['Change']]
y = df['Up']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,


random_state=42)
model = LogisticRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)

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

OUTPUT

Predictions: [1 1]
Accuracy: 0.0

Dept. of CSE, JIT-Bangalore Page 59


Machine Learning Laboratory- BCSL606

VIVA QUESTIONS

1. What is Machine Learning? How is it different from traditional programming?


Answer:
Machine Learning (ML) is a branch of AI where machines learn patterns from data and make
predictions or decisions without being explicitly programmed.
Difference: Traditional programming: Input + Program → Output
ML: Input + Output (Data) → Program/Model is learned

2. Name the main types of Machine Learning.


Answer:
1. Supervised Learning – Data with labels (e.g., Linear Regression, KNN)
2. Unsupervised Learning – Data without labels (e.g., K-Means, PCA)
3. Reinforcement Learning – Learning by trial and error using rewards

3. What is overfitting and underfitting?


Answer:
• Overfitting: Model learns training data too well, performs poorly on new data.
• Underfitting: Model is too simple, cannot capture patterns in the data.
• Solution: Regularization, more data, feature selection, cross-validation

4. What is feature scaling? Why is it important?


Answer:
Feature scaling standardizes data (e.g., using StandardScaler) so that all features have the same
scale.
• Important for algorithms like KNN, K-Means, SVM, which are distance-based.

5. What is PCA?
Answer:
Principal Component Analysis reduces the dimensionality of data while preserving most

Dept. of CSE, JIT-Bangalore Page 60


Machine Learning Laboratory- BCSL606

variance.
• Converts correlated features into principal components.
• Useful for visualization and reducing computation.

6. What is the difference between classification and regression?


Answer:
• Classification: Predicts discrete labels (e.g., spam/ham, cancer/no cancer)
• Regression: Predicts continuous values (e.g., house price, MPG)

7. What is K-Nearest Neighbors (KNN)?


Answer:
KNN is a supervised algorithm that classifies a data point based on majority class of its k nearest
neighbors.
Distance metric: Euclidean, Manhattan, etc.

8. How does K-Means clustering work?


Answer:
1. Choose number of clusters (k)
2. Initialize centroids randomly
3. Assign points to nearest centroid
4. Update centroid as mean of points in cluster
5. Repeat until convergence

9. What is Naive Bayes?


Answer:
A probabilistic classifier based on Bayes theorem with the assumption that features are
independent
Commonly used in text classification (spam detection, sentiment analysis)

Dept. of CSE, JIT-Bangalore Page 61


Machine Learning Laboratory- BCSL606

10. Explain the difference between fit(), predict(), and fit_predict().


Answer:
• fit(): Trains the model on the data
• predict(): Predicts output for new data
• fit_predict(): Combines fitting and predicting (used in clustering)

11. What are centroids in K-Means clustering?


Answer:
Centroid is the mean position of all points in a cluster. It represents the cluster center.

12. What is Multinomial Naive Bayes used for?


Answer:
• Mainly for text classification with discrete features (word counts, term frequencies).
• Example: Spam detection, Sentiment analysis

13. How do you handle missing values in a dataset?


Answer:
• Remove rows with missing values (dropna())
• Fill missing values (fillna())
• Use mean/median/mode for numerical data or most frequent for categorical data

Dept. of CSE, JIT-Bangalore Page 62

You might also like