Machine Learning
Machine Learning
Machine Learning
CSN-382 (Lecture 5)
Dr. R. Balasubramanian
Professor
Department of Computer Science and Engineering
Mehta Family School of Data Science and Artificial Intelligence
Indian Institute of Technology Roorkee
Roorkee 247 667
[email protected]
https://faculty.iitr.ac.in/cs/bala/
R squared
2
Adjusted R squared
(1 - R2)(N - 1)
Adjusted R2 =1-
N-p-1
Where,
R2 = Sample R-squared
N = Total Sample size
p= No. of independent variables
3
Fluke Correlation
4
Linear Regression Assumptions
5
Let’s Answer some questions
6
Polynomial Regression
► Source: https://www.javatpoint.com/machine-learning-polynomial-regression
7
Non-Linear Regression
8
1975 Rs.540.00
1976 Rs.432.00
1977 Rs.486.00
1978 Rs.685.00
1979 Rs.937.00
1980 Rs.1,330.00
1981 Rs.1,800.00
1982 Rs.1,645.00
1983 Rs.1,800.00
1984 Rs.1,970.00
1985 Rs.2,130.00
9
1986 Rs.2,140.00
1987 Rs.2,570.00
1988 Rs.3,130.00
1989 Rs.3,140.00
1990 Rs.3,200.00
1991 Rs.3,466.00
1992 Rs.4,334.00
1993 Rs.4,140.00
1994 Rs.4,598.00
1995 Rs.4,680.00
1996 Rs.5,160.00
10
1997 Rs.4,725.00
1998 Rs.4,045.00
1999 Rs.4,234.00
2000 Rs.4,400.00
2001 Rs.4,300.00
2002 Rs.4,990.00
2003 Rs.5,600.00
2004 Rs.5,850.00
2005 Rs.7,000.00
2007 Rs.10,800.00
11
2008 Rs.12,500.00
2009 Rs.14,500.00
2010 Rs.18,500.00
2011 Rs.26,400.00
2012 Rs.31,050.00
2013 Rs.29,600.00
2014 Rs.28,006.50
2015 Rs.26,343.50
2016 Rs.28,623.50
2017 Rs.29,667.50
12
2018 Rs.31,438.00
2019 Rs.35,220.00
2020 Rs.48,651.00
2021 Rs.48,720.00
2022 (Till Today) Rs.52,690.00
13
14
15
Predicted Gold rate in 2032 and
2042
► 2032: Rs. 92390
16
Feature Engineering for ML
17
Feature Engineering
18
Four Processes of ML
► Feature Creation
► Transformations
► Feature Extraction
► Feature Selection.
19
Feature Creation
► Reference Papers:
20
Transformations
21
Feature Extraction
22
Feature Selection
23
Principal Component Analysis
(PCA)
► Purpose of PCA
To compress a lot of data into small data such that the compressed data
captures the essence the original data.
Dimensionality Reduction.
X-D into 3-D or 2-D.
► Not all the features in the dataset are relevant to the problem. Some
features are more relevant than others. The processing may be done
for the more relevant features only, without significant loss of the
information.
24
Intuition behind using PCA
► Let’s take an example of counting the minions that are scattered in
a 2-D space. Suppose we want to project them onto a 1-D line and
count.
Courtesy: https://www.youtube.com/channel/UCFJPdVHPZOYhSyxmX_C_Pew
25
Intuition behind using PCA
► How to choose the 1-D line?
Vertical: the minions will collide onto each other while projecting => ✖
At an angle: still, the possibility of collision is there. => ✖
Horizontal: least possibility of collision. Max. variation. => ✔
26
Principal Component Analysis
(PCA)
► In the minion’s example:
We reduced the dimensionality from 2 to 1.
The horizontal line would be the Principal Component.
27
Variance and Covariance
Variance
‘How spread out a given dataset is.’
Courtesy: https://www.youtube.com/watch?v=g-Hb26agBFg
28
Variance and Covariance
Covariance Covariance Matrix
‘Total variation of two variables
from their expected values.’
nn
C (ci , j )
where :
ci , j cov( Ai , A j )
A1 , ..., A n given n attributes.
• Covariance:
– Positive ⇒ both the variables increase together.
– Negative ⇒ as one variable increases, the other decreases.
– Zero ⇒ both the variables are independent of each other.
Courtesy: Smith, Lindsay I. A tutorial on principal components analysis. 2002.
29
Example on Covariance
Matrix
Covariance Matrix
cov( H , H ) cov( H , M )
cov( M , H ) cov( M , M )
var( H ) 104.5
104.5 var( M )
47.7 104.5
104.5 370
Eigenvalues and Eigenvectors
► Eigenvector is a direction. E.g., in the minion's example, the
eigenvectors were the directions of the lines - vertical,
horizontal or at an angle.
► Eigenvalue is a number telling how much variance is there
in the data in that direction. E.g., in the minion's example, the
eigenvalue is the number telling how spread out the minions
are on the line.
► Principal Component = Eigenvector with higher
eigenvalue.
► Every Eigenvector has a corresponding Eigenvalue.
► Eigenvectors & Eigenvalues that exist = No. of dimensions
(experimental observation).
31
Eigenvalues and Eigenvectors
► Let A be an nn matrix.
x is an eigenvector of A if:
Ax = x
is called the eigenvalue associated with x
32
Eigenvalues and Eigenvectors
Ques. Find the eigenvalues and eigenvectors of the matrix
4 6
A
3 5
Let us first derive the characteristic polynomial of A. We get:
4 6 1 0 4 6
A I 2
3 5 0 1 3 5
A I 2 (4 )(5 ) 18 2 2
By solving the above characteristic equation of A, we get the
eigenvalues, = 2 and = –1.
The corresponding eigenvectors are found by using these values of
in the equation (A – I)x = 0. There are many eigenvectors
corresponding to each eigenvalue.
Courtesy: http://webct.math.yorku.ca/file.php/230/eigenvalues-and-eigenvectors.ppt
33
Eigenvalues and Eigenvectors
• For = 2
We solve the equation (A – 2I)x = 0 for x.
The matrix (A – 2I) is obtained by subtracting 2 from the
diagonal elements of A. We get 6 6 x1
3 0
3 x2
This leads to the system of equations
6 x1 6 x2 0
3 x1 3 x2 0
giving x1 = –x2. The solutions to this system of equations are
x1 = –r, x2 = r, where r is a scalar. Thus the eigenvectors of A
corresponding to = 2 are nonzero vectors of the form
x1 1 1
v1 x2 r
x2 1 1
34
Eigenvalues and Eigenvectors
• For = –1
We solve the equation (A + 1I)x = 0 for x.
The matrix (A + 1I) is obtained by adding 1 to the
diagonal elements of A. We get 3 6 x1
3 0
6 x2
This leads to the system of equations
3 x1 6 x2 0
3x1 6 x2 0
Thus x1 = –2x2. The solutions to this system of equations are
x1 = –2s and x2 = s, where s is a scalar. Thus the eigenvectors
of A corresponding to = –1 are nonzero vectors of the form
x1 2 2
v 2 x2 s
x2 1 1
35
Dimensionality Reduction
► Reduce the data down to its basic components, chipping away any
unnecessary part.
► Assume, the minions data would’ve been represented in 3-D.
36
Dimensionality Reduction
► Clearly, EV3 is unnecessary. Reduce it and represent the data in
terms of EV1 and EV2.
► Re-arrange the axes along the Eigenvectors, rather than the
original 3-D axes.
37
Dimensionality Reduction
Advantages:
► Reduces redundant features,
► Solves multi-collinearity issue,
► Helps compressing the data and reduce the space requirements,
► Quickens the time required to perform the same computation.
Applications:
► Stock Market Analysis,
► Image and Text processing,
► Speech Recognition,
► Recommendation Engine, etc.
38
Example on performing PCA
Step 1: Get some data
Shown in Table 1.
Step2: Subtract the mean
• All the x values have x̅ subtracted
and y values have y̅ subtracted
from them. This produces a data
set whose mean is zero.
• Subtracting the mean makes
variance and covariance
calculation easier by simplifying
their equations. The variance and
co-variance values are not affected
by the mean value.
Courtesy: Smith, Lindsay I. A tutorial on principal components analysis. 2002.
39
Example
Step 3: Calculate the Covariance Matrix:
Solved similar to the example depicted on slide 8.
40
Example
41
Example
42
Example
Step 5: Choosing components and forming a feature vector:
43
Example
► Step 6: Deriving the new data
44
Example
Refer: slide 17
.69 1.31 .39 .09 1.29 .49 .19 .81 .31 .71
RowDataAdjust
.49 1.21 .99 .29 1.09 .79 .31 .81 .31 1.01
This gives original data in terms of chosen components (eigenvectors), i.e., along
these axes.
45
Example
46
Example
47
Thank You!
48