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

Aml Lab Manual

Uploaded by

vasradinesh32
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)
18 views40 pages

Aml Lab Manual

Uploaded by

vasradinesh32
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
You are on page 1/ 40

A Laboratory Manual for

Applied Machine Learning


(3171617)

B.E. Semester 7th


(Information Technology)

Directorate of Technical Education


Gandhinagar, Gujarat
L.D. College of Engineering, Ahmedabad

Certificate

This is to certify that Mr. GONDALIYA MAHEK


PRAVINBHAI Enrollment No. 220280116030 of B.E. Semester
7th Information Technology of this Institute (GTU Code: 028) has
satisfactorily completed the Practical / Tutorial work for the
subject Applied Machine Learning (3171617) for the academic
year 2025-26.

Place:

Date:

Name and Sign of Faculty member Head of the Department


Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 1 : Statistical measures such as Mean, Median and Mode of the


data
Objectives:

1. To understand basic statistical properties of data like mean, median and mode

2. To learn about effect of outliers on these properties

Implementation:

1. Write a program to compute mean, median and mode for given data using preferred programming
language.
 Code:-
import matplotlib.pyplot as plt

data = [10, 15, 8, 10, 20, 15, 10, 25, 30]

n = len(data)
mean_value = sum(data) / n

sorted_data = sorted(data) if
n % 2 == 0:
median_value = (sorted_data[n//2 - 1] + sorted_data[n//2]) / 2
else:
median_value = sorted_data[n//2]

frequency = {}
for num in data:
frequency[num] = frequency.get(num, 0) + 1
max_freq = max(frequency.values())
mode_value = [k for k, v in frequency.items() if v == max_freq][0]

plt.hist(data, bins=6, color="skyblue", edgecolor="black")

plt.axvline(mean_value, color='red', linestyle='dashed', linewidth=2, label=f'Mean


= {mean_value:.2f}')
plt.axvline(median_value, color='green', linestyle='dashed', linewidth=2,
label=f'Median = {median_value}')
plt.axvline(mode_value, color='blue', linestyle='dashed', linewidth=2,
label=f'Mode = {mode_value}')
1
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

plt.legend()
plt.title("Histogram with Mean, Median, Mode")
plt.xlabel("Data values") plt.ylabel("Frequency")
plt.show()

 Output:
Data: [10, 15, 8, 10, 20, 15, 10, 25, 30]
Mean: 15.88888888888889
Median: 15
Mode: 10

2. Plot the histogram for the data and show mean, median and mode in the histogram.

Related Questions:

1. What is the difference between mean and median?

 Mean: The average of all values in a dataset, calculated by dividing the sum of values by the
number of values.

2
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

 Median: The middle value when the data is arranged in ascending or descending order.

 Mean is affected by extreme values (outliers), while Median is not.

2. Which measure of central tendency is preferred when the data set has extreme values?

 When the dataset contains extreme values (outliers), the Median is preferred because it is not
influenced by unusually high or low values.

 Mean can get “pulled” in the direction of outliers, giving a misleading picture of the data’s
center.

Suggested Reference:

● https://www.techtarget.com/searchdatacenter/definition/statistical-mean-median-mode-and-range

● https://www.statisticshowto.com/probability-and-statistics/statistics-definitions/mean-median-mode/

● https://www.twinkl.co.in/teaching-wiki/mean-median-mode-and-range

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

3
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No: 2 Probability Distributions


Objectives:

1. To understand different probability distributions such as the normal distribution, Poisson


distribution, or Bernoulli distribution

2. To learn about model a dataset as per requirements.

Implementation:

1. Implement probability distributions such as the normal distribution, Poisson distribution, or


Bernoulli distribution using Python.
Code:-
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm, poisson, bernoulli

# Normal Distribution
normal_data = np.random.normal(loc=0, scale=1, size=1000)
plt.hist(normal_data, bins=30, density=True, alpha=0.6, color='skyblue',
edgecolor="black")
x = np.linspace(-4, 4, 1000)
plt.plot(x, norm.pdf(x, 0, 1), 'r', linewidth=2, label="Normal PDF") plt.title("Normal
Distribution")
plt.legend()
plt.show()

# Poisson Distribution
poisson_data = np.random.poisson(lam=5, size=1000) plt.hist(poisson_data,
bins=15, density=True, alpha=0.6, color='lightgreen', edgecolor="black")
x = np.arange(0, max(poisson_data))
plt.plot(x, poisson.pmf(x, 5), 'r', linewidth=2, label="Poisson PMF") plt.title("Poisson
Distribution (λ=5)")
plt.legend()
plt.show()

# Bernoulli Distribution
bernoulli_data = bernoulli.rvs(p=0.5, size=1000)
plt.hist(bernoulli_data, bins=2, density=True, alpha=0.6, color='orange',

4
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

edgecolor="black")
plt.xticks([0,1])
plt.title("Bernoulli Distribution (p=0.5)")
plt.show()

2. Generate random samples from the distributions and visualize the results using Matplotlib.

5
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Related Questions:

1. What is a Random Variable?


A Random Variable is a variable whose values depend on the outcome of a random experiment.
 It assigns numerical values to each possible outcome.
 Two types:
o Discrete Random Variable: Takes countable values (e.g., number of heads in 3 coin
tosses).
o Continuous Random Variable: Takes infinitely many values within an interval (e.g.,
height, temperature).

2. What is the difference between normal distribution, Poisson distribution, and Bernoulli distribution?
Distribution Type Definition Example Shape
Symmetrical, bell-shaped distribution
Normal Human height, exam
Continuous defined by mean (μ) and standard Bell curve
Distribution scores
deviation (σ).
Probability of a given number of events Skewed,
Poisson Number of calls at a
Discrete occurring in a fixed interval of time or depends on
Distribution call center in 1 hour
space, with rate λ. λ
Coin toss
Bernoulli Probability of a binary outcome Two bars at
Discrete (Head/Tail), yes/no
Distribution (success/failure) with probability p. 0 and 1
events

6
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Suggested Reference:
● https://www.healthknowledge.org.uk/public-health-textbook/research-methods/1b-statistical-methods/
statistical-distributions
● https://www.researchoptimus.com/article/normal-binomial-poisson-distribution.php
● http://www.eagri.org/eagri50/STAM101/pdf/lec07.pdf

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

7
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 3: Implement Bayes Theorem model


Objectives:

1. To understand the working of Bayes theorem.

2. To implement and test Bayes theorem model.

Implementation:

1. For a medical testing company, your company has developed a new test for a disease that affects 1
in every 1,000 people. The test is very accurate, with a false positive rate of only 1% (meaning that
it correctly identifies 99% of people who don't have the disease) and a false negative rate of 5%
(meaning that it correctly identifies 95% of people who do have the disease).

 Prevalence(PriorProbability):
1
𝑃(𝐷) = = 0.001(probability of having the disease)
1000
𝑃(¬𝐷) = 0.999(probability of not having the disease)
 Test Accuracy:
o False Positive Rate = 1% → 𝑃(Test Positive ∣ ¬𝐷) = 0.01
o True Negative Rate = 99% → 𝑃(Test Negative ∣ ¬𝐷) = 0.99
o False Negative Rate = 5% → 𝑃(Test Negative ∣ 𝐷) = 0.05
o True Positive Rate = 95% → 𝑃(Test Positive ∣ 𝐷) = 0.95

(a) Probability that a randomly selected person tests positive


𝑃(Positive) = 𝑃(Positive ∣ 𝐷)𝑃(𝐷) + 𝑃(Positive ∣ ¬𝐷)𝑃(¬𝐷)
= (0.95)(0.001) + (0.01)(0.999)
= 0.00095 + 0.00999 = 0.01094 (≈ 1.094%)

(b) Probability that a person actually has the disease given they test positive
(Bayes’ theorem)
𝑃(Positive ∣ 𝐷)𝑃(𝐷)
𝑃(𝐷 ∣ Positive) =
𝑃(Positive)
0.95 × 0.001
=
0.01094
0.00095
= ≈ 0.0868 (≈ 8.7%)
0.01094

2. A patient comes to your company for testing, and the test comes back positive. What is the
probability that the patient actually has the disease using bayes rules?
 Step 1: Probability of testing positive (law of total probability)
 𝑃(Positive) = 𝑃(Positive ∣ 𝐷)𝑃(𝐷) + 𝑃(Positive ∣ ¬𝐷)𝑃(¬𝐷)
𝑃(Positive) = (0.95)(0.001) + (0.01)(0.999)
𝑃(Positive) = 0.00095 + 0.00999 = 0.01094

8
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

 Step 2: Bayes’ Theorem


P(Positive∣D)P(D)
 𝑃(𝐷 ∣ Positive) = 
P(Positive)
0.95 × 0.001
𝑃(𝐷 ∣ Positive) =
0.01094
0.00095
𝑃(𝐷 ∣ Positive) = ≈ 0.0868
0.01094

 ⬛Step 3: Interpretation
Even though the test is highly accurate, because the disease is very rare, the probability that a
patient actually has the disease given a positive test is only about:
 8.7%

 This is a classic example of how rare diseases and false positives can lead to counterintuitive
results in medical testing.

Related Questions:

1. What is Bayes theorem?


 Definition:
Bayes’ Theorem is a formula that allows you to update the probability of an event based on new
evidence. In other words, it helps you find the probability of a cause given an observed effect.
 Formula:
P(B∣A)⋅P(A)
 𝑃(𝐴 ∣ 𝐵) = 
P(B)

 Where:
 𝑃(𝐴 ∣ 𝐵)= Probability of event A occurring given event B has occurred (posterior probability)
 𝑃(𝐵 ∣ 𝐴)= Probability of event B occurring given event A has occurred (likelihood)
 𝑃(𝐴)= Probability of event A occurring (prior probability)
 𝑃(𝐵)= Probability of event B occurring (marginal probability)
 Example:
If a test for a disease is positive, Bayes’ theorem helps calculate how likely it is that the person
actually has the disease, taking into account the test accuracy and disease prevalence.

2. What is conditional probability ?


 Definition:
Bayes’ Theorem is a formula that allows you to update the probability of an event based on new
evidence. In other words, it helps you find the probability of a cause given an observed effect.
 Formula:
P(B∣A)⋅P(A)
 𝑃(𝐴 ∣ 𝐵) = 
P(B)

 Where:
 𝑃(𝐴 ∣ 𝐵)= Probability of event A occurring given event B has occurred (posterior probability)
 𝑃(𝐵 ∣ 𝐴)= Probability of event B occurring given event A has occurred (likelihood)
 𝑃(𝐴)= Probability of event A occurring (prior probability)

9
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

 𝑃(𝐵)= Probability of event B occurring (marginal probability)


 Example:
If a test for a disease is positive, Bayes’ theorem helps calculate how likely it is that the person
actually has the disease, taking into account the test accuracy and disease prevalence.

Suggested Reference:

● https://www.mathsisfun.com/data/bayes-theorem.html

● https://plato.stanford.edu/entries/bayes-theorem/

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

10
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 4: Linear Regression Model

Objectives:

1. To understand how linear regression works

2. To learn the applications of linear regression

3. To implement and test the linear regression model

Implementation:

1. Implement a simple linear regression model.

2. Train the model using a given dataset.

3. Evaluate the performance of the model using the mean squared error and R-squared metrics.

Code:-

# Import required libraries


import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Step 1: Create a sample dataset


# Let's say we are predicting y from x
np.random.seed(0)
X = 2 * np.random.rand(100, 1) # 100 samples, 1 feature
y = 4 + 3 * X + np.random.randn(100, 1) # y = 4 + 3x + noise

# Step 2: Split 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)

# Step 3: Create a Linear Regression model


model = LinearRegression()

# Step 4: Train the model


model.fit(X_train, y_train)

# Step 5: Make predictions


y_pred = model.predict(X_test)
11
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

# Step 6: Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("Model Coefficient (slope):", model.coef_[0][0])


print("Model Intercept:", model.intercept_[0])
print("Mean Squared Error (MSE):", mse)
print("R-squared (R²):", r2)

# Optional: Visualize the results


plt.scatter(X_test, y_test, color='blue', label='Actual')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Simple Linear Regression')
plt.legend()
plt.show()

OUTPUT:-
Model Coefficient (slope): 3.02
Model Intercept: 3.89
Mean Squared Error (MSE): 0.87
R-squared (R²): 0.98

12
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Related Questions:

1. What is mean squared error and R-squared metrics mean?

1. Mean Squared Error (MSE) and R-squared (R²)


a) Mean Squared Error (MSE):
 MSE measures the average squared difference between the actual values and the predicted
values from a model.
 Formula:
n
1
MSE = Σ (𝑦i − 𝑦^ i ) 2
𝑛
i=1

Where:
 𝑦i= actual value
 𝑦^ i = predicted value
 𝑛= number of data points
Interpretation:
 Smaller MSE → better model performance (predictions are closer to actual values).
 Squaring emphasizes larger errors.

b) R-squared (R²):
 R² measures the proportion of variance in the dependent variable that is explained by the
independent variable(s).
 Formula:
∑(𝑦i − 𝑦^ i ) 2
𝑅2 = 1 −
∑(𝑦i − 𝑦¯ )2

Where:
 𝑦¯ = mean of actual values
Interpretation:
 R² = 1 → perfect fit
 R² = 0 → model explains none of the variance
 R² < 0 → model is worse than using the mean as prediction

2. What is multivariate regression?

Definition:
 Multivariate regression is a type of regression where more than one independent variable is used
to predict a dependent variable.
 It’s an extension of simple linear regression (which uses only one independent variable).
General formula:
𝑦 = 𝑏0 + 𝑏1𝑥1 + 𝑏2𝑥2 + ⋯ + 𝑏n𝑥n + 𝜖

Where:
 𝑦= dependent variable
 𝑥1, 𝑥2, . . . , 𝑥n= independent variables
 𝑏0= intercept, 𝑏1, 𝑏2, . . . , 𝑏n= coefficients
 𝜖= error term
Example:

13
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Predicting house price based on:


 𝑥1= size of the house
 𝑥2= number of bedrooms
 𝑥3= age of the house
All these features are used together in multivariate regression to predict the house price.

Suggested Reference:

● https://medium.datadriveninvestor.com/regression-in-machine-learning-296caae933ec

● https://www.analyticsvidhya.com/blog/2021/06/linear-regression-in-machine-learning/

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

14
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 5: Logistics Regression Model


Objectives:

1. To understand how logistic regression works

2. To learn the applications of logistic regression

3. To implement and test the logistic regression model

Implementation:

1. Implement a logistic regression model using the scikit-learn library.

from sklearn.linear_model import LogisticRegression

# Create a logistic regression model


model = LogisticRegression()

2. Train the model using a binary classification dataset of your choice.

from sklearn.datasets import load_breast_cancer


from sklearn.model_selection import train_test_split

# Load the dataset


data = load_breast_cancer()
X = data.data # Features
y = data.target # Labels (0 or 1)

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

# Train the logistic regression model


model.fit(X_train, y_train)

3. Evaluate the performance of the model using metrics such as accuracy, precision, recall, and
F1-score.

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Make predictions
y_pred = model.predict(X_test)

# Calculate metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

15
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)

Accuracy: 0.9649

Precision: 0.96

Recall: 0.98

F1-score: 0.97

Related Questions:

1. What are some common performance metrics used to evaluate the accuracy of a logistic regression
model, and how do you interpret them?

When evaluating a logistic regression model, especially for classification problems, the following
metrics are commonly used:

Metric Definition Interpretation


High accuracy → model predicts most labels
Accuracy Proportion of total correct predictions. correctly, but may be misleading if classes are
imbalanced.
Proportion of predicted positives that are
actually positive. Formula: Precision = High precision → few false positives; useful when
Precision
TP false positives are costly (e.g., spam detection).
TP+FP

Recall Proportion of actual positives that are High recall → few false negatives; useful when
TP
(Sensitivity) correctly predicted. Formula: Recall =TP+FN missing a positive is costly (e.g., disease detection).
Balances precision and recall; useful when classes are
F1-score Harmonic mean of precision and recall.
imbalanced.
16
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Metric Definition Interpretation


Precision⋅Recall
Formula: 𝐹1 = 2 ⋅ Precision+Recall
Area under the Receiver Operating Measures model’s ability to distinguish between
ROC-AUC
Characteristic curve. classes; closer to 1 → better performance.

2. What is the difference between a binary logistic regression and a multinomial logistic regression?
How do you choose which one to use for your data?

Feature Binary Logistic Regression Multinomial Logistic Regression


Target Two classes (0/1, Yes/No,
More than two classes (3, 4, … categories)
variable Positive/Negative)
Output Probability of one class (usually class 1) Probabilities for each class, sum = 1
𝑝 Generalized version: 𝑃(𝑌 = 𝑘) =
logit(𝑝) = ln
Equation 1−𝑝 eb0k+b1kx1+⋯+bnkxn
= 𝑏0 + 𝑏1𝑥1 + ⋯ + 𝑏n𝑥n ∑j eb0j+b1jx1+⋯+bnjxn
When to use Use when predicting two outcomes Use when predicting three or more outcomes

How to choose:

 If your target variable has two categories, use binary logistic regression.
 If your target variable has more than two categories, use multinomial logistic regression (or
“softmax regression”).

17
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Suggested Reference:

● https://medium.datadriveninvestor.com/regression-in-machine-learning-296caae933ec

● https://www.kdnuggets.com/2022/07/logistic-regression-work.html

● https://machinelearningmastery.com/logistic-regression-for-machine-learning/

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

18
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 6: K-NN classifier


Objectives:

1. To understand how KNN works.

2. To split the dataset in Train/Test.

3. To learn to train classifier

Implementation:

1. Implement a KNN classifier using the scikit-learn library.

from sklearn.neighbors import KNeighborsClassifier

# Create KNN classifier with k=5 neighbors


knn_model = KNeighborsClassifier(n_neighbors=5)

2. Train the model using a binary classification dataset of your choice.

from sklearn.datasets import load_breast_cancer


from sklearn.model_selection import train_test_split

# Load dataset
data = load_breast_cancer()
X = data.data # Features
y = data.target # Labels (0 or 1)

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

# Train the KNN model


knn_model.fit(X_train, y_train)

3. Evaluate the performance of the model using metrics such as accuracy, precision, recall, and
F1-score.

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

# Make predictions
y_pred = knn_model.predict(X_test)

# Calculate performance metrics


accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

19
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)

OUTPUT:-
Accuracy: 0.9561
Precision: 0.9706
Recall: 0.9706
F1-score: 0.9706

Related Questions:

1. What are some common performance metrics used to evaluate the accuracy of a KNN classifier,
and how do you interpret them?

Metric Definition Interpretation


High accuracy → model predicts
Proportion of correctly classified samples: (
most labels correctly; may be
Accuracy \frac{\text{Correct Predictions}}{\text{Total
misleading if classes are
Predictions}} )
imbalanced.
High precision → few false
Of all predicted positives, how many were actually
Precision positives; useful when false
positive: ( \frac{TP}{TP+FP} )
positives are costly.
High recall → few false negatives;
Recall Of all actual positives, how many were correctly
useful when missing a positive is
(Sensitivity) predicted: ( \frac{TP}{TP+FN} )
costly.
Harmonic mean of precision and recall: ( F1 = 2 \cdot
Balances precision and recall;
F1-score \frac{\text{Precision} \cdot
useful for imbalanced datasets.
\text{Recall}}{\text{Precision} + \text{Recall}} )
Confusion Helps visualize model performance
Table showing TP, TN, FP, FN counts
Matrix for each class.

2. How do you choose the value of K in KNN?

 K = number of nearest neighbors considered in prediction.

 Small K (e.g., 1-3):

o Can capture fine details but may overfit (sensitive to noise).

 Large K (e.g., 10+):

o Reduces noise sensitivity, smoother decision boundary, but may underfit.

20
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

 Typical approach:

1. Use cross-validation to test multiple K values.

2. Choose K that gives highest validation accuracy or best balance between bias and variance.

Rule of thumb: Often 𝐾 = √𝑛where n = number of samples.

3. What are the pros and cons of using KNN for classification tasks?

Pros:
 Simple and intuitive, easy to implement.
 Non-parametric → no assumption about data distribution.
 Can handle multi-class classification naturally.
 Flexible with different distance metrics (Euclidean, Manhattan, etc.).
Cons:
 Computationally expensive for large datasets → must compute distance for all points at prediction
time.
 Sensitive to irrelevant features and feature scaling → normalization is important.
 Performance decreases with high-dimensional data (curse of dimensionality).
 Sensitive to noisy data and outliers.

Suggested Reference:

● https://www.ibm.com/topics/knn

● https://www.scaler.com/topics/machine-learning/knn-algorithm-in-machine-learning/

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

21
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 7: Clustering Model


Objectives:

1. To understand the clustering model.

2. To learn about L-means clustering algorithms.

Implementation:

1. Implement k-means clustering using scikit-learn.

from sklearn.cluster import KMeans

# Create a KMeans model with 3 clusters


kmeans_model = KMeans(n_clusters=3, random_state=42)

2. Apply k-means to a dataset of your choice.

from sklearn.datasets import make_blobs

# Create a synthetic dataset with 300 samples and 2 features


X, y_true = make_blobs(n_samples=300, centers=3, cluster_std=0.60, random_state=42)

# Fit KMeans to the dataset


kmeans_model.fit(X)

# Get cluster labels


labels = kmeans_model.labels_

# Get cluster centroids


centroids = kmeans_model.cluster_centers_

3. Visualize the results using Matplotlib.


import matplotlib.pyplot as plt

# Plot data points colored by cluster


plt.scatter(X[:, 0], X[:, 1], c=labels, s=50, cmap='viridis', label='Data points')

# Plot cluster centroids


plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=200, marker='X', label='Centroids')

plt.title('K-Means Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()

22
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Related Questions:

1. What is the difference between classification and clustering algorithms?

Feature Classification Clustering


Assigns predefined labels to new Groups similar data points into clusters
Definition
data based on training data. without predefined labels.
Supervised learning (requires
Supervised/Unsupervised Unsupervised learning (no labels required).
labeled data).
Discover natural groupings or patterns in
Goal Predict the class of new samples.
data.
Logistic Regression, KNN, K-Means, Hierarchical Clustering,
Example Algorithms
Decision Tree, SVM DBSCAN
23
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Feature Classification Clustering


Discrete class labels (e.g., Cluster IDs or groupings (e.g., Cluster 1,
Output
Spam/Not Spam). Cluster 2, …).
Accuracy, Precision, Recall, F1- Silhouette score, Davies-Bouldin index,
Evaluation
score, ROC-AUC Within-Cluster Sum of Squares (WCSS)

2. What are the different clustering algorithms used in real-time applications?

Algorithm Description Real-Time Applications


Customer segmentation, image
Partitions data into k clusters based on
K-Means compression, document
distance from centroids.
clustering
Hierarchical Builds a tree of clusters (dendrogram), Gene expression analysis, social
Clustering can be agglomerative or divisive. network analysis
Groups points based on density; can Fraud detection, anomaly
DBSCAN (Density-
find arbitrarily shaped clusters and detection, spatial data
Based)
outliers. analysis
Iteratively moves points towards the Object tracking, image
Mean Shift
highest density region. segmentation
Gaussian Mixture Assumes data is generated from a Speech recognition, finance risk
Model (GMM) mixture of Gaussian distributions. modeling

24
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Suggested Reference:

● https://www.geeksforgeeks.org/clustering-in-machine-learning/

● https://developers.google.com/machine-learning/clustering/overview

● https://serokell.io/blog/k-means-clustering-in-machine-learning

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

25
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 8: Neural Network


Objectives:

1. To understand the basic difference between biological neural network and artificial neural network

2. To lean the mathematical structure of artificial neural network

3. To implement neural network

Implementation:

1. Implement a simple neural network on a dataset of your choice.

# Import required libraries


import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam

# Load dataset
data = load_iris()
X = data.data # Features
y = data.target.reshape(-1, 1) # Labels

# One-hot encode the target variable


encoder = OneHotEncoder(sparse=False)
y_encoded = encoder.fit_transform(y)

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

# Split into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.2,
random_state=42)

# Create a simple neural network


model = Sequential()
model.add(Dense(10, input_dim=4, activation='relu')) # Input layer + hidden layer
model.add(Dense(8, activation='relu')) # Hidden layer
model.add(Dense(3, activation='softmax')) # Output layer (3 classes)

# Compile the model


model.compile(optimizer=Adam(learning_rate=0.01), loss='categorical_crossentropy',
metrics=['accuracy'])

2. Train the model and evaluate its performance using metrics such as accuracy and loss.

26
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

# Train the model


history = model.fit(X_train, y_train, validation_split=0.2, epochs=50, batch_size=8,
verbose=1)

# Evaluate the model on the test set


loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)

OUTPUT:-
 Epoch 1/50
 12/12 [==============================] - 1s 15ms/step - loss: 1.0591 -
accuracy: 0.4048 - val_loss: 1.0312 - val_accuracy: 0.4167
 Epoch 2/50
 12/12 [==============================] - 0s 3ms/step - loss: 0.9782 -
accuracy: 0.5833 - val_loss: 0.9673 - val_accuracy: 0.5417
 Epoch 3/50
 12/12 [==============================] - 0s 3ms/step - loss: 0.9221 -
accuracy: 0.6786 - val_loss: 0.9120 - val_accuracy: 0.6250
 ...
 Epoch 50/50
 12/12 [==============================] - 0s 3ms/step - loss: 0.0198 -
accuracy: 0.9881 - val_loss: 0.0753 - val_accuracy: 0.9583

Related Questions:

1. What are the different types of neural networks and their applications?
Type Description Applications
Simple classification and regression
Feedforward Neural Data flows in one direction (input →
tasks, e.g., predicting house
Network (FNN) hidden → output).
prices.
Image recognition, object detection,
Convolutional Neural Uses convolutional layers to capture
facial recognition, medical
Network (CNN) spatial features.
imaging.
Recurrent Neural Processes sequential data, has memory of Time-series prediction, language
Network (RNN) previous inputs. modeling, speech recognition.
A type of RNN that solves vanishing
Long Short-Term Text generation, stock price
gradient problem, remembers long-
Memory (LSTM) prediction, language translation.
term dependencies.
Learns efficient data representation by Data compression, anomaly
Autoencoder
encoding and decoding. detection, denoising images.
Generative Adversarial Consists of generator and discriminator Image generation, deepfake
Network (GAN) networks that compete. creation, art generation.
Radial Basis Function Uses radial basis functions as activation in Function approximation, control
(RBF) Network hidden layer. systems, pattern recognition.

27
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

2. What is regularization in neural networks and why is it important?


Definition:
Regularization is a technique used to prevent overfitting by adding constraints or penalties to the
model.
Common Methods:
1. L1 Regularization (Lasso): Adds the sum of absolute weights to the loss function. Encourages
sparsity.
2. L2 Regularization (Ridge): Adds the sum of squared weights to the loss function. Encourages
smaller weights.
3. Dropout: Randomly drops neurons during training to prevent co-adaptation.
4. Early Stopping: Stops training when validation loss stops improving.
Importance:
 Prevents overfitting → improves generalization to unseen data.
 Helps neural networks perform better on real-world datasets.

3. What are some common activation functions used in neural networks?


Activation
Formula Use Case
Function
Sigmoid ( \sigma(x) = \frac{1}{1+e^{-x}} ) Binary classification, output layer.
( \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-
Tanh Hidden layers, outputs in [-1,1].
x}} )
Hidden layers in deep networks, avoids
ReLU ( f(x) = \max(0,x) )
vanishing gradient.
Variant of ReLU to fix “dead neurons”
Leaky ReLU ( f(x) = x ) if x>0 else 0.01*x
problem.
( \text{softmax}(x_i) =
Softmax Multi-class classification output layer.
\frac{e^{x_i}}{\sum_j e^{x_j}} )

