Machine Learning Lab Practical Report
Machine Learning Lab Practical Report
Submitted by
Prince Verma
25/SWE/24
I Sem, I Year
Submitted to
Dr. Sanjay Patidar
Associate Professor
Department of Software Engineering
S. No. Dataset
Aim:
Using Python libraries perform data cleaning, feature selection, finding null values and analysis
through plotting different graphs between features on Housing dataset(Housing).
Theory:
Data analysis is a systematic approach to examining datasets to extract useful insights and
patterns. In real-world datasets, raw data often contains inconsistencies, missing values,
irrelevant features, or noisy information that can affect the accuracy of any analytical or
predictive modeling. Data cleaning and feature selection are fundamental steps to ensure
high-quality analysis.
○ The process of identifying the most important features (columns) in a dataset that
contribute significantly to the predictive or analytical task.
○ Python libraries like Matplotlib, Seaborn, and Plotly provide tools to create
scatter plots, histograms, boxplots, bar charts, pair plots, etc.
○ Analysis can reveal trends, correlations, outliers, and patterns which can guide
further modeling or decision-making.
By performing data cleaning, null value detection, feature selection, and visualization in Python,
the dataset becomes more reliable, consistent, and interpretable.
● Data Cleaning ensures the removal of inconsistencies and missing data, which prevents
errors during analysis.
● Feature Selection focuses on the most relevant variables, reducing noise and improving
analytical efficiency.
Overall, this approach improves the quality of data-driven decisions and forms the foundation for
further predictive modeling or machine learning tasks. Python’s libraries make this process
efficient and user-friendly, allowing analysts to gain meaningful insights quickly.
Data visualization is a crucial part of data analysis that involves representing data in a visual
format to make patterns, trends, and relationships easier to understand. Python provides powerful
libraries to perform data visualization on datasets efficiently.
● Matplotlib: A fundamental library to create line plots, bar charts, scatter plots,
histograms, and more.
1. Line Plot: Used to show trends over time or continuous variables.
7. Pair Plot: Provides a matrix of scatter plots to observe pairwise relationships between
features.
4. Application on Datasets
● By applying these visualization techniques on two given datasets, we can compare and
contrast patterns.
● It also helps in detecting any inconsistencies, missing values, or outliers that may require
further data preprocessing.
IMPLEMENTATION:
Data visualization is an essential step in the data analysis workflow. By using Python libraries
like Matplotlib, Seaborn, and Plotly:
● We can easily explore and understand datasets, revealing hidden patterns and
relationships.
Overall, data visualization transforms raw data into a clear, understandable, and actionable
form, enabling analysts and stakeholders to make informed decisions.
Theory:
Linear Regression
Linear Regression is a fundamental statistical and machine learning technique used to model the
relationship between a dependent variable and one or more independent variables. The goal is to
find a linear equation that best predicts the dependent variable based on the values of the
independent variables.
Simple Linear Regression models the relationship between a single independent variable
(feature) and a dependent variable (target) by fitting a linear equation to the observed data.
● Equation: y = β₀ + β₁*x + ε
○ y: Dependent variable (Target)
○ x: Independent variable (Feature)
○ β₀: y-intercept (Bias term)
○ β₁: Slope (Coefficient of the feature x)
○ ε: Random error term
● Objective: To find the values of β₀ and β₁ that minimize the difference between the actual
observed values and the values predicted by the model.
Multiple Linear Regression extends SLR by modeling the relationship between two or more
independent variables and a single dependent variable.
Evaluation Metrics
1. R-squared (R²): Represents the proportion of the variance in the dependent variable that
is predictable from the independent variables. A value closer to 1 indicates a better fit.
2. Mean Squared Error (MSE): The average of the squared differences between predicted
and actual values. Lower values are better.
3. Root Mean Squared Error (RMSE): The square root of MSE. It is in the same units as the
target variable, making it more interpretable.
4. Mean Absolute Error (MAE): The average of the absolute differences between predicted
and actual values. It is less sensitive to outliers than MSE/RMSE.
Methodology
Data Preprocessing
1. Data Loading: Import the dataset (used_cars.csv and [Link]) into a Pandas
DataFrame.
2. Data Cleaning:
○ Handle missing values using appropriate methods (e.g., mean/median imputation,
or dropping rows/columns).
○ Identify and treat outliers if necessary, as they can significantly skew the
regression line.
3. Exploratory Data Analysis (EDA):
○ Use [Link]() and [Link]() to understand the data structure.
○ Create visualizations like scatter plots (for SLR) and correlation heatmaps (for
MLR) to understand relationships between variables.
4. Feature Encoding: Convert categorical variables (e.g., car brand, fuel type, location) into
numerical format using techniques like One-Hot Encoding or Label Encoding.
Implementation:
Both simple and multiple linear regression models were implemented on Used Cars and Housing
datasets. The models were evaluated using R² Score and Mean Squared Error. Results show that
multiple regression captures more relationships between variables, generally leading to better
performance.
Theory
Logistic Regression is a statistical and machine learning algorithm used for binary classification
problems. Unlike linear regression which predicts a continuous value, logistic regression predicts
the probability that a given instance belongs to a particular category.
The core of the model is the logistic function (also called the sigmoid function), which maps any
real-valued number into a value between 0 and 1. This output is interpreted as a probability.
The model works by first calculating a weighted sum of the input features (similar to linear
regression). This output is then passed through the sigmoid function. A threshold (typically 0.5)
is applied to this probability to make the final class prediction. For example, if the predicted
probability is >= 0.5, the instance is classified as the positive class (e.g., "Churn"), otherwise as
the negative class (e.g., "No Churn").
The model is trained by optimizing its coefficients (weights) to minimize a cost function,
typically log loss, which penalizes wrong predictions based on how confident and incorrect they
were.
Methodology
Data Preprocessing: The dataset will first be loaded and inspected for missing values and
inconsistencies. Missing values will be handled through imputation or removal. Categorical
variables (e.g., gender, internet service type) will be converted into a numerical format using
techniques like one-hot encoding.
Feature Scaling: Numerical features with different scales (e.g., tenure, monthly charges) will be
standardized or normalized. This ensures that no single feature dominates the model's learning
process due to its scale.
Feature-Target Split: The dataset will be divided into a matrix of features (independent variables)
and a vector for the target variable (dependent variable), which is 'Churn'.
Train-Test Split: The data will be split into a training set and a testing set. The training set is used
to train the logistic regression model, and the testing set is reserved to evaluate its performance
on unseen data.
Model Training: The Logistic Regression algorithm will be implemented on the training data.
This process involves finding the optimal coefficients that minimize the log loss function.
Prediction and Performance Analysis: The trained model will be used to make predictions on the
test set. Performance will be evaluated using several metrics:
Prince Verma (25/SWE/24) 26
● Confusion Matrix: To visualize True Positives, True Negatives, False Positives, and False
Negatives.
● Accuracy: The overall proportion of correct predictions.
● Precision: The proportion of positive predictions that were actually correct.
● Recall (Sensitivity): The proportion of actual positives that were correctly identified.
● F1-Score: The harmonic mean of Precision and Recall, providing a single balanced
metric.
● ROC Curve and AUC: The Receiver Operating Characteristic curve and the Area Under
the Curve will be plotted to assess the model's ability to distinguish between classes
across different thresholds.
Implementation:
This project successfully developed a Logistic Regression model to predict customer churn. The
methodology involved a standard machine learning pipeline from data preprocessing to model
evaluation.
The performance metrics, particularly the confusion matrix and the ROC-AUC score, will
provide a comprehensive understanding of the model's strengths and weaknesses. The model's
interpretability is a key advantage, as the coefficients can reveal which features (e.g., contract
type, monthly charges) are most predictive of churn.
Based on the analysis, it will be concluded whether logistic regression is a suitable model for this
dataset. The results will offer actionable insights for the business to identify at-risk customers
and develop targeted retention strategies.
AIM: Write a python program to implement KNN Algorithm on dataset emails and mobile_price
and analyse performance metrics.
Theory
For a given new, unlabeled data point, the KNN algorithm identifies the 'k' number of training
examples that are closest to it in the feature space. The distance is typically calculated using
measures like Euclidean or Manhattan distance.
● For Classification: The algorithm takes a majority vote among the 'k' nearest neighbors.
The class that appears most frequently among these neighbors is assigned to the new data
point.
● For Regression: The algorithm calculates the average (or weighted average) of the target
values of the 'k' nearest neighbors, and this value is assigned to the new data point.
The choice of 'k' is crucial. A small 'k' can make the model sensitive to noise (overfitting), while
a very large 'k' can make the model too general, potentially ignoring important local patterns
(underfitting).
Methodology
This section outlines the steps to implement the KNN algorithm on the provided datasets.
Data Preprocessing:
● Loading Data: The datasets ([Link] and mobile_price.csv) will be loaded into Pandas
DataFrames.
● Exploratory Data Analysis (EDA): Initial analysis will be performed to understand the
data structure, check for missing values, and examine the distribution of the target
variable.
● Feature Selection/Engineering: Relevant features will be selected. Irrelevant columns like
'ID' will be dropped.
● Handling Categorical Data: If any categorical features are present, they will be converted
into numerical format using appropriate techniques like Label Encoding or One-Hot
Encoding.
Prince Verma (25/SWE/24) 32
● Feature Scaling: The KNN algorithm is distance-based, making it essential to scale the
features to a standard range (e.g., using StandardScaler or MinMaxScaler) to prevent
features with larger magnitudes from dominating the distance calculation.
● Train-Test Split: The cleaned dataset will be split into a training set (to fit the model) and
a testing set (to evaluate its performance), typically using an 80-20 or 70-30 split.
Model Implementation:
● The KNN classifier will be implemented using the KNeighborsClassifier class from the
[Link] library.
● The model will be trained (fitted) on the scaled training data.
Hyperparameter Tuning:
● The optimal value of 'k' (number of neighbors) will be determined using techniques like
GridSearchCV or by plotting the model's accuracy for a range of 'k' values and selecting
the one with the highest performance on the validation set.
Performance Analysis:
Implementation:
The implementation of the KNN algorithm on the two datasets will provide a practical
understanding of its application in different classification contexts, such as email filtering and
product categorization.
The performance metrics (Accuracy, Precision, Recall, F1-Score) will be analyzed to draw
conclusions about the model's effectiveness for each specific task. The analysis will highlight the
importance of data preprocessing, especially feature scaling, and the critical role of selecting the
right 'k' value.
A comparison of the results between the two datasets will illustrate how the nature of the data
(e.g., feature types, class distribution) influences the performance of the KNN algorithm. Finally,
the conclusion will summarize the strengths of KNN, such as its simplicity and intuitiveness, and
its limitations, including computational cost for large datasets and sensitivity to irrelevant
features.
Theory
Support Vector Machine (SVM) is a powerful supervised machine learning algorithm primarily
used for classification tasks. The core idea behind SVM is to find the optimal hyperplane that
best separates data points of different classes in a high-dimensional feature space.
The "optimal" hyperplane is chosen as the one with the maximum margin, which is the greatest
possible distance between the hyperplane and the nearest data points from any class. These
closest data points are called "support vectors," as they are the critical elements that define the
position and orientation of the hyperplane.
SVM is particularly effective in scenarios where the data is not linearly separable. It employs a
technique called the "kernel trick," which implicitly maps the input features into a
higher-dimensional space where a linear separation becomes possible. Common kernel functions
include the linear, polynomial, and Radial Basis Function (RBF) kernel.
Methodology
The implementation of the Support Vector Machine for the earthquake dataset will follow a
structured machine learning pipeline.
Data Preprocessing: The first step involves loading the dataset from the CSV file. Key features
relevant to earthquake characterization (such as latitude, longitude, depth, and magnitude) will
be selected. The target variable will be defined, for instance, classifying earthquakes based on
magnitude into categories like "Minor" and "Major." The data will be cleaned by handling
missing values and normalized to ensure all features contribute equally to the model.
Data Splitting: The preprocessed dataset will be divided into two subsets: a training set and a
testing set. The training set is used to teach the SVM model the underlying patterns in the data,
while the testing set is held back to evaluate the model's performance on unseen data.
Model Training: An SVM classifier will be instantiated, typically starting with an RBF kernel
due to its effectiveness for non-linear problems. The model will be trained (or "fitted") using the
training data. During this phase, the algorithm learns to find the optimal hyperplane based on the
provided features and target labels.
In this project, the Support Vector Machine algorithm is applied to classify global earthquake
data. The methodology outlines a clear path from data preparation to model evaluation. By
following this approach, we can build a predictive model capable of categorizing seismic events.
The computed accuracy on the test dataset will serve as the primary metric to assess the model's
generalization capability and its potential utility in seismological analysis. The success of the
model will depend on the quality of the data and the appropriate selection of features and SVM
parameters.
Theory
The "best" attribute is chosen using a metric called Information Gain, which is based on the
concept of Entropy.
● Entropy: This measures the level of impurity or uncertainty in a group of samples. If all
samples belong to the same class, the entropy is 0 (pure). If the samples are evenly split
among classes, the entropy is 1 (impure). It is calculated as:
Information Gain: This measures the reduction in entropy achieved by splitting the
dataset S on a particular attribute A. ID3 calculates the Information Gain for every
attribute and selects the one with the highest value as the node for the split.
The algorithm recursively builds the tree until a stopping criterion is met, such as all
samples at a node belonging to the same class or no more attributes being available to
split on.
Data Preprocessing
1. Start: Begin with the entire preprocessed dataset as the root node.
2. Calculate Base Entropy: Calculate the entropy of the target class (the discretized salary
bins) for the current set of samples.
3. Calculate Information Gain: For every feature (e.g., experience_level), calculate the
weighted average entropy of the subsets created by splitting on that feature. Subtract this
from the base entropy to get the Information Gain.
4. Select Best Attribute: Choose the attribute with the highest Information Gain as the
splitting attribute (decision node) for the current node.
5. Build Tree: Create a new branch for each unique value of the selected attribute.
6. Recurse: Recursively apply the same process (steps 2-5) to the subset of data in each
branch.
7. Stop: The recursion for a branch stops when all samples in that branch belong to the
same target class (creating a leaf node) or when there are no more attributes to split on.
Classification
1. Define New Sample: Create a new, unseen data sample (e.g., experience_level='SE',
company_size='M', remote_ratio=100).
2. Traverse Tree: Pass this sample through the generated decision tree. Start at the root and
follow the branches that match the sample's attribute values.
3. Predict: The leaf node reached at the end of the path provides the predicted salary class
(e.g., 'High') for the new sample.
This program successfully demonstrated the working of the ID3 decision tree algorithm. By
preprocessing the ds_salaries dataset and applying the core logic of Entropy and Information
Gain, a classification model was built.
The process illustrated how ID3 greedily selects the most informative attributes to create a
simple, interpretable, rule-based tree. The final step of classifying a new sample confirmed the
tree's ability to make predictions based on the patterns learned from the data. The experiment
highlights the importance of data preprocessing, especially discretization, when using traditional
algorithms like ID3.
The algorithm works iteratively to minimize the within-cluster sum of squares (WCSS), which
is the total squared distance between each point and its assigned cluster's centroid.
1. Initialization: $k$ initial "centroids" (cluster centers) are chosen randomly or
strategically.
2. Assignment: Each data point is assigned to the nearest centroid, usually based on
Euclidean distance.
3. Update: The centroids are recalculated as the mean (average) of all data points assigned
to that cluster.
Steps 2 and 3 are repeated until the cluster assignments no longer change, meaning the algorithm
has converged.
For the Titanic dataset, K-Means is not used to predict survival. Instead, it is used for pattern
discovery. The goal is to identify natural groupings or "profiles" of passengers based on their
shared characteristics (e.g., "wealthy families," "young solo male travelers").
Data Preprocessing
1. Feature Selection: First, we select relevant numerical and categorical features. Good
candidates include Pclass, Age, Fare, Sex, SibSp (Siblings/Spouses Aboard), and Parch
(Parents/Children Aboard). Features like Name or Ticket are usually dropped as they are
not useful for distance calculations.
2. Handling Missing Data: Missing values, particularly in the Age column, must be
handled. This is typically done by imputing the value using the mean, median, or a more
advanced method.
3. Encoding Categorical Data: K-Means only understands numerical data. Categorical
features like Sex (male/female) and Embarked (C/Q/S) must be converted into numerical
format, often using one-hot encoding or label encoding.
Prince Verma (25/SWE/24) 50
4. Feature Scaling: This is a critical step. K-Means is a distance-based algorithm, so
features with large scales (like Fare, ranging from 0 to 500+) will dominate features with
small scales (like Pclass, 1 to 3). All selected features must be scaled to a similar range
using a tool like StandardScaler or MinMaxScaler.
Model Application
● Finding Optimal $k$: The number of clusters ($k$) is a parameter we must choose. The
standard approach is the Elbow Method. This involves running K-Means for a range of
$k$ values (e.g., 1 to 10) and plotting the WCSS for each. The "elbow" of the resulting
curve—the point where the rate of decrease in WCSS sharply slows down—indicates a
good balance between the number of clusters and the variance within them.
● Clustering: Once an optimal $k$ is chosen (e.g., $k=4$), the K-Means algorithm is
trained on the fully preprocessed data.
● Analysis: Finally, each passenger in the dataset is assigned a cluster label.
Implementation:
By examining the centroid (average feature values) of each cluster, we can build a "persona" for
each group. For example, we might find:
● Cluster 0: High Fare, low Pclass (i.e., 1st), high Age (e.g., "Wealthy, Elderly
Passengers").
Prince Verma (25/SWE/24) 53
● Cluster 1: Low Fare, high Pclass (i.e., 3rd), Sex=male, Age in 20s (e.g., "Young, Solo
Male Travelers").
● Cluster 2: High SibSp/Parch, mixed Pclass (e.g., "Large Families").
While the Survived column was not used during the clustering process (as it's unsupervised), we
can now analyze the survival rate within each cluster. This provides powerful insights. For
instance, we might find that the "Wealthy, Elderly Passengers" cluster had a 65% survival rate,
while the "Young, Solo Male Travelers" cluster had only a 15% survival rate. This demonstrates
how unsupervised segmentation can reveal hidden patterns related to a supervised outcome.
Theory
A Bayesian Network (BN) is a probabilistic graphical model that represents a set of variables
and their conditional dependencies via a Directed Acyclic Graph (DAG). It combines principles
from graph theory and probability theory to efficiently model uncertainty in complex domains.
Key Components:
● Nodes: Represent random variables (e.g., Age, Smoking, Tumor Size). These can be
observable quantities, latent variables, or hypotheses.
● Edges: Represent direct probabilistic dependencies or causal influences between nodes.
An edge from node A to node B indicates that A has a direct influence on B.
● Conditional Probability Tables (CPTs): Each node has a CPT that quantifies the effect of
its parent nodes. For a node with no parents, this is simply its prior probability.
The fundamental rule that allows Bayesian networks to compactly represent a joint probability
distribution is the chain rule of probability. For a network with variables
This formula states that the joint probability of all variables is the product of the conditional
probability of each variable given its parents in the graph. This factorization drastically reduces
the number of parameters needed to define the model.
The primary goal is to perform inference, which involves calculating the posterior probability of
a query variable given observed evidence. For a disease diagnosis, this is formulated using
Bayes' Theorem:
● P(Cancer∣Evidence)
● P(Cancer∣Evidence) is the posterior probability we want to compute—the probability of
cancer given the observed symptoms and test results.
● P(Evidence∣Cancer)
● P(Evidence∣Cancer) is the likelihood—the probability of observing the evidence if the
patient has cancer.
● P(Cancer)
● P(Cancer) is the prior probability—the general prevalence of cancer in the population.
● P(Evidence)
● P(Evidence) is the marginal likelihood or normalizing constant, often computed by
summing over all possible states of the hidden variables.
Methodology
This section outlines the steps to build and use a Bayesian network for cancer diagnosis.
● Expert Knowledge: A domain expert (oncologist) defines the links based on known
medical causality. For example, Smoking directly influences Cancer, and Cancer causes
changes in Tumor Size and Biopsy Result.
● Data-Driven Algorithms: Use algorithms like the K2 or PC algorithm to learn the
structure directly from the dataset by analyzing conditional independencies.
Bayesian networks provide a powerful and intuitive framework for modeling medical diagnosis
under uncertainty. Their ability to incorporate both prior knowledge (from experts) and data
makes them highly suitable for complex domains like oncology. The model clearly represents the
causal relationships between risk factors, the disease itself, and its symptoms or test results.
The key advantage in a medical context is the capacity for explainable reasoning. Unlike some
"black-box" models, a BN can show how evidence from different sources combines to lead to a
specific diagnosis. Furthermore, it allows for flexible inference, enabling "what-if" scenarios. By
implementing this model on a standard cancer dataset, one can demonstrate an effective,
transparent, and statistically sound decision-support system to aid in the early and accurate
diagnosis of cancer.
Theory
relationships in a dataset. The fundamental building block is the artificial neuron, which
receives inputs, processes them with an activation function, and produces an output.
Multiple neurons are organized into layers: an input layer, one or more hidden layers, and an
output layer. Information flows from the input layer, through the hidden layers, and finally to
the output layer. The connections between these neurons have associated weights, which are
The Back-Propagation algorithm is the core learning mechanism for training such
the network, and it propagates through the layers to generate an output. This output is
compared to the actual target value, and the difference is calculated as an error. In the
backward pass, this error is propagated back through the network from the output layer
to the input layer. As the error travels backward, the algorithm calculates the gradient of
the error with respect to each weight. These gradients are then used to update the
weights in a direction that minimizes the overall error of the network. This iterative
process of forward and backward passes over the training data allows the network to
Methodology
dataset. This data will be preprocessed, which includes handling any missing values,
and selecting relevant features (like magnitude, depth, latitude, longitude) to be used as
input variables. The target variable for prediction will be defined, which could be, for
instance, the magnitude of an earthquake. The data will then be split into two subsets: a
training set used to teach the model, and a testing set (the "Test" dataset) used to
involves defining the number of hidden layers, the number of neurons in each layer, and
for the training process, including the learning rate and the number of training iterations
Training and Testing: The model will be trained on the training dataset. During this
phase, the Back-Propagation algorithm will iteratively adjust the network's weights to
minimize the prediction error. Once training is complete, the finalized model will be
Performance Analysis: The model's predictions on the test set will be compared against
the actual values. Performance metrics will be calculated to analyse the model's
effectiveness. For a regression task like earthquake magnitude prediction, key metrics
include Mean Absolute Error (MAE), which measures the average magnitude of errors;
Mean Squared Error (MSE), which gives more weight to larger errors; and R-Squared,
which indicates how well the model explains the variance in the target variable.
Implementation:
with the Back-Propagation algorithm for analyzing earthquake data. The methodology
the network on historical global earthquake data and testing it on a separate dataset, the
aim is to build a model capable of predicting seismic parameters. The final performance
matrix, comprising metrics like MAE, MSE, and R-Squared, will provide a
comprehensive analysis of the model's predictive accuracy and its ability to generalize
to new, unseen data. The success of the model will be determined by how low the error
metrics are and how well it captures the complex, non-linear relationships inherent in
seismic events.