MLnotes Module 3
MLnotes Module 3
MODULE 3
SIMILARITY BASED LEARNING
• Similarity-based learning or Instance based learning is a type of machine learning where the
system makes decisions or predictions by comparing new data to previously seen examples
(instances). Instead of learning a complex formula or pattern, it simply looks at how "close" or
"similar" a new item is to known items and uses that information to make a decision.
• Let’s say you're training a model to recognize fruits (apples, bananas, and oranges). Here’s how
similarity-based learning would go:
o Store examples: The system remembers the shape, colour, and size of different fruits it has
seen.
o Get a new fruit: You give it a new fruit to identify.
o Compare: It measures how similar this fruit is to the examples it has stored.
o Decide: Based on which stored fruits are most similar, it decides what the new fruit probably
is.
• Algorithms based on similarity-based learning:
1. K- Nearest Neighbour
2. Variants of Nearest Neighbour Learning
3. Locally Weighted Regression
4. Learning Vector Quantization (LVQ)
5. Self-Organizing Map (SOM)
6. Radial Basis Function (RBF) Networks
The model would classify the new point based on the majority
class of those 3 neighbours (k=3).
Department of CSE,CEC 1
Machine Learning (BCS02)
• Euclidian Distance- is used to measure how close each training point is to the test point and the
closest neighbours are selected based on this metric.
Algorithm:
Inputs: Training dataset T, distance metric d, Test instance t, the number of nearest neighbours k
Output: Predicted class or category
Prediction: For test instance t,
1. For each instance i in T, compute the distance between the test instance t and every other instance i
in the training dataset using a distance metric (Euclidean distance).
[Continuous attributes – Euclidean distance between two points in the plane with
coordinates (x₁, y₁) and (x₂, y₂) is given as
dist((x₁, y₁), (x₂, y₂)) = √((𝐱₂ − 𝐱₁)² + (𝐲₂ − 𝐲₁)²) ]
[Categorical attributes (Binary) – Hamming Distance: If the value of the two is same, the
distance d will be equal to 0, otherwise d = 1.]
2. Sort the distances in ascending order and select the first k nearest training data instances to the test
instance.
3. Predict the class of the test instance by majority voting (if target attribute is discrete valued) or mean
(if target attribute is continuous valued) of the k selected nearest instances.
Problem 1. Consider the student performance training dataset of 8 data instances shown in Table
below which describes the performance of individual students in a course and their CGPA obtained
in the previous semesters. The independent attributes are CGPA, Assessment and Project. The target
variable is ‘Result’ which is a discrete valued variable that takes two values ‘Pass’ or ‘Fail’. Based
on the performance of a student, classify whether a student with CGPA 6.1, Assessment 40 and project
submission score 5 will pass or fail in that course. Apply KNN
1 9.2 85 8 Pass
2 8.0 80 7 Pass
3 8.5 81 8 Pass
4 6.0 45 5 Fail
5 6.5 50 4 Fail
6 8.2 72 7 Pass
7 5.8 38 5 Fail
8 8.9 91 9 Pass
Department of CSE,CEC 2
Machine Learning (BCS02)
Solution:
Step 1: Calculate Euclidean Distance from the Test Instance Using the formula:
For 3 attributes (CGPA, Assessment, Project):
Distance = √[(𝐂𝐆𝐏𝐀ₜ − 𝐂𝐆𝐏𝐀ᵢ)² + (𝐀𝐬𝐬𝐞𝐬𝐬𝐦𝐞𝐧𝐭ₜ − 𝐀𝐬𝐬𝐞𝐬𝐬𝐦𝐞𝐧𝐭ᵢ)² + (𝐏𝐫𝐨𝐣𝐞𝐜𝐭ₜ − 𝐏𝐫𝐨𝐣𝐞𝐜𝐭ᵢ)²]
Project
[Link]. CGPA Assessment Result Euclidean Distance
Submitted
Sort the distances in ascending order and select the first k nearest training data instances to the test instance.
7 2.022375
4 5.001
5 10.05783
6 32.13114
2 40.09501
3 41.17961
1 45.2063
8 51.23319
Department of CSE,CEC 3
Machine Learning (BCS02)
7 2.022375 Fail
4 5.001 Fail
5 10.05783 Fail
Algorithm
Inputs: Training dataset T, Distance metric d(i, t), Weighting function w(d), Test instance t, the number
of nearest neighbours k
1. For each instance i in Training dataset T, compute the distance between the test instance t and every
other instance i using a distance metric (Euclidean distance).
[Continuous attributes – Euclidean distance between two points in the plane with
coordinates (x₁, y₁) and (x₂, y₂) is given as
dist((x₁, y₁), (x₂, y₂)) = √((𝐱₂ − 𝐱₁)² + (𝐲₂ − 𝐲₁)²) ]
[Categorical attributes (Binary) – Hamming Distance: If the values of two instances are the
same, the distance d will be equal to 0. Otherwise, d = 1.]
2. Sort the distances in the ascending order and select the first k nearest training data instances to the
test instance.
3. Predict the class of the test instance by weighted voting technique (Weighting function w(d)) for the
k selected nearest instances:
Department of CSE,CEC 4
Machine Learning (BCS02)
Problem Consider the student performance training dataset of 8 data instances shown in Table below which
describes the performance of individual students in a course and their CGPA obtained in the previous
semesters. The independent attributes are CGPA, Assessment and Project. The target variable is ‘Result’
which is a discrete valued variable that takes two values ‘Pass’ or ‘Fail’. Based on the performance of a
student, classify whether a student with CGPA 6.1, Assessment 40 and project submission score 5 will pass
or fail in that course. Apply weighted KNN
[Link]. CGPA Assessment Project Submitted Result
1 9.2 85 8 Pass
2 8.0 80 7 Pass
3 8.5 81 8 Pass
4 6.0 45 5 Fail
5 6.5 50 4 Fail
6 8.2 72 7 Pass
7 5.8 38 5 Fail
8 8.9 91 9 Pass
Step 1: Calculate Euclidean Distance from the Test Instance Using the formula:
For 3 attributes (CGPA, Assessment, Project):
Distance = √[(𝐂𝐆𝐏𝐀ₜ − 𝐂𝐆𝐏𝐀ᵢ)² + (𝐀𝐬𝐬𝐞𝐬𝐬𝐦𝐞𝐧𝐭ₜ − 𝐀𝐬𝐬𝐞𝐬𝐬𝐦𝐞𝐧𝐭ᵢ)² + (𝐏𝐫𝐨𝐣𝐞𝐜𝐭ₜ − 𝐏𝐫𝐨𝐣𝐞𝐜𝐭ᵢ)²]
Project
[Link]. CGPA Assessment Result Euclidean Distance
Submitted
1 9.2 85 8 Pass √((9.2 − 6.1)² + (85 − 40)² + (8 − 5)²) = 45.2063
2 8.0 80 7 Pass √((8 − 6.1)² + (80 − 40)² + (7 − 5)²) = 40.09501
3 8.5 81 8 Pass √((8.5 − 6.1)² + (81 − 40)² + (8 − 5)²) = 41.17961
4 6.0 45 5 Fail √((6 − 6.1)² + (45 − 40)² + (5 − 5)²) = 5.001
5 6.5 50 4 Fail √((6.5 − 6.1)² + (50 − 40)² + (4 − 5)²) = 10.05783
6 8.2 72 7 Pass √((8.2 − 6.1)² + (72 − 40)² + (7 − 5)²) = 32.13114
7 5.8 38 5 Fail √((5.8 − 6.1)² + (38 − 40)² + (5 − 5)²) = 2.022375
8 8.9 91 9 Pass √((8.9 − 6.1)² + (91 − 40)² + (9 − 5)²) = 51.23319
Department of CSE,CEC 5
Machine Learning (BCS02)
Sort the distances in ascending order and select the first k nearest training data instances to the test instance.
Instance Euclidean Distance
7 2.022375
4 5.001
5 10.05783
6 32.13114
2 40.09501
3 41.17961
1 45.2063
8 51.23319
Step 2: Select 3 Nearest Neighbors (smallest distances)
Instance Euclidean Distance Class
7 2.022375 Fail
4 5.001 Fail
5 10.05783 Fail
Step 3: Predict the class of the test instance by weighted voting technique from the 3 selected nearest
instances.
Sum=0.495+0.200+0.0995=0.7945
• Compute the weight by dividing each inverse distance by the sum as shown in Table.
Instance Euclidean Distance Inverse Distance Weight =( Inverse / Sum ) Class
7 2.02 0.495 0.6229 Fail
4 5.00 0.200 0.2517 Fail
5 10.05 0.0995 0.1252 Fail
• Add the weights of the same classes:
Fail = 0.6229 + 0.2517 + 0.1252 = 1.000
Pass = 0
• Predict the class by choosing the class with the maximum vote.
The class is predicted as "Fail".
Department of CSE,CEC 6
Machine Learning (BCS02)
Problem 2
A COVID care centre decided to develop a case-based reasoning system to predict whether a person will test
positive or negative based on the symptoms. The table below shows the number of possible symptoms and
the results of the previous cases. The training dataset contains the following instances as shown in the Table
4.13 below.
Loss of
Dry Sore Shortness Chest
[Link]. Fever Tiredness Diarrhea Headache Taste or Result
Cough Throat of Breath Pain
Smell
1 Yes Yes Yes Yes Yes Yes Yes Yes Yes Positive
2 Yes No Yes No No Yes No No No Negative
3 No No No No No No No No No Negative
4 Yes Yes No No No No No No Yes Negative
5 Yes Yes Yes No No No No Yes Yes Positive
6 Yes Yes Yes No No Yes No No No Positive
7 Yes Yes Yes No No No No No No Positive
8 Yes Yes Yes No No No No No No Positive
9 Yes Yes Yes No No No No No No Positive
10 No No No No No No No No No Negative
• Increase 'K' value and check the prediction. Is it good or bad to have a smaller or larger 'K' value?
• Apply proper similarity measure [Asymmetric binary features] and predict the test result of the instance
[Fever = Yes, Dry Cough = Yes, Tiredness = Yes, Sore Throat = Yes, Diarrhea = No, Headache = No,
Loss of Taste or Smell = No, Shortness of Breath = No, Chest Pain = No].
Solution
When we are dealing with binary features (Yes/No converted to 1/0), and presence (Yes) is more important
than absence (No), we use:
We only count mismatches where the test instance has 1 (Yes) and the training instance has 0 (No).
This is because a symptom present in the test case but absent in the training case is important.
The other way (training has Yes, test has No) is ignored.
Department of CSE,CEC 7
Machine Learning (BCS02)
Test Vector:
[1, 1, 1, 1, 0, 0, 0, 0, 0]
Row 1: [1, 1, 1, 1, 1, 1, 1, 1, 1]
Compare with test:
[1, 1, 1, 1, 0, 0, 0, 0, 0]
Only check where test has 1 → first 4 values.
Symptom Test Train Penalty?
Fever 1 1 No
Dry Cough 1 1 No
Tiredness 1 1 No
Sore Throat 1 1 No
Distance = 0
Result: Positive
Row 2: [1, 0, 1, 0, 0, 1, 0, 0, 0]
Symptom Test Train Penalty?
Fever 1 1 No
Dry Cough 1 0 Yes
Tiredness 1 1 No
Sore Throat 1 0 Yes
Distance = 2
Result: Negative
Row 3: [0, 0, 0, 0, 0, 0, 0, 0, 0]
Department of CSE,CEC 8
Machine Learning (BCS02)
Result: Negative
Row 4: [1, 1, 0, 0, 0, 0, 0, 0, 1]
Symptom Test Train Penalty?
Fever 1 1 No
Dry Cough 1 1 No
Tiredness 1 0 Yes
Sore Throat 1 0 Yes
Distance = 2
Result: Negative
Row 5: [1, 1, 1, 0, 0, 0, 0, 1, 1]
Symptom Test Train Penalty?
Fever 1 1 No
Dry Cough 1 1 No
Tiredness 1 1 No
Sore Throat 1 0 Yes
Distance = 1
Result: Positive
Row 6: [1, 1, 1, 0, 0, 1, 0, 0, 0]
Symptom Test Train Penalty?
Fever 1 1 No
Dry Cough 1 1 No
Tiredness 1 1 No
Sore Throat 1 0 Yes
Department of CSE,CEC 9
Machine Learning (BCS02)
All 1’s in test mismatch → Fever, Dry Cough, Tiredness, Sore Throat → 4 mismatches
Distance = 4
Result: Negative
Final Table:
Row Distance Result
1 0 Positive
2 2 Negative
3 4 Negative
4 2 Negative
5 1 Positive
6 1 Positive
7 1 Positive
8 1 Positive
9 1 Positive
10 4 Negative
Department of CSE,CEC 10
Machine Learning (BCS02)
Prediction = Positive
2. Increase 'K' value and check the prediction. Is it good or bad to have a smaller or larger 'K' value
Prediction using k = 5:
Top 5 Neighbors:
Row Distance Result
1 0 Positive
5 1 Positive
6 1 Positive
7 1 Positive
8 1 Positive
Prediction = Positive
The predicted result for the given test instance is: POSITIVE using both k = 3 and k = 5 with
asymmetric binary Hamming distance.
Department of CSE,CEC 11
Machine Learning (BCS02)
In Nearest Centroid Classifier, the distance between the centroid (average) of each class and the test
instance is calculated. The test instance is assigned to the class with the minimum distance.
Algorithm
Problem:
Consider the sample data shown in Table with two features x and y. The target classes are ‘A’ or ‘B’.
Predict the class using Nearest Centroid Classifier.
Step 1: Compute the mean/centroid of each class. In this example there are two classes called ‘A’
and ‘B’.
Centroid of class “A” = (3 + 5 + 4, 1 + 1 + 2)/3 = (12, 6)/3 = (4, 2)
Centroid of class “B” = (7 + 6 + 8, 6 + 7 + 5)/3 = (21, 18)/3 = (7, 6)
Now given a test instance (6, 5), we can predict the class.
Step 2: Calculate the Euclidean distance between test instance (6, 5) and each of the centroid.
The test instance has smaller distance to class B. Hence, the class of this test instance is
predicted as ‘B’.
Department of CSE,CEC 12
Machine Learning (BCS02)
• In simple linear regression, the goal is to find a straight line that best fits all the data points.
The prediction function is given as:
𝒉𝜷 (𝒙) = 𝜷𝟎 + 𝜷𝟏 (x)
ℎ𝛽 (𝑥) is predicted function/ hypothesis function
𝛽1 𝑖𝑠 𝑡ℎ𝑒 𝑐𝑜𝑒𝑓𝑓𝑖𝑐𝑖𝑒𝑛𝑡 𝑜𝑓 𝑥
𝛽0 is the intercept
• (The intercept is where the line crosses or touches the Y-axis. It is the value of y when x = 0.)
This minimises the error between predicted value ℎ𝛽 (𝑥) and true value y.
• In LWR, the cost function is changed/ modified so that closer points have more influence, and farther
points have less influence when making predictions. This is done using weights, and that's what
Equation below represents.
𝟏
J(β) = 𝟐 ∑𝒎
𝒊=𝟏 𝒘𝒊 (𝒉𝜷 (𝒙𝒊 ) − 𝒚𝒊 )
𝟐
−(𝒙𝒊 −𝒙)𝟐
Gaussian kernel, 𝒘𝒊 = 𝒆 𝟐𝝉𝟐
τ is bandwidth parameter
Department of CSE,CEC 13
Machine Learning (BCS02)
Problem:
Consider a simple example with four instances shown in Table below and apply locally weighted
regression.
Solution:
Using linear regression model assuming we have computed the parameters: β₀ = 4.72, β₁ = 0.62
Given a test instance with x = 2, the predicted y is:
𝒚𝒍 = 𝜷𝟎 + 𝜷𝟏 (x)
Department of CSE,CEC 14
Machine Learning (BCS02)
compute the weights for the closest instances, using the Gaussian kernel,
−(𝒙𝒊 −𝒙)𝟐
𝒘𝒊 = 𝒆 𝟐𝝉𝟐
The predicted output for the three closest instances is given as follows:
The predicted output of Instance 2 is: 𝒚𝟐 = 𝒉𝜷 (𝒙𝟐 ) = ( 𝜷𝟎 + 𝜷𝟏 (𝒙𝟐 ) )= 4.72 +( 0.62 X 1) = 5.34
The predicted output of Instance 3 is: 𝒚𝟑 = 𝒉𝜷 (𝒙𝟑 ) = (𝜷𝟎 + 𝜷𝟏 (𝒙𝟑 ) )= 4.72 +( 0.62 X 2) = 5.96
The predicted output of Instance 4 is: 𝒚𝟒 = 𝒉𝜷 (𝒙𝟒 ) = (𝜷𝟎 + 𝜷𝟏 (𝒙𝟒 )) = 4.72 +( 0.62 X 1) = 5.34
𝟏
The error value/ adjusted cost function is calculated as: J(β) = 𝟐 ∑𝒎
𝒊=𝟏 𝒘𝒊 (𝒉𝜷 (𝒙𝒊 ) − 𝒚𝒊 )
𝟐
𝟏
= 𝟐[0.043(𝟓. 𝟑𝟒 − 𝟓)𝟐 + 1(𝟓. 𝟗𝟔 − 𝟕)𝟐 + 0.043(𝟓. 𝟑𝟒 − 𝟖)𝟐
= 0.6953
Department of CSE,CEC 15
Machine Learning (BCS02)
How to choose τ :
1. Manual testing: Try values like 0.1, 0.3, 0.5, 0.8, 1.0, 2.0, ... and see performance.
2. Cross-validation: Split your data, and test different τ\tauτ values to see which gives best
prediction accuracy.
3. Plot weights vs distance: Visually check how fast weights decay.
4. Grid search + error metric (e.g., RMSE): Automate the selection using a performance metric.
Department of CSE,CEC 16
Machine Learning (BCS02)
Regression Analysis
INTRODUCTION TO REGRESSION
Regression analysis is the premier method of supervised learning. This is one of the most popular and oldest
supervised learning techniques. Given a training dataset D containing N training points (xi,yi), where
i=1,...,N, regression analysis is used to model the relationship between one or more independent variables x
and a dependent variable y. The relationship between the dependent and the independent variables can be
represented as a function as follows:
y=f(x) …………………..(5.1)
The feature variable x is also known as an explanatory variable, exploratory variable, a predictor variable,
an independent variable, a covariate, or a domain point. y is a dependent variable. Dependent variables are
also called as labels, target variables, or response variables.
Regression analysis determines the change in response variables when one explanatory variable is varied
while keeping all other parameters constant. This is used to determine the relationship each of the exploratory
variables exhibits. Thus, regression analysis is used for prediction and forecasting.
Regression is used to predict continuous variables or quantitative variables such as price and revenue.
Thus, the primary concern of regression analysis is to find answers to questions such as:
There are many applications of regression analysis. Some of the applications of regression include predicting:
Types of Regression
Department of CSE,CEC 17
Machine Learning (BCS02)
1. Linear Regression
It is a type of regression where a line is fitted upon given data for finding the linear relationship
between one independent variable and one dependent variable to describe relationships.
2. Multiple Regression
It is a type of regression where a line is fitted for finding the linear relationship between two or more
independent variables and one dependent variable to describe relationships among variables.
3. Polynomial Regression
It is a type of non-linear regression method of describing relationships among variables where Nᵗʰ
degree polynomial is used to model the relationship between one independent variable and one
dependent variable. Polynomial multiple regression is used to model two or more independent
variables and one dependent variable.
4. Logistic Regression
It is used for predicting categorical variables that involve one or more independent variables and one
dependent variable. This is also known as a binary classifier.
In the simplest form, the linear regression model can be created by fitting a line among the scattered data
points. The line is of the form given in Eq. (5.2).
y = a₀ + a₁x + e (5.2)
Here, a0 is the intercept which represents the bias and a1 represents the slope of the line. These are called
regression coefficients. e is the error in prediction.
The idea of linear regression is based on Ordinary Least Square (OLS) approach. This method is also known
as ordinary least squares method. In this method, the data points are modelled using a straight line. Any
arbitrarily drawn line is not an optimal line. In Figure 5.4, three data points and their errors (e1,e2,e3)are
shown. The vertical distance between each point and the line (predicted by the approximate line equation
y=a0+a1x) is called an error. These individual errors are added to compute the total error of the predicted
line. This is called sum of residuals. The squares of the individual errors can also be computed and added
to give a sum of squared error. The line with the lowest sum of squared error is called line of best fit.
Department of CSE,CEC 18
Machine Learning (BCS02)
In another words, OLS is an optimization technique where the difference between the data points and the
line is optimized.
Mathematically, based on Eq. (5.2), the line equations for points (x1,x2,...,xn)) are:
y₁ = a₀ + a₁x₁ + e₁
y₂ = a₀ + a₁x₂ + e₂
yₙ = a₀ + a₁xₙ + eₙ (5.3)
Here, the terms (e1,e2,...,en) are error associated with the data points and denote the difference between the
true value of the observation and the point on the line. This is also called as residuals. The residuals can be
positive, negative or zero.
A regression line is the line of best fit for which the sum of the squares of residuals is minimum. The
minimization can be done as minimization of individual errors by finding the parameters a0 and a1such that:
Department of CSE,CEC 19
Machine Learning (BCS02)
Sum of the squares of the individual errors, often preferred as individual errors (positive and negative errors),
do not get cancelled out and are always positive, and sum of squares results in a large increase even for a
small change in the error. Therefore, this is preferred for linear regression.
Here, J(a1,a0) is the criterion function of parameters a0 and a1. This needs to be minimized. This is done
by differentiating and substituting to zero. This yields the coefficient values of a0 and a1. The values of
estimates of a0 and a1 are given as follows:
Example 5.1
Let us consider an example where the five weeks' sales data (in Thousands) is given as shown below in
Table 5.1. Apply linear regression technique to predict the 7ᵗʰ and 12ᵗʰ month sales.
Solution:
Here, there are 5 items, i.e., 𝑖 = 1, 2, 3, 4, 5. The computation table is shown below (Table 5.2). Here, there
are five samples, so 𝑖 ranges from 1 to 5.
Department of CSE,CEC 20
Machine Learning (BCS02)
𝑥ᵢ 𝑦ᵢ (𝑥ᵢ)² 𝑥ᵢ × 𝑦ᵢ
1 1.2 1 1.2
2 1.8 4 3.6
3 2.6 9 7.8
4 3.2 16 12.8
5 3.8 25 19.0
Sum=15 Sum=12.6 Sum=55 Sum=44.4
Average of xi Average of yi. Average of xᵢ²: Average of xi×yi
xˉ=15/5=3 yˉ=12.6/5=2.52 xi2ˉ=55/5=11 xy‾=44.4/5=8.88
Predicted Sales:
• 7ᵗʰ week (x = 7):
y = 0.54 + 0.66 × 7 = 5.16
Department of CSE,CEC 21
Machine Learning (BCS02)
• Actual values (xi,yi) : These are shown as individual data points (like dots or squares) on the
graph.
• Predicted values (xi, ȳ) : These are calculated using the regression equation ȳ = 0.54 + 0.66x, and
the line connecting these points is the regression line.
The above graph shows a fitted line. The line almost passes through or very close to the actual points —
because the data is highly linear, and the regression model captures that relationship accurately.
y₁ = a₀ + a₁x₁ + e₁
y₂ = a₀ + a₁x₂ + e₂
yₙ = a₀ + a₁xₙ + eₙ (5.3)
Y=Xa+e
Department of CSE,CEC 22
Machine Learning (BCS02)
where:
• X is an n×2 matrix,
• Y is an n×1 vector,
• a is a 2×1 column vector, and
• e is an n×1 column vector.
Example
Find the linear regression of the data of week and product sales (in Thousands) using the matrix form of
linear regression.
(Table 5.3)
Department of CSE,CEC 23
Machine Learning (BCS02)
Step-by-Step Computation
1. Compute XTX :
3. Compute (XTX)-1 XT :
4. Multiply with Y:
• So, the intercept is –1.5 and the slope is 2.2. (The first value in the result vector (-1.5) is the
intercept — denoted as [Link] second value (2.2) is the slope — denoted as a1.)
Department of CSE,CEC 24
Machine Learning (BCS02)
Multiple Linear Regression is an extension of simple linear regression. It models the relationship between
one dependent variable and two or more independent (predictor) variables. The basic assumptions of
Multiple Linear Regressions are
1. Independent variables are not highly correlated (i.e., no multicollinearity).
2. The residuals (errors) are normally distributed.
Example with Two Predictors:
When there are two independent variables x1 and x2, the regression model is:
Example
Apply multiple regression for the values given in Table 5.7, where weekly sales y are provided along with
sales of products x1 and x2. Use the matrix approach to find the regression equation.
x1 x2 Y
Product one sales Product one sales Output weekly sales(in thousands)
1 4 1
2 5 6
3 8 8
4 2 12
Step 1: General Formula
The matrix formula for multiple regression is:
𝐴 = (𝑋ᵗ𝑋)⁻¹ 𝑋ᵗ𝑌
Where:
• X is the matrix of independent variables (with a column of ones for the intercept),
• Y is the column vector of dependent variables,
• A is the column vector of regression coefficients:
Department of CSE,CEC 25
Machine Learning (BCS02)
Note : The first column of 1s allows the equation to include an intercept a0. Without it, the model would
force the regression line to pass through the origin (0,0) — which is usually not what we want.
Department of CSE,CEC 26
Machine Learning (BCS02)
POLYNOMIAL REGRESSION
In many practical situations, the relationship between the independent and dependent variables is not linear.
If a linear regression model is applied to such data, it may result in large prediction errors. To handle this
issue, there are two commonly used approaches to deal with non-linear regression problems:
1. Transforming the non-linear data into linear form, allowing the use of linear regression.
2. Using polynomial regression, which can directly model non-linear relationships.
1. Transformations
The transformation method is based on converting a non-linear relationship into a linear one. Once the data
has been transformed into a linear form, standard linear regression techniques can be applied.
Let us consider an exponential function where y is expressed as y = aeᵇˣ. To convert this into a linear form,
we take the natural logarithm of both sides:
ln y = bx + ln a (5.24)
The resulting equation is now linear in terms of ln y and x. This transformed data can now be analyzed using
linear regression.
Another common non-linear function is the power function y = axᵇ. To linearize this relationship, we apply
the base-10 logarithm to both sides of the equation:
log₁₀y = b log₁₀x + log₁₀a (5.25)
Again, this transformation produces a linear relationship between log₁₀y and log₁₀x, allowing for the use of
linear regression techniques.
After the linear regression is applied to the transformed data and the coefficients are estimated, the original
non-linear model can be retrieved by applying the inverse of the transformation.
2. Polynomial Regression
Polynomial regression is a method that models the relationship between variables as an n-th degree
polynomial. This approach does not require any transformation and is well suited to handle curvilinear
relationships directly.
For example, quadratic regression (second-degree polynomial) models the data using a function of the form:
y = a₀ + a₁x + a₂x²
Cubic regression (third-degree polynomial) uses a function of the form:
y = a₀ + a₁x + a₂x² + a₃x³
In general, polynomial regression of degree up to 4 is used, as higher-degree polynomials may result in
overfitting, which reduces the model's ability to generalize to new data.
Let us now consider fitting a second-degree polynomial to a given set of data points (x₁, y₁), (x₂, y₂), ..., (xₙ,
yₙ). The polynomial model is given by:
Department of CSE,CEC 27
Machine Learning (BCS02)
To determine the optimal coefficients a₀, a₁, and a₂ that minimize the error E, we take the partial derivatives
of E with respect to each coefficient and set them equal to zero:
∂E/∂a₀ = 0, ∂E/∂a₁ = 0, ∂E/∂a₂ = 0
This process leads to a system of linear equations known as the normal equations. These equations are:
These equations can be expressed in matrix form for convenience. The matrix form of the system is:
This matrix equation is of the form Xa = B, where X is the matrix of input features, a is the vector of
unknown coefficients, and B is the vector of known outcomes. To solve for the coefficient vector a, we use
the inverse of matrix X as follows:
a = X⁻¹B (5.29)
Example Consider the data provided in Table and fit it using the second-order polynomial.
x y
1 1
2 4
3 9
4 15
Department of CSE,CEC 28
Machine Learning (BCS02)
Solution:
To apply polynomial regression of order 2, computations are carried out as shown in Table .
Computation Table
Step 2: Solve
LOGISTIC REGRESSION
Linear regression is used to predict numerical responses, but it is not suitable for categorical variables. When
dealing with categorical variables, the problem is known as a classification problem. Logistic regression is
suitable for binary classification (i.e., two possible outcomes).
Department of CSE,CEC 29
Machine Learning (BCS02)
Logistic regression works by predicting the probability of a categorical variable. It uses one or more features
x to predict the response y. If linear regression is used to predict probability:
p(x) = a₀ + a₁x
But the value of p(x) must lie between 0 and 1, unlike linear regression which gives a range from -∞ to +∞.
Sigmoid Function:
To map values between 0 and 1, the sigmoid function is used:
y = { 1 if p(x) ≥ 0.5,
0 otherwise } (5.36)
Where:
x: predictor variable
e: Euler number
a₀, a₁: regression coefficients (learned during training)
Department of CSE,CEC 30
Machine Learning (BCS02)
Example
Let us assume a binary logistic regression problem with two classes: pass and fail. The student dataset
includes entrance exam marks. Based on past data, the model is trained to predict selection.
Given: Regression coefficients are a₀ = 1 and a₁ = 8. If a student has marks x = 60, compute the probability
of selection and determine the class.
Step 1: Compute z using regression coefficients:
z = a₀ + a₁x
= 1 + 8 × 60 = 481
Since the threshold is 0.5 and 0.44 < 0.5, the student is not selected.
To estimate the parameters, we take the log of the likelihood function and use methods like Newton’s method
to maximize it.
Department of CSE,CEC 31
Machine Learning (BCS02)
The decision tree learning model is a popular supervised predictive learning model used for classification
tasks. It classifies data instances with high accuracy and consistency. It performs inductive inference,
meaning it draws general conclusions from observed examples. The model is widely used for complex
classification problems. A decision tree summarizes information from the training dataset in a tree
structure. Once this model is built, it can easily classify test data.
Decision trees can handle both categorical and continuous-valued target variables. Given a training dataset
X, it computes a hypothesis function f(X) in the form of a decision tree. Inputs to the model are objects or
data instances with features, which can be either discrete or continuous. The model outputs a tree that predicts
the class of the test data. In statistics, these features are called independent variables, while the target class
is called the response variable.
The model generates a complete hypothesis space using the training dataset. This allows searching through
different hypotheses by traversing the tree. Smaller trees represent specific hypotheses during this process.
This search bias is known as preference bias.
A decision tree has a structure that includes a root node, internal nodes (also called decision nodes),
branches, and leaf nodes (also called terminal nodes). The root node is the topmost node. Internal nodes
are test points based on input attributes, and the branches represent the outcomes of these tests. Each decision
node leads to branches that represent sub-sections of the tree. Each branch ends at a leaf node that gives the
final classification output.
Leaf nodes contain class labels which are the final outcomes of the decision paths. Every path from the root
to a leaf represents a logical rule formed by a combination of test conditions. The full tree forms a set of
classification rules in logical form.
Decision trees can also be extended into decision networks or influence diagrams, which have a directed
graph structure. These are based on Bayesian belief networks and represent node states, actions, outcomes,
and utilities. Symbols used in decision trees include circles for root nodes, diamonds for decision nodes,
and rectangles for leaf nodes.
Department of CSE,CEC 32
Machine Learning (BCS02)
Example1
How to draw a decision tree to predict a student’s academic performance based on the given information
such as class attendance, class assignments, home-work assignments, tests, participation in
competitions or other events, group activities such as projects and presentations, etc.
Department of CSE,CEC 33
Machine Learning (BCS02)
Department of CSE,CEC 34
Machine Learning (BCS02)
The decision tree is constructed by following a series of if-else conditions based on all or some of the
attributes. The tree allows for non-binary splits (e.g., Good/Average/Poor), meaning it's not always a
binary tree.
A decision tree is not always a binary tree. It is a tree which can have more than two branches.
Example 2:
Predict a student’s academic performance of whether they will pass or fail based on the given information
such as ‘Assessment’ and ‘Assignment’. The independent variables are Assessment and Assignment, and
the target variable is Exam Result with values Pass and Fail.
Attributes Values
Assessment ≥50, <50
Assignment Yes, No
Exam Result Pass, Fail
Department of CSE,CEC 35
Machine Learning (BCS02)
Fundamentals of Entropy
• When building a decision tree, the goal is to choose the attribute that best separates the data based
on the target class (label).
• The best attribute for splitting is the one that provides the most information about the classification
outcome.
• This splitting continues until the stopping condition is met. At each step, we want the data subsets
to be as pure as possible.
What is Entropy?
• Entropy is a measure of randomness or uncertainty in data.
• It helps decide which feature gives the best split.
• A lower entropy means higher purity (i.e., data is more similar), and a higher entropy means more
uncertainty.
This gives a value between 0 and 1. A value closer to 0 is preferred because it means a clearer classification.
General Formula
• Let P be the probability distribution of outcomes (classes).
• If there are n possible classes, then:
• If we consider a case with 6 Pass and 4 Fail students (i.e., P1=0.6 = 0.6, P2=0.4):
Department of CSE,CEC 36
Machine Learning (BCS02)
• Here, Pr[X = x] is the probability of an outcome x, and this form shows that lower probabilities
give higher entropy.
Note:
Stopping Criteria
The following are some of the common stopping conditions:
1. The data instances are homogeneous, which means all belong to the same class Ci, and hence its
entropy is 0.
2. A node with some defined minimum number of data instances becomes a leaf.
(The number of data instances in a node is between 0.25% and 1.00% of the full training dataset).
3. The maximum tree depth is reached, so further splitting is not done and the node becomes a leaf
node.
Department of CSE,CEC 37
Machine Learning (BCS02)
Definitions
• Let T be the training dataset.
• Let A be the set of attributes:
A={A1,A2,A3,…,An}
• Let m be the number of classes in the training dataset.
• Let Pi be the probability that a data instance or tuple 'd' belongs to class Ci.
It is calculated as:
Department of CSE,CEC 38
Machine Learning (BCS02)
Where:
• Attribute A has v distinct values {a1,a2,...,aj}
• ∣Ai∣: Number of instances for distinct value i in attribute A
• Entropy_Info(Ai): Entropy for the i-th subset of instances
Information Gain
• Information_Gain(A) measures how much information is gained by branching on attribute A.
• It reflects the reduction in impurity after the split.
It is computed as:
Example
Assess a student’s performance during his course of study and predict whether a student will get a job offer
or not in his final year of the course. The training dataset T consists of 10 data instances with attributes such
as ‘CGPA’, ‘Interactiveness’, ‘Practical Knowledge’ and ‘Communication Skills’ as shown in Table .
The target class attribute is the ‘Job Offer’.
Table 6.3: Training Dataset T
[Link]. CGPA Interactiveness Practical Knowledge Communication Skills Job Offer
1 ≥9 Yes Very good Good Yes
2 ≥8 No Good Moderate Yes
3 ≥9 No Average Poor No
4 <8 No Average Good No
5 ≥8 Yes Good Moderate Yes
6 ≥9 Yes Good Moderate Yes
7 <8 Yes Good Poor No
8 ≥9 No Very good Good Yes
9 ≥8 Yes Good Good Yes
10 ≥8 Yes Average Good Yes
Department of CSE,CEC 39
Machine Learning (BCS02)
Solution :
Department of CSE,CEC 40
Machine Learning (BCS02)
Department of CSE,CEC 41
Machine Learning (BCS02)
Department of CSE,CEC 42
Machine Learning (BCS02)
Department of CSE,CEC 43
Machine Learning (BCS02)
C4.5 Construction
C4.5 is an improvement over ID3. C4.5 works with continuous and discrete attributes and missing values,
and it also supports post-pruning. C5.0 is the successor of C4.5 and is more efficient and used for building
smaller decision trees. C4.5 works with missing values by marking as ‘?’, but these missing attribute values
are not considered in the calculations.
The algorithm C4.5 is based on Occam’s Razor which says that given two correct solutions, the simpler
solution has to be chosen. Moreover, the algorithm requires a larger training set for better accuracy. It uses
Gain Ratio as a measure during the construction of decision trees. ID3 is more biased towards attributes with
larger values. For example, if there is an attribute called ‘Register No’ for students it would be unique for
every student and will have distinct value for every data instance resulting in more values for the attribute.
Hence, every instance belongs to a category and would have higher Information Gain than other attributes.
To overcome this bias issue, C4.5 uses a purity measure Gain ratio to identify the best split attribute. In C4.5
algorithm, the Information Gain measure used in ID3 algorithm is normalized by computing another factor
called Split_Info. This normalized information gain of an attribute called as Gain_Ratio is computed by the
ratio of the calculated Split_Info and Information Gain of each attribute. Then, the attribute with the highest
normalized information gain, that is, highest gain ratio is used as the splitting criteria.
As an example, we will choose the same training dataset shown in Table 6.3 to construct a decision tree using
the C4.5 algorithm.
Given a Training dataset T,
The Split_Info of an attribute A is computed as given in Eq. (6.11):
where, the attribute A has got ‘d’ distinct values a1,a2,...,ad and ∣Ai∣ is the number of instances for distinct
value ‘i’ in attribute A.
The Gain_Ratio of an attribute A is computed as given in Eq. (6.12):
Department of CSE,CEC 44
Machine Learning (BCS02)
5. The root node is branched into subtrees with each subtree as an outcome of the test condition of the
root node attribute. Accordingly, the training dataset is also split into subsets.
6. Recursively apply the same operation for the subset of the training set with the remaining attributes
until a leaf node is derived or no more training instances are available in the subset.
Department of CSE,CEC 45
Machine Learning (BCS02)
Department of CSE,CEC 46
Machine Learning (BCS02)
Department of CSE,CEC 47
Machine Learning (BCS02)
Department of CSE,CEC 48
Machine Learning (BCS02)
Department of CSE,CEC 49
Machine Learning (BCS02)
Department of CSE,CEC 50
Machine Learning (BCS02)
Department of CSE,CEC 51
Machine Learning (BCS02)
Where:
• Pi be the probability that a data instance or a tuple ‘d’ belongs to class Ci. It is computed as:
• Pi=No. of data instances belonging to class i /Total no. of data instances in the training dataset TP_i
GINI Index assumes a binary split on each attribute, therefore, every attribute is considered as a binary
attribute which splits the data instances into two subsets S1 and S2
Gini_Index(T, A) is computed as given in Eq. (6.14):
The splitting subset with minimum Gini_Index is chosen as the best splitting subset for an attribute. The
best splitting attribute is chosen by the minimum Gini_Index which is otherwise maximum ΔGini because
it reduces the impurity.
ΔGini is computed as given in Eq. (6.15):
Department of CSE,CEC 52
Machine Learning (BCS02)
Department of CSE,CEC 53
Machine Learning (BCS02)
Department of CSE,CEC 54
Machine Learning (BCS02)
Department of CSE,CEC 55
Machine Learning (BCS02)
Department of CSE,CEC 56
Machine Learning (BCS02)
Department of CSE,CEC 57
Machine Learning (BCS02)
Department of CSE,CEC 58
Machine Learning (BCS02)
Department of CSE,CEC 59
Machine Learning (BCS02)
Department of CSE,CEC 60
Machine Learning (BCS02)
Regression Trees
Regression trees are a variant of decision trees where the target feature is a continuous valued variable.
These trees can be constructed using an algorithm called reduction in variance which uses standard
deviation to choose the best splitting attribute.
Algorithm 6.5: Procedure for Constructing Regression Trees
1. Compute standard deviation for each attribute with respect to target attribute.
2. Compute standard deviation for the number of data instances of each distinct value of an attribute.
3. Compute weighted standard deviation for each attribute.
4. Compute standard deviation reduction by subtracting weighted standard deviation for each attribute
from standard deviation of each attribute.
5. Choose the attribute with a higher standard deviation reduction as the best split attribute.
6. The best split attribute is placed as the root node.
7. The root node is branched into subtrees with each subtree as an outcome of the test condition of the
root node attribute. Accordingly, the training dataset is also split into different subsets.
8. Recursively apply the same operation for the subset of the training set with the remaining attributes
until a leaf node is derived or no more training instances are available in the subset.
Department of CSE,CEC 61
Machine Learning (BCS02)
Department of CSE,CEC 62
Machine Learning (BCS02)
Department of CSE,CEC 63
Machine Learning (BCS02)
Department of CSE,CEC 64
Machine Learning (BCS02)
Department of CSE,CEC 65
Machine Learning (BCS02)
Department of CSE,CEC 66