4. What are some real-world applications of neural networks?


 Computer Vision: Image classification, object detection, facial recognition.
 Natural Language Processing (NLP): Chatbots, language translation, sentiment analysis.
 Healthcare: Disease prediction, medical imaging analysis, drug discovery.
 Finance: Stock price prediction, fraud detection, credit scoring.
 Autonomous Vehicles: Self-driving car navigation and object detection.
 Gaming & AI: Game bots, strategy optimization.
 Speech Recognition: Voice assistants like Alexa, Siri, Google Assistant.
 Recommendation Systems: Netflix, Amazon, YouTube recommendations.

Suggested Reference:

● https://www.analyticsvidhya.com/blog/2022/01/introduction-to-neural-networks/

● https://www.simplilearn.com/tutorials/deep-learning-tutorial/what-is-neural-network

28
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

29
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 9: Deep Neural Network


Objectives:

1. To understand the basic difference between neural network and Deep neural network

2. To understand various activation functions.

3. To implement a deep neural network on a real-time dataset.

Implementation:

1. Design a deep neural network architecture using a framework such as TensorFlow or Keras.

# Import required libraries


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load MNIST dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Preprocess data
X_train = X_train.reshape(X_train.shape[0], -1).astype('float32') / 255
X_test = X_test.reshape(X_test.shape[0], -1).astype('float32') / 255

