Unit 2 Partial
Unit 2 Partial
1
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
UNIT 2
SUPERVISED LEARNING
Linear Regression Models: Least squares, single & multiple
variables, Bayesian linear regression, gradient descent, Linear
Classification Models: Discriminant function – Perceptron
algorithm, Probabilistic discriminative model - Logistic regression,
Probabilistic generative model – Naive Bayes, Maximum margin
classifier – Support vector machine, Decision Tree(ID3), Random
Forests.
2
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
3
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
4
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
There are many types of functions or modules that can be used for
regression. A linear function is the simplest type of function. Here,
X may be a single feature or multiple features representing the
problem.
The best-fit line will be the one that optimizes the values of m
(slope) and b (intercept); so that the predicted y values are as close
as possible to the actual data points.
Least squares:
To find the best-fit line, we use a method called Least
Squares. The idea behind this method is to minimize the sum of
squared differences between the actual values (data points) and the
predicted values from the line. These differences are called
residuals.
The formula for residuals is:
5
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Where:
• yᵢ is the actual observed value
• y^ᵢ is the predicted value from the line for that xᵢ
The least squares method minimizes the sum of the squared
residuals:
6
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
• The red points in the above plot represent the data points for
the sample data available.
• Independent variables are plotted as x-coordinates, and
dependent ones are plotted as y-coordinates.
• The equation of the line of best fit obtained from the Least
Square method is plotted as the red line in the graph.
We can conclude from the above graph how the Least Square
method helps us to find a line that best fits the given data points and
hence can be used to make further predictions about the value of the
dependent variable where it is not known initially.
How Do You Calculate Least Square?
To calculate the least squares solution, you typically need to:
i. Determine the equation of the line you believe best fits the data.
ii. Calculate the residuals (differences) between the observed
7
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
The slope of the line of best fit can be calculated from the formula
8
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
as follows:
m = (Σ (X - xi)*(Y - yi)) /Σ(X - xi)2
m = 55/32.8 = 1.68 (rounded upto 2 decimal places)
Now, the intercept will be calculated from the formula as follows:
c = Y - mX
c = 8 - 1.68*4.2 = 0.94
Thus, the equation of the line of best fit becomes, y = 1.68x + 0.94.
Problem 2: Find the line of best fit for the following data of
heights and weights of students of a school using the Least
Squares method:
• Height (in centimeters): [160, 162, 164, 166, 168]
• Weight (in kilograms): [52, 55, 57, 60, 61]
Solution:
Here, we denote Height as x (independent variable) and Weight as
y (dependent variable). Now, we calculate the means of x and y
values denoted by X and Y respectively.
X = (160 + 162 + 164 + 166 + 168 ) / 5 = 164
Y = (52 + 55 + 57 + 60 + 61) / 5 = 57
9
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Now, the slope of the line of best fit can be calculated from the
formula as follows:
m = (Σ (X - xi)✕(Y - yi)) / Σ(X - xi)2
m = 46/40 = 1.15
Now, the intercept will be calculated from the formula as follows:
c = Y - mX
c = 57 - 1.15*164 = -131.6
Thus, the equation of the line of best fit is, y = 1.15x - 131.6
x 1 2 3 4
y 2 3 5 7
x 10 20 30 40
y 25 28 32 35
x -2 -1 0 1 2
y 4 1 0 1 4
10
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
x (year) 1 2 3 4 5
y (population in thousands) 50 54 57 60 65
11
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
12
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
13
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
14
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
15
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
16
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
17
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Types of Classification
i) Binary Classification
This is the simplest kind of classification. In binary
classification, the goal is to sort the data into two distinct
categories. Think of it like a simple choice between two
options. Imagine a system that sorts emails into either spam or
not spam. It works by looking at different features of the email
like certain keywords or sender details and decides whether
it’s spam or not. It only chooses between these two options.
ii) Multiclass Classification
Here, instead of just two categories, the data needs to be
sorted into more than two categories. The model picks the one
that best matches the input. Each data point belongs to only
one class or category. Think of an image recognition system
that sorts pictures of animals into categories like cat, dog and
bird.
18
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Example for linear classifier: X1 and X2 are the internal mark and
external mark of students
X1 X2 Y=X1+X2-50 Class / Label
30 40 20 (+ve) Pass
25 35 10 (+ve) Pass
10 15 -25 (-ve) Fail
12 18 -20 (-ve) Fail
19
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
20
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Discriminant function:
Linear Discriminant Analysis (LDA) also known as Normal
Discriminant Analysis or Discriminant Function Analysis is
supervised classification problem that helps separate two or more
classes by converting higher-dimensional data space into a lower-
dimensional space. (2D into 1D)
For example, the below image shows 2 different classes with set of
black and green data points that are not linearly separable. It means,
there is no straight line that can separate 2 classes of data points
21
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
22
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
This shows how LDA creates a new axis to project the data and
separate two classes along a linear path. However, when class
distributions share the same mean, LDA cannot find a separating axis
and non-linear discriminant analysis is needed.
Step 5: Compute Eigen values and Eigen vectors from the ‘within
classes (Sw)’ and ‘between class scatter matrix (SB)’
23
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Step 8: Obtain the LDA by taking the dot product of Eigen vector
and original data
24
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
25
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
26
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
27
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
28
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
29
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Perceptron algorithm:
Perceptron is a linear supervised machine learning algorithm.
It is used for binary classification. These algorithms analyze and
process the data to allow machines to recognize patterns, make
decisions, and interact effectively, mimicking human sensory
perception.
Key applications: Autonomous vehicles, facial recognition, and
augmented reality etc.,
30
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
∑wi∗xi = x1∗w1+x2∗w2+…+wn∗xn.
Add another essential term called bias 'b' (interceptor) to the
weighted sum to improve the model performance.
∑wi∗xi+b.
ii. Next, an activation function is applied to this weighed sum,
producing a binary or a continuous-valued output.
Y=f(∑wi∗xi+b)
iii. Next, the difference between this output and the actual target value
is computed to get the error term, E, generally in terms of mean
squared error. The steps up to this form the forward propagation
part of the algorithm.
E=(Y−Yactual)2
iv. Then, optimize this error (loss function) using an optimization
algorithm. Generally, some form of gradient descent algorithm is
used to find the optimal values of the hyperparameters
like learning rate, weight, Bias, etc. This step forms the backward
propagation part of the algorithm.
An overview of this algorithm is illustrated in the following Figure:
31
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Perceptron Example:
Imagine a perceptron (in your brain).
The perceptron tries to decide if you should go to a music concert.
Is the artist good? Is the weather good?
What weights should these facts have?
32
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Note:
• If the weather weight is 0.6 for you, it might be different for
someone else. A higher weight means that the weather is more
important to them.
• If the threshold value is 1.5 for you, it might be different for
someone else. A lower threshold means they are more
wanting to go to any concert.
33
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Generative models:
Generative models aim to model the joint distribution of the
input and output variables. These models generate new data
34
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Discriminative models:
Discriminative models are used for classification, where the
output is a binary (either true or false) prediction. The
discriminative model aims to model the conditional distribution
of the output variable given the input variable. They learn a
decision boundary that separates the different classes of the
output variable. Discriminative models are useful when the focus
is on making accurate predictions rather than generating new
data. They can be used for tasks such as image recognition,
speech recognition, and sentiment analysis. Discriminative
models have been applied successfully in many machine learning
applications, such as spam filtering, document classification, and
voice recognition. They have also been used successfully to
predict the outcomes of events such as earthquakes and natural
disasters.
Graphical models:
These models use graphical representations to show the
conditional dependence between variables. They are commonly
35
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
36
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
The dataset is divided into two parts i.e feature matrix and
the response vector.
• Feature matrix contains all the vectors(rows) of dataset in which
each vector consists of the value of dependent features. In above
dataset, features are ‘Outlook’, ‘Temperature’, ‘Humidity’ and
‘Windy’.
• Response vector contains the value of class variable (prediction
or output) for each row of feature matrix. In above dataset, the
class variable name is ‘Play golf’.
37
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
38
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
39
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
40
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
41
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Logistic regression:
42
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
43
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
44
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
Simply this can be represented as the dot product of weight and bias.
z=mX+b
Logistic regression then applies the sigmoid function to z to convert
it into a probability between 0 and 1 which can be used to predict
the class.
Now we use the sigmoid function where the input will be z and we
find the probability between 0 and 1. i.e. predicted y.
45
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE
24AM442-MACHINE LEARNING Unit: 1
*******
46
Prof. Dr. K. SARAVANAN, Dept. of AI&DS, KCE