Applied Machine Learning Lab Manual
Applied Machine Learning Lab Manual
B.E. Semester 7
(Information Technology)
Certificate
Date:
2
Applied Machine Learning (3171617)
Preface
Main motto of any laboratory/practical/field work is for enhancing required skills as well
as creating ability amongst students to solve real time problem by developing relevant
competencies in psychomotor domain. By keeping in view, GTU has designed competency
focused outcome-based curriculum for engineering degree programs where sufficient
weightage is given to practical work. It shows importance of enhancement of skills amongst
the students and it pays attention to utilize every second of time allotted for practical
amongst students, instructors and faculty members to achieve relevant outcomes by
performing the experiments rather than having merely study type experiments. It is must
for effective implementation of competency focused outcome-based curriculum that every
practical is keenly designed to serve as a tool to develop and enhance relevant competency
required by the various industry among every student. These psychomotor skills are very
difficult to develop through traditional chalk and board content delivery method in the
classroom. Accordingly, this lab manual is designed to focus on the industry defined
relevant outcomes, rather than old practice of conducting practical to prove concept and
theory.
By using this lab manual students can go through the relevant theory and procedure in
advance before the actual performance which creates an interest and students can have
basic idea prior to performance. This in turn enhances pre-determined outcomes amongst
students. Each experiment in this manual begins with competency, industry relevant skills,
course outcomes as well as practical outcomes (objectives). The students will also achieve
safety and necessary precautions to be taken while performing practical.
This manual also provides guidelines to faculty members to facilitate student centric lab
activities through each experiment by arranging and managing necessary resources in
order that the students follow the procedures with required safety and necessary
precautions to achieve the outcomes. It also gives an idea that how students will beassessed
by providing rubrics.
Engineering Thermodynamics is the fundamental course which deals with various forms
of energy and their conversion from one to the another. It provides a platform for students
to demonstrate first and second laws of thermodynamics, entropy principle and concept
of exergy. Students also learn various gas and vapor power cycles and refrigeration cycle.
Fundamentals of combustion are also learnt.
Utmost care has been taken while preparing this lab manual however always there is
chances of improvement. Therefore, we welcome constructive suggestions forimprovement
and removal of errors if any.
3
Applied Machine Learning (3171617)
Sr. CO CO CO CO CO
Objective(s) of Experiment
No. 1 2 3 4 5
Statistical measures such as Mean, Median and
1. √ √
Mode of the data.
2. Probability Distributions. √ √
6. K-NN classifier √ √
7. Clustering Model √ √
8. Neural Network √ √
4
Applied Machine Learning (3171617)
6
Index
(Progressive Assessment Sheet)
6 K-NN classifier 38
7 Clustering Model 44
8 Neural Network 50
10 Convolutional Neural 64
Network
Total
7
Applied Machine Learning (3171617)
Date:
Relevant CO: 1, 2
Objectives:
1. To understand basic statistical properties of data like mean, median and mode
Implementation:
1. Write a program to compute mean, median and mode for given data using preferred
programming language.
2. Plot the histogram for the data and show mean, median and mode in the histogram.
Implementation 1:
Program:
import statistics
def calculate_mean(data):
return [Link](data)
def calculate_median(data):
return [Link](data)
def calculate_mode(data):
try:
return [Link](data)
except [Link]:
return "No unique mode found"
if __name__ == "__main__":
data = [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9]
mean = calculate_mean(data)
median = calculate_median(data)
mode = calculate_mode(data)
print(f"Data: {data}")
8
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")
Output:
Implementation 2:
Program:
import statistics
import [Link] as plt
def calculate_mean(data):
return [Link](data)
def calculate_median(data):
return [Link](data)
def calculate_mode(data):
try:
return [Link](data)
except [Link]:
return None
data = [1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9]
mean = calculate_mean(data)
median = calculate_median(data)
mode = calculate_mode(data)
[Link]('Value')
[Link]('Frequency')
[Link]()
[Link]('Histogram with Mean, Median, and Mode')
[Link]()
9
Output:
Conclusion:
In practice, the choice of which measure to use depends on the nature of the data and the specific goals
of the analysis. Mean is appropriate for symmetrical, normally distributed data with minimal outliers. Median
is preferred when data is skewed or contains outliers, as it is more robust. Mode is useful for identifying the
most common value or category within a dataset.
In summary, Mean, Median, and Mode are indispensable statistical measures that provide valuable
insights into the central tendencies of data. Careful consideration of the data's characteristics and potential
outliers is essential when choosing the most appropriate measure for a given analysis. These measures, in
combination with other statistical techniques, help researchers and analysts make informed decisions and
draw meaningful conclusions from their data.
Related Questions:
2. Which measure of central tendency is preferred when the data set has extreme values?
➢ When a dataset contains extreme values or outliers, the median is typically preferred as the
measure of central tendency. Here's why:
1. Robustness to Outliers: The median is robust to outliers, meaning that extreme values do not
unduly influence its value. It is resistant to the effects of outliers, as it depends solely on the position
of values within the dataset. Outliers might be far from the median, but they won't affect its value.
2. Reflects the Center of the Data: The median represents the middle value when the dataset is sorted,
making it a better indicator of the center of the data, especially when outliers are present. It divides
the dataset into two equal halves, with 50% of the values falling above and 50% falling below it.
3. Use in Skewed Distributions: In datasets with skewed distributions, where the majority of data
points are concentrated on one side, the mean can be heavily skewed by outliers. The median, on the
other hand, remains a stable representation of the typical value.
4. Common Practice: It is a common practice to use the median in statistical analysis when dealing
with data that may contain outliers. This helps to prevent the distortion of central tendency measures
caused by extreme values.
However, it's important to note that the choice between the mean and median ultimately depends on
the specific goals of the analysis, the nature of the data, and the presence of outliers. In some cases,
you may choose to analyze both the mean and median to gain a comprehensive understanding of the
dataset.
11
Applied Machine Learning (3171617)
Suggested Reference:
• [Link]
mode-and-range
• [Link]
definitions/mean-median-mode/
• [Link]
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
12
Applied Machine Learning (3171617)
Date:
Relevant CO: 1, 2
Objectives:
Implementation:
2. Generate random samples from the distributions and visualize the results using
Matplotlib.
Implementation 1:
Normal Distribution:
Program:
import numpy as np
from [Link] import norm
import [Link] as plt
mean = 0
std_dev = 1
size = 1000
13
[Link]('Normal Distribution')
[Link]()
[Link]()
Output:
Poisson Distribution:
Program:
from [Link] import poisson
print(data)
Output:
14
Bernoulli Distribution:
Program:
import numpy as np
print(data)
Output:
Implementation 2:
Normal Distribution:
Program:
import numpy as np
import [Link] as plt
from [Link] import norm
mean = 0
std_dev = 1
size = 1000
[Link]('Normal Distribution')
[Link]()
15
[Link]()
Output:
Poisson Distribution:
Program:
import numpy as np
import [Link] as plt
from [Link] import poisson
[Link]('Poisson Distribution')
[Link]()
[Link]()
16
Output:
Bernoulli Distribution:
Program:
import numpy as np
import [Link] as plt
[Link]('Bernoulli Distribution')
[Link]()
Output:
17
Implementation 3:
Program:
import numpy as np
import [Link] as plt
from [Link] import norm
Conclusion:
These probability distributions and their respective Python libraries provide essential tools for
modeling, simulating, and analyzing various random processes and events in both scientific and
engineering domains. Understanding the characteristics and appropriate use cases for each distribution is
crucial in conducting experiments and drawing meaningful conclusions from data.
18
Related Questions:
Random variables are used to describe and analyze probabilistic events and phenomena. They
allow us to define probability distributions that specify the likelihood of different outcomes and
enable the calculation of various statistical measures and properties, such as expected values,
variances, and probabilities.
In mathematical notation, random variables are often represented by capital letters (e.g., X, Y) and
are associated with probability functions or probability density functions that describe the
probabilities of different values or ranges of values for the random variable. Random variables are
a foundational concept in statistics and probability and are used in a wide range of applications,
including risk assessment, statistical modeling, and data analysis.
Normal distribution, Poisson distribution, and Bernoulli distribution are all fundamental probability
distributions used to model different types of random variables in statistics and probability theory.
Here are the key differences between these distributions:
2. Poisson Distribution:
- Shape: The Poisson distribution is a discrete probability distribution that models the number of
events that occur within a fixed interval of time or space. It is characterized by a probability mass
function (PMF) that describes the probabilities of different counts.
-Use: The Poisson distribution is used to model rare events or count data, such as the number of
phone calls at a call center in an hour or the number of emails received in a day.
- Parameter: It is defined by a single parameter, λ (lambda), which represents the average rate at
which events occur.
- Notable Properties: The Poisson distribution is memoryless, meaning that the probability of an
event occurring in the future is not influenced by the past.
3. Bernoulli Distribution:
- Shape: The Bernoulli distribution is the simplest discrete probability distribution. It models a
binary outcome, where there are only two possible results (success and failure).
- Use: The Bernoulli distribution is used to model binary events, such as the outcome of a coin flip
(heads or tails), the success or failure of a single trial, or a yes/no outcome.
- Parameter: It is defined by a single parameter, p, which represents the probability of success (e.g.,
the probability of getting a "heads" in a coin toss).
- Notable Properties:The Bernoulli distribution can be considered as a special case of the binomial
distribution when the number of trials is 1. The mean of the Bernoulli distribution is equal to p, and
the variance is p(1-p).
In summary, the main differences between these distributions lie in their shapes, the types of data
they model, and the number of parameters required to define them. The normal distribution is
continuous and used for modeling continuous data, while the Poisson and Bernoulli distributions are
discrete and are used for modeling count and binary data, respectively.
20
Applied Machine Learning (3171617)
Suggested Reference:
• [Link]
statistical-methods/statistical-distributions
• [Link]
• [Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
21
Experiment No 3: Implement Bayes Theorem model
Date:
Relevant CO: 2, 5
Objectives:
Implementation:
For a medical testing company, and 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).
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?
Implementation:
Program:
# Define the probabilities
p_disease = 0.001 # Probability of having the disease
p_no_disease = 1 - p_disease # Probability of not having the disease
p_positive_given_disease = 0.95 # Probability of a positive test result given the disease
p_positive_given_no_disease = 0.01 # Probability of a positive test result given no disease
# Calculate the probability of a positive test result using the law of total probability
p_positive = (p_positive_given_disease * p_disease) + (p_positive_given_no_disease *
p_no_disease)
# Calculate the probability of having the disease given a positive test result using Bayes'
theorem
p_disease_given_positive = (p_positive_given_disease * p_disease) / p_positive
22
Output:
Conclusion:
In conclusion, implementing a Bayes Theorem model is a powerful and versatile approach in the
field of statistics and probability theory. Bayes' theorem allows us to update our beliefs and make
informed decisions in the presence of uncertainty, making it a fundamental tool in various
applications, including machine learning, medical diagnosis, spam filtering, and more.
The model leverages conditional probability to compute the probability of an event occurring
based on prior knowledge and new evidence. By formalizing the relationship between prior beliefs,
likelihood, and posterior probabilities, Bayes' theorem provides a structured framework for
reasoning and inference.
Related Questions:
𝐴 𝐵
𝑝 ( ) = 𝑃 ( ) . 𝑝(𝐴)/𝑃(𝐵)
𝐵 𝐴
Where:
- P(A|B)is the posterior probability of event or hypothesis A given evidence B.
- P(B|A)is the probability of observing evidence B given that event A has occurred. This is called
the likelihood.
- P(A)is the prior probability of event A, which is our initial belief or probability before considering
the evidence.
- P(B) the total probability of observing evidence B.
In essence, Bayes' theorem provides a structured way to update our beliefs (the prior probability)
about an event or hypothesis based on observed evidence. It is particularly valuable in scenarios
with uncertainty and incomplete information.
Bayes' theorem is a foundational concept in probability and statistics, and it provides a principled
way to incorporate new evidence into our understanding of the world and make informed
decisions in the presence of uncertainty.
Conditional probability is known as the possibility of an event or outcome happening, based on the
existence of a previous event or outcome. It is calculated by multiplying the probability of the preceding
event by the renewed probability of the succeeding, or conditional, event.
Here the concept of the independent event and dependent event occurs. Imagine a student who takes
leave from school twice a week, excluding Sunday. If it is known that he will be absent from school on
Tuesday then what are the chances that he will also take a leave on Saturday in the same week? It is
observed that in problems where the occurrence of one event affects the happening of the following event,
these cases of probability are known as conditional probability
. P(A|B) = N(A∩B)/N(B)
Or
P(B|A) = N(A∩B)/N(A)
Suggested Reference:
• [Link]
• [Link]
24
References used by the students:
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
25
Experiment No 4: Linear Regression Model
Date:
Relevant CO: 3, 5
Objectives:
Implementation:
3. Evaluate the performance of the model using the mean squared error and R-squared
metrics.
Implementation 1:
Program:
import numpy as np
import [Link] as plt
# Make predictions
X_new = [Link]([[0], [2]])
X_new_b = np.c_[[Link]((2, 1)), X_new]
y_predict = X_new_b.dot(theta_best)
27
Implementation 2:
Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import [Link] as plt
# Split the dataset into a training set and a testing set (e.g., 80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
28
Output:
Implementation 3:
Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# Split the dataset into a training set and a testing set (e.g., 80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
29
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R-squared (R²) Score: {r2:.2f}")
Output:
Conclusion:
In conclusion, the linear regression model is a fundamental and widely used technique in statistics
and machine learning. It provides a simple yet powerful framework for understanding and modeling the
relationship between variables.
Related Questions:
R-squared (R²):
- Measures how well a model explains the variance in the dependent variable.
- Ranges from 0 to 1, with higher values indicating a better fit to the data.
In summary, MSE assesses prediction accuracy, while R² assesses model fit and how well it
explains the data.
Suggested Reference:
• [Link]
• [Link]
learning/
30
References used by the students:
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
31
Experiment No 5: Logistics Regression Model
Date:
Relevant CO: 3, 5
Objectives:
Implementation:
3. Evaluate the performance of the model using metrics such as accuracy, precision,
recall, and F1-score.
Implementation 1:
Program:
# Import necessary libraries
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, confusion_matrix
import [Link] as plt
Output:
33
Implementation 2:
Program:
# Import necessary libraries
from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, confusion_matrix
Output:
34
Implementation 3:
Program:
from [Link] import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, confusion_matrix
import pandas as pd
X = data[['dbt']]
y = data['data']
# Calculate precision
precision = precision_score(y_test, y_pred)
print("Precision:", precision)
# Calculate recall
recall = recall_score(y_test, y_pred)
print("Recall:", recall)
# Calculate F1-score
f1 = f1_score(y_test, y_pred)
print("F1-Score:", f1)
Output:
Conclusion:
In conclusion, the logistic regression model is a fundamental and widely used technique in the fields of
statistics and machine learning, particularly for binary and multi-class classification tasks.
Related Questions:
35
1. Accuracy: Overall correctness of predictions.
Choose the metric that best aligns with your specific goals and priorities, as each metric
emphasizes different aspects of model performance.
➢ Binary logistic regression is used when you have a binary (two-class) outcome variable,
and it predicts the probability of belonging to one of the two classes. It's suitable for
problems like yes/no or true/false.
Multinomial logistic regression, on the other hand, is used for multinomial classification
where the outcome variable has more than two unordered categories. It predicts the
probabilities of belonging to each of these categories. Use it when dealing with problems
that have multiple, non-ordered classes.
The choice between the two depends on the nature of your outcome variable and the
number of classes you need to predict.
Suggested Reference:
• [Link]
296caae933ec
• [Link]
• [Link]
36
References used by the students:
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
37
Experiment No 6: K-NN classifier
Date:
Relevant CO: 3, 5
Objectives:
Implementation:
3. Evaluate the performance of the model using metrics such as accuracy, precision,
recall, and F1-score.
Implementation 1:
Program:
# Import necessary libraries
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, confusion_matrix
38
# Display the results
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
Output:
Implementation 2:
Program:
# Import necessary libraries
from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, confusion_matrix
import pandas as pd
39
Output:
Implementation 3:
Program:
from [Link] import accuracy_score, precision_score, recall_score, f1_score
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Calculate precision
precision = precision_score(y_test, y_pred)
print("Precision:", precision)
# Calculate recall
recall = recall_score(y_test, y_pred)
print("Recall:", recall)
# Calculate F1-score
f1 = f1_score(y_test, y_pred)
print("F1-Score:", f1)
Output:
Conclusion:
In practice, the k-NN classifier is a valuable tool for tasks that rely on the concept of similarity and
where data points with similar features tend to have similar target values. Its simplicity, flexibility, and
ability to handle both classification and regression tasks make it a versatile choice for various machine
learning applications.
40
Related Questions:
1. What are some common performance metrics used to evaluate the accuracy of
a KNNclassifier, and how do you interpret them?
➢ Common performance metrics for evaluating a K-Nearest Neighbors (KNN) classifier:
6. ROC Curve and AUC-ROC: Trade-off between true positive rate and false positive rate.
Choose the metric that aligns with your classification goals, considering factors like
class balance and the trade-off between false positives and false negatives.
3. Perform grid search to find the best K value along with other hyperparameters.
6. Adjust K for class imbalance, giving more weight to the minority class if needed.
7. Continuously evaluate and fine-tune K for the desired trade-off between bias and variance.
8. Finally, assess the model's performance on a separate test dataset to ensure generalization.
The choice of K should be data-specific and based on rigorous experimentation and validation.
41
3. What are the pros and cons of using KNN for classification tasks?
3. Flexibility: Suitable for various classification tasks, including binary and multiclass problems.
4. Local Patterns: Robust to noisy data and outliers as it focuses on local patterns.
KNN is best suited for smaller, low-dimensional datasets and is sensitive to parameter choices
and data quality.
Suggested Reference:
• [Link]
• [Link]
42
References used by the students:
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
43
Experiment No 7: Clustering Model
Date:
Relevant CO: 3, 5
Objectives:
Implementation:
Implementation 1:
Program:
# Import necessary libraries
import numpy as np
from [Link] import KMeans
import [Link] as plt
44
Output:
Implementation 2:
Program:
# Import necessary libraries
from [Link] import load_iris
from [Link] import KMeans
import [Link] as plt
45
Output:
Implementation 3:
Program:
# Import necessary libraries
from [Link] import load_iris
from [Link] import KMeans
import [Link] as plt
46
Output:
Conclusion:
In conclusion, clustering models are unsupervised machine learning techniques designed to identify
natural groupings or patterns in data.
In practice, clustering models are invaluable for uncovering patterns and structures within data,
aiding in data exploration, segmentation, and decision-making. The choice of clustering algorithm
depends on the characteristics of the data and the specific problem at hand. Evaluating clustering
results and understanding the domain context are essential for successful applications of clustering
models.
Related Questions:
- Clustering is an unsupervised learning technique where the algorithm groups data points into
clusters based on their inherent similarities without prior knowledge of class labels. Its
objective is to discover patterns or groupings within the data.
The key differences include the learning type (supervised vs. unsupervised), the output
(predicted labels vs. data grouping), and the use cases (classification for labeling, clustering for
pattern discovery).
47
2. What are the different clustering algorithms used in real-time applications?
Clustering algorithms used in real-time applications:
3. DBSCAN: Groups data points based on density, suitable for varying shapes and sizes.
5. Mean Shift: Shifts data points toward density peaks for non-uniform data.
9. Fuzzy C-Means: Allows data points to belong to multiple clusters with varying degrees.
The choice depends on data characteristics, the number of clusters, and computational
resources. Each algorithm has specific strengths and use cases.
Suggested Reference:
• [Link]
• [Link]
• [Link]
48
References used by the students:
[Link]
[Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
49
Experiment No 8: Neural Network
Date:
Relevant CO: 3, 5
Objectives:
1. To understand the basic difference between biological neural network and artificial
neural network
Implementation:
2. Train the model and evaluate its performance using metrics such as accuracy and
loss.
Implementation 1:
Program:
# Import necessary libraries
import numpy as np
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import Sequential
from [Link] import Dense
from [Link] import to_categorical
from [Link] import Adam
Output:
Implementation 2:
Program:
# Import necessary libraries
import numpy as np
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import Sequential
from [Link] import Dense
from [Link] import to_categorical
from [Link] import Adam
Output:
Conclusion:
Neural networks have significantly advanced the state of the art in machine learning and have
demonstrated their effectiveness in a wide range of tasks. They continue to be an area of active research
and innovation, pushing the boundaries of what's possible in artificial intelligence and deep learning.
Related Questions:
1. What are the different types of neural networks and their applications?
➢ Different types of neural networks and their applications:
2. Convolutional Neural Network (CNN): Image recognition, object detection, facial recognition.
3. Recurrent Neural Network (RNN): Time series prediction, NLP, speech recognition.
5. Gated Recurrent Unit (GRU): Similar to LSTM, used for NLP and sequences.
52
11. Hopfield Network: Associative memory, optimization problems.
13. Echo State Network (ESN): Time series prediction, speech recognition.
Each type is suited for specific applications, addressing a wide range of tasks in computer vision,
NLP, time series analysis, and more. The choice depends on the problem at hand.
1. Prevents Overfitting: Neural networks can be overly complex, memorizing training data noise.
Regularization constraints model complexity to avoid overfitting.
2. Improves Generalization: It helps models perform well on both training and new, unseen data,
which is essential for practical applications.
4. Reduces Model Complexity: It adds penalties to the loss function, promoting simpler,
interpretable models.
Common regularization techniques include L1 and L2 regularization, dropout, early stopping, data
augmentation, weight decay, and batch normalization. These methods ensure neural networks
generalize effectively and produce reliable results.
1. Sigmoid: Outputs values between 0 and 1, used for binary classification and probability
estimation.
2. Tanh (Hyperbolic Tangent): Outputs values between -1 and 1, useful in hidden layers.
53
3. ReLU (Rectified Linear Unit): Outputs the input for positive values, widely used in hidden layers
due to computational efficiency.
4. Leaky ReLU: A variant of ReLU that allows a small gradient for negative values to prevent "dying
ReLU" problem.
5. PReLU (Parametric ReLU): Leaky ReLU with a learnable slope for negative values.
6. ELU (Exponential Linear Unit): Smooth and addresses the vanishing gradient problem in deep
networks.
7. SELU (Scaled Exponential Linear Unit): Self-normalizing and suitable for deep networks.
9. GRU (Gated Recurrent Unit): Used in recurrent neural networks (RNNs) and LSTM-like
structures.
Selecting the right activation function depends on the specific task, network architecture, and
empirical performance. Experimentation is often necessary to determine the most suitable choice.
1. Computer Vision: Object detection, facial recognition, and medical image analysis.
2. Natural Language Processing (NLP): Machine translation, chatbots, and sentiment analysis.
3. Speech and Audio Processing: Speech recognition, speaker identification, and noise reduction.
8. Anomaly Detection: Network traffic analysis, fraud detection, and quality control.
54
13. Robotics: Robot control and human-robot interaction.
14. Social Media: Trend analysis, sentiment monitoring, and content filtering.
Neural networks play a crucial role in solving complex problems across diverse fields, contributing
to advancements in technology and research.
55
Suggested Reference:
• [Link]
• [Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
56
Experiment No 9: Deep Neural Network
Date:
Relevant CO: 4, 5
Objectives:
1. To understand the basic difference between neural network and Deep neural
network
Implementation:
2. Choose the number of layers, number of neurons per layer, activation functions, and
regularization techniques.
Implementation 1:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
# Flatten layer
[Link](),
57
# Compile the model
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Implementation 2:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
# Convolutional layers
layers.Conv2D(32, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
# Output layer
[Link](10, activation='softmax')
])
58
Output:
Implementation 3:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
from [Link] import mnist
# Output layer
[Link](10, activation='softmax')
])
Implementation 4:
Program:
# Evaluate the model on the test data
test_loss, test_accuracy = [Link](x_test, y_test)
print(f"Test Loss: {test_loss:.4f}")
print(f"Test Accuracy: {test_accuracy:.4f}")
Output:
Conclusion:
Deep neural networks have brought about significant advances in the field of artificial intelligence
and have achieved state-of-the-art performance in various tasks. They continue to push the boundaries
of what can be achieved in machine learning and are at the forefront of technology breakthroughs and
applications across diverse industries.
Related Questions:
7. Autoencoder:
- Learns efficient data representations with encoder and decoder.
- Used for dimensionality reduction and anomaly detection.
11. Transformer:
- Designed for NLP tasks with self-attention mechanisms.
- Efficient for sequence-to-sequence tasks.
Each DNN architecture is suited for specific tasks and data types, showcasing the versatility of
neural networks in solving a wide range of problems. The choice depends on the problem's nature
and requirements.
➢ Learning Rate:
- The learning rate is a hyperparameter that determines the step size at which a neural network's
weights are updated during training.
- It controls how quickly or slowly a neural network converges to a solution. A high learning rate
makes the training process faster but can lead to overshooting the optimal solution. A low learning
rate ensures stability but may require more training epochs.
- It's a crucial parameter to tune to balance training speed and model accuracy. Common learning
rates range from 0.1 to 0.0001.
➢ Batch Size:
- The batch size is a hyperparameter that defines the number of training examples used in each
forward and backward pass during one iteration of model training.
61
- Large batch sizes can speed up training by taking advantage of parallel processing but may
require more memory. Small batch sizes may lead to slower training but can provide a more
accurate estimate of the gradient.
- The choice of batch size depends on available hardware and the dataset. Common batch sizes
range from 32 to 256.
Both learning rate and batch size play critical roles in training neural networks effectively. The
optimal values often depend on the specific problem and the network architecture being used.
They are essential hyperparameters that machine learning practitioners must tune to achieve the
best model performance.
62
Suggested Reference:
• [Link]
• [Link]
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
63
Experiment No 10: Convolutional Neural Network
Date:
Relevant CO: 4, 5
Objectives:
1. To understand the basic difference between neural network and Deep neural
network
Implementation:
2. Choose the number of convolutional layers, pooling layers, fully connected layers,
and activation functions.
3. Train the deep neural network on the given dataset. ( i.e. example cat vs dog, MNIST
etc).
4. Evaluate the trained model's performance using metrics such as accuracy, precision,
recall, and F1-score.
Implementation 1:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
# Flatten layer
[Link](),
64
# Fully connected layers
[Link](120, activation='relu'),
[Link](84, activation='relu'),
# Output layer
[Link](num_classes, activation='softmax') # Adjust 'num_classes' for your
specific problem
])
Output:
Implementation 2:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
# Flatten layer
[Link](),
65
[Link](128, activation='relu'),
# Output layer
[Link](num_classes, activation='softmax') # Adjust 'num_classes' for your
specific problem
])
Output:
Implementation 3:
Program:
# Import necessary libraries
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
from [Link] import cifar10
66
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, (3, 3), activation='relu'),
# Flatten layer
[Link](),
# Output layer
[Link](num_classes, activation='softmax')
])
Output:
Implementation 4:
Program:
from [Link] import accuracy_score, precision_score, recall_score, f1_score
# Assuming you have already trained the model, make predictions on the test data
y_pred = [Link](x_test)
# Calculate metrics
accuracy = accuracy_score(y_true_classes, y_pred_classes)
precision = precision_score(y_true_classes, y_pred_classes, average='weighted')
recall = recall_score(y_true_classes, y_pred_classes, average='weighted')
f1 = f1_score(y_true_classes, y_pred_classes, average='weighted')
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1-Score: {f1:.4f}")
Output:
67
Conclusion:
CNNs have revolutionized the field of computer vision and have set new benchmarks for visual data
analysis tasks. They continue to drive advancements in technology and play a critical role in applications
that rely on the interpretation and understanding of visual content.
Related Questions:
1. Feature Extraction: Convolutional layers apply learnable filters to the input data, enabling the
network to detect features like edges, textures, and patterns.
2. Local Receptive Fields: Each filter has a small region (local receptive field) that it operates on,
and its weights are shared across the entire input. This reduces the number of parameters.
3. Convolution Operation: The filter slides or convolves across the input data, computing dot
products within its local receptive field. This process creates feature maps that highlight specific
patterns.
4. Stride and Padding: Stride determines how much the filter shifts during each slide, and padding
controls the size of the feature map.
6. Pooling: Optional pooling layers may follow to reduce feature map dimensions, making the
network more efficient and less prone to overfitting.
Convolutional layers are essential for capturing hierarchical and spatial features in data, making
CNNs ideal for image recognition, object detection, and more. Stacking multiple convolutional
layers allows the network to learn increasingly complex features.
➢ Learning Rate and Batch Size are two important hyperparameters used in training
convolutional neural networks (CNNs) and other deep learning models.
Learning Rate:
- The learning rate is a hyperparameter that determines the size of steps taken during the
optimization process (e.g., gradient descent) to update the model's weights.
68
- In CNNs, it controls how much the model's parameters are adjusted based on the gradient of the
loss function with respect to those parameters.
- A high learning rate can make training faster but may lead to overshooting the optimal solution,
while a low learning rate can improve convergence but may require longer training times.
- Common learning rates for CNNs typically range from 0.1 to 0.0001, but the optimal value can
vary based on the problem and model architecture.
Batch Size:
- The batch size is a hyperparameter that specifies the number of data samples (e.g., images) used
in each forward and backward pass during a single training iteration.
- In CNN training, data is divided into batches, and gradients are computed for each batch. The
model's weights are updated after processing each batch.
- The choice of batch size affects training speed, memory usage, and the quality of the weight
updates. Smaller batch sizes provide a more accurate estimate of the gradient but can slow down
training due to frequent weight updates. Larger batch sizes can speed up training through parallel
processing but may result in a noisier gradient estimate.
- Typical batch sizes for CNNs range from 32 to 256, but the optimal batch size depends on factors
like available hardware and the dataset size.
Both learning rate and batch size are critical for effective CNN training. Properly tuning these
hyperparameters is often necessary to achieve the best model performance, and the optimal
values can vary from one problem to another.
69
Suggested Reference:
• [Link]
• [Link]
network
Rubric Not Acceptable (0) Below Expectation (1) Considerable (2) Acceptable (3) Score
Results Results are not Partially corrected The results are correct, The results are
included. results. but not in the proper correct. Well
format. structured,
References No references are Incorrect references Related references are Related references are
included by the are included. included but are not included and well
students. well structured. structured.
Question Not able answer a Answers are partially Answers are correct. Answers are correct.
Answers single question. correct. No in-depth Having in-depth
understating. knowledge of concept.
Total
70
The bias term (intercept) in a linear regression model allows the regression line to fit the data more accurately by shifting it vertically along the Y-axis. Without it, the line would be forced to pass through the origin, which could significantly reduce the model’s ability to capture the true relationship between the variables, particularly if the data is not centered around zero .
Mean Squared Error (MSE) measures the average squared difference between predicted and actual values, reflecting the model’s accuracy. R-squared indicates the proportion of variance explained by the model, showing the goodness of fit. Together, they provide insight into both the model’s predictive accuracy and its overall quality .
The implementation for CIFAR-10, which involves more complex and colorful images, typically requires deeper networks with more convolutional and pooling layers to accurately capture detailed features. In contrast, MNIST's simpler grayscale images demand less complexity, often allowing effective results with shallower networks .
Convolutional layers contain filters that perform the convolution operation by sliding over input data. Key components include learnable parameters (filter weights) and a local receptive field that allows for spatial hierarchies of features. This facilitates detection of patterns such as edges and textures, essential for recognizing complex visual structures in data .
Logistic regression is used for binary classification, where the outcome variable has two possible categories (true/false). Multinomial logistic regression is used when the outcome variable has more than two unordered categories, such as predicting types of flowers. The choice depends on the number and nature of categories in the outcome variable .
Batch size affects the stability and speed of the training process, with smaller batches providing noisier but potentially more informative updates, while larger batches offer smoother but less frequent updates. The learning rate influences how quickly weights are adjusted during training. Both need fine-tuning to balance training speed and convergence to an optimal model .
Hyperparameter tuning is crucial as it directly impacts model performance metrics such as accuracy and loss. Proper tuning can significantly enhance model accuracy by optimizing convergence and generalization, while poor tuning can lead to overfitting or underfitting, negatively affecting both accuracy and computational efficiency .
Using a large number of neighbors in a K-NN classifier can lead to over-smoothing, where the model becomes less sensitive to variations in the data and may oversimplify the classification boundary. This can reduce accuracy, especially if the classes are not well-separated, and can increase computational costs .
Dropout layers randomly deactivate a portion of neurons during training, preventing complex co-adaptations, thus helping mitigate overfitting. By forcing the network to rely on distributed representations, dropout enhances model robustness and generalization to unseen data .
Conditional probability is used in scenarios where the occurrence of one event influences the likelihood of another event. Mathematically, it is formulated as P(A|B) = N(A∩B)/N(B), where P(A|B) is the probability of event A occurring given that event B has occurred. This relationship is crucial in fields like finance and machine learning for making informed predictions and decisions .