# One-hot encode labels


y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

2. Choose the number of layers, number of neurons per layer, activation functions, and regularization
techniques.
 Input layer: 784 neurons (28x28 pixels flattened)
 Hidden layers: 3 layers with 512, 256, and 128 neurons
 Activation function: ReLU for hidden layers
 Dropout: 0.2 for regularization
 Output layer: 10 neurons (softmax for multi-class classification)
# Build the deep neural network
model = Sequential()
model.add(Dense(512, input_dim=784, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
30
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

model.add(Dense(10, activation='softmax')) # Output layer

# Compile the model


model.compile(optimizer=Adam(learning_rate=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])

3. Train the deep neural network on the given dataset.


# Train the model
history = model.fit(X_train, y_train,
validation_split=0.2,
epochs=20,
batch_size=128,
verbose=1)

4. Evaluate its performance using metrics such as accuracy and loss.


# Evaluate on test set
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)

OUTPUT:-
 Epoch 1/20
 375/375 [==============================] - 6s 15ms/step - loss: 0.3754 - accuracy:
0.8901 - val_loss: 0.1642 - val_accuracy: 0.9518
 Epoch 2/20
 375/375 [==============================] - 5s 14ms/step - loss: 0.1510 - accuracy:
0.9557 - val_loss: 0.1213 - val_accuracy: 0.9637
 Epoch 3/20
 375/375 [==============================] - 5s 14ms/step - loss: 0.1085 - accuracy:
0.9671 - val_loss: 0.1028 - val_accuracy: 0.9692
 ...
 Epoch 20/20
 375/375 [==============================] - 5s 14ms/step - loss: 0.0094 - accuracy:
0.9971 - val_loss: 0.0712 - val_accuracy: 0.9813

Test Loss: 0.065


Test Accuracy: 0.981

Related Questions:

1. What are the different types of architectures of Deep Neural Network?


Architecture Description Common Applications
Feedforward Neural Data flows in one direction from input to Classification, regression, simple
Network (FNN) output. No cycles. pattern recognition
Convolutional Neural Uses convolutional layers to capture spatial Image classification, object
Network (CNN) patterns. detection, video analysis

31
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Architecture Description Common Applications


Recurrent Neural Designed for sequential data. Has loops to Time-series prediction, speech
Network (RNN) maintain memory of previous inputs. recognition, NLP
A type of RNN that remembers long-term
Long Short-Term Text generation, language
dependencies and solves vanishing gradient
Memory (LSTM) translation, stock price prediction
problem.
Gated Recurrent Unit Similar applications as LSTM
Simplified LSTM with fewer parameters.
(GRU) but faster training
Learns compressed representation of input Data compression, anomaly
Autoencoder
data via encoder-decoder layers. detection, denoising images
Generative Adversarial Consists of Generator and Discriminator Image generation, deepfakes, art
Network (GAN) networks competing with each other. creation
Residual Network Uses skip connections to prevent vanishing Image classification, object
(ResNet) gradients in very deep networks. detection, deep CNN applications
Uses attention mechanism instead of NLP tasks: translation,
Transformer Networks
recurrence. summarization, chatbots

2. What is the learning rate and batch size?


a) Learning Rate (𝜂)
 Learning rate controls how much the model weights are updated during training for each step.
 If learning rate is too high, the model may overshoot the minimum and fail to converge.
 If learning rate is too low, training becomes very slow and may get stuck in local minima.
 Example: learning_rate=0.001 in Adam optimizer.
b) Batch Size
 Batch size is the number of samples processed before updating the model weights.
 Types:
o Stochastic Gradient Descent (SGD): batch size = 1
o Mini-batch Gradient Descent: batch size = 32, 64, 128 (most common)
o Batch Gradient Descent: batch size = total number of training samples
Effect of Batch Size:
 Small batch size: Noisy updates, slower but may generalize better.
 Large batch size: Faster training, but may generalize poorly.
In short:
 Learning rate → how fast the model learns
 Batch size → how many samples are used per weight update

32
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Suggested Reference:

● https://www.kdnuggets.com/2020/02/deep-neural-networks.html

● https://www.simplilearn.com/tutorials/deep-learning-tutorial/what-is-deep-learning

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

33
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

Experiment No 10: Convolutional Neural Network


Objectives:

1. To understand the basic difference between neural network and Deep neural network

2. To understand various activation functions.

3. To implement a deep neural network on a real-time dataset.

Implementation:

1. Design a convolutional neural network architecture


# Import required libraries
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

2. Choose the number of convolutional layers, pooling layers, fully connected layers, and activation
functions.
 Convolutional Layers: 2 layers with filters 32 and 64, kernel size 3x3
 Pooling Layers: MaxPooling 2x2 after each convolution
 Fully Connected Layers: 128 neurons + output layer 10 neurons
 Activation Functions: ReLU for hidden layers, Softmax for output
 Dropout: 0.2 for regularization

3. Train the deep neural network on the given dataset. ( i.e. example cat vs dog, MNIST etc).

# Load MNIST dataset

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape to include channel dimension (grayscale)

X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255

X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255

# One-hot encode labels

y_train = to_categorical(y_train, 10)

y_test = to_categorical(y_test, 10)

34
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

4. Evaluate the trained model's performance using metrics such as accuracy, precision, recall, and
F1-score.
# Create CNN model
model = Sequential()

# Convolutional layer 1 + pooling


model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2,2)))

# Convolutional layer 2 + pooling


model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

# Flatten and fully connected layers


model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax')) # Output layer

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Output :-
 Training (example for 10 epochs)
Epoch 1/10
375/375 [==============================] - 12s 31ms/step - loss: 0.1892 - accuracy: 0.9442 -
val_loss: 0.0563 - val_accuracy: 0.9820
...
Epoch 10/10
375/375 [==============================] - 11s 30ms/step - loss: 0.0115 - accuracy: 0.9967 -
val_loss: 0.0283 - val_accuracy: 0.9910
 Loss decreases quickly
 Accuracy increases to ~99%
● Test Set Evaluation
Test Loss: 0.026
Test Accuracy: 0.991
● Classification Report (example)
precision recall f1-score support

0 0.9912 0.9945 0.9928 980


1 0.9930 0.9910 0.9920 1135
2 0.9895 0.9867 0.9881 1032
3 0.9885 0.9885 0.9885 1010
4 0.9917 0.9888 0.9902 982
35
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

5 0.9876 0.9886 0.9881 892


6 0.9913 0.9925 0.9919 958
7 0.9906 0.9861 0.9884 1028
8 0.9853 0.9877 0.9865 974
9 0.9885 0.9877 0.9881 1009

accuracy 0.9910 10000


macro avg 0.9910 0.9910 0.9910 10000
weighted avg 0.9910 0.9910 0.9910 10000

Related Questions:

1. What is the convolutional layer?

A convolutional layer is the core building block of a Convolutional Neural Network (CNN). It is
designed to extract features from input data, such as images, by applying filters (kernels) that detect
patterns like edges, textures, or shapes.

Key points:
Feature Description
Filter/Kernel A small matrix (e.g., 3×3 or 5×5) that slides over the input image to compute dot products.
Feature Map The output of the convolution operation showing where a feature appears in the image.
Stride How many pixels the filter moves at each step. Stride >1 reduces output size.
Padding Adding extra pixels around the input to control output size (valid or same padding).
Activation Non-linear function (like ReLU) applied to the feature map to introduce non-linearity.
Example:
If you have a 28×28 grayscale image and apply a 3×3 filter with ReLU activation, the convolutional layer
will produce a new feature map highlighting certain patterns (edges, corners, textures) in the image.
Purpose:
 Automatically learn important features from raw input data.
 Reduce the need for manual feature extraction.

2. What is the learning rate and batch size used in CNN?

a) Learning Rate:
 Controls how much the network weights are updated during each training step.
 Too high → model may overshoot minima → fail to converge.
 Too low → training is slow → may get stuck in local minima.
 Example: learning_rate=0.001 in Adam optimizer (commonly used).
b) Batch Size:
 Number of samples processed before the model updates its weights.
 Types:
o Mini-batch Gradient Descent: batch size = 32, 64, 128 (most common in CNNs)
o Stochastic Gradient Descent (SGD): batch size = 1
o Batch Gradient Descent: batch size = total number of training samples
Effect of Batch Size:
36
Mahek Gondaliya Applied Machine Learning (3171617) 220280116030

 Small batch size: Noisy updates, slower, better generalization.


 Large batch size: Faster training, may overfit or generalize poorly.
In CNN training:
 Typical learning rate: 0.001 (Adam)
 Typical batch size: 32 to 128
Summary:
 Learning rate → speed of learning
 Batch size → number of samples per weight update

Suggested Reference:

● https://www.analyticsvidhya.com/blog/2021/05/convolutional-neural-networks-cnn/

● https://www.simplilearn.com/tutorials/deep-learning-tutorial/convolutional-neural-network

Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score

Implementation Implementation is Implementation is Implementation is Implementation is


not presented. complete. It does not complete. It does not complete. It meets all
compile and run. meet all the the specifications and
specifications and does works for all test data.
not work for all test
data.

Results Results are not Partially corrected The results are correct, The results are correct.
included. results. but not in the proper Well structured,
format.

Conclusion No conclusion is In-Correct conclusion Partially correct Conclusion meets the


present. is presented. conclusion. requirements of the
experiment.

References No references are Incorrect references are Related references are Related references are
included by the included. included but are not included and well
students. well structured. structured.

Question Not able to answer Answers are partially Answers are correct. No Answers are correct.
Answers a single question. correct. in-depth understating. Having in-depth
knowledge of concepts.

37

You might also like