Introduction To ML Linear Regression
Introduction To ML Linear Regression
Machine Learning
1
Introduction to machine learning
“[Machine Learning is the] field of study that gives computers the ability to
learn without being explicitly programmed.” Arthur Samuel 1959:
2
Introduction to machine learning
2. The ability to do the tasks come from the underlying model which is the result of
the learning process. Sometimes the ability comes from an mathematical
algorithm
3. The model generated represents behaviour of the processes that were earlier
performed before machine learning
4. The model is generated from huge volume of data, huge both in breadth and
depth reflecting the real world in which the processes are performed
5. The more representative data is of the real world, the better the model would be.
The challenge is how to make it a true representative
3
Introduction to machine learning
4
Introduction to machine learning
When is machine learning useful ?
1. Cannot express our knowledge about patterns as a program. For e.g. Character
recognition or natural language processing
2. Do not have an algorithm to identify a pattern of interest. For e.g. In spam mail detection
4. Too many permutations and combinations possible. For e.g. Genetic code mapping
5
Introduction to machine learning
1. Fraud detection
2. Sentiment analysis
6
Introduction to machine learning
Machine Learning & Data Science
2. Data science is the process of applying science and domain expertise to data to
extract useful information from data.
3. It includes application of all the statistical and mathematical tools and techniques to
glean out the useful information from data using machine learning
7
Introduction to machine learning
Machine Learning Pre-requisites
8
Introduction to machine learning
9
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
1. A data set representing the real world, is a collection attributes that define an
entity
Attributes / Dimensions
10
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
Sugar
BP level
Heart healthy
Potential heart ailments
11
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
1. Position of a point in
space is defined with
respect to the origin
Sugar
BP level
Heart healthy
Potential heart ailments
12
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
Sugar
5. But multiple planes can do
the job. Each representing
an alternate hypothesis
13
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
ax + by + cz = d
Sugar
the color i.e. healthy or
ailing heart
BP level
Heart healthy
Potential heart ailments
14
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
Sugar
11. The data point will be
placed above or below the
plane based on d
ax + by + cz = d, BP level
Heart healthy
Potential heart ailments
15
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
Sugar
ax + by + cz = d, BP level
Heart healthy
Potential heart ailments
16
Introduction to machine learning
Machine learning happens in mathematical space / feature space:
Sugar
ax + by + cz = d,
17
Introduction to machine learning
Introduction to Supervised
Machine Learning
18
Introduction to machine learning
Characteristics of Supervised Machine Learning -
c. The model thus generated is used to make predictions about future instances
where the predictor feature values are known but the target / class value is
unknown
a. E.g.-1 building model to predict the re-sale value of a car based on its current mileage,
age, color etc.
b. E.g.-2 Predicting the final year scores based on student performance in previous
years.
19
Introduction to machine learning
Data Science Machine Learning Steps -
Identify Data Identify what type of data, source of data and how to ingest data into
your system. Need domain expertise and lateral thinking
Required
Pre-process Address data quality issues such as missing values, outliers, data
Data pollution etc. Establish veracity of the data. Select attributes for model,
Need domain expertise
Create
Split the data into training set and test set. Generally
training & 70:30 ratio is used
test set
Select
Select appropriate algorithm/s to model. For e.g. Random
appropriate Forest, K Nearest Neighbors etc. Depends on data
algorithm/s
Deploy at scale
OK?
No Yes
Productionize
& calibrate
20
Introduction to machine learning
Linear Regression
21
Introduction to machine learning
Linear Regression Models -
b. The term "linear" in the name “linear regression” refers to the fact that the
method models data with linear combination of the explanatory variables.
d. In the case of linear regression with a single explanatory variable, the linear
combination used in linear regression can be expressed as:
e. In its most basic form fits a straight line to the response variable. The model is
designed to fit a line that minimizes the squared differences (also called errors
or residuals.).
22
Introduction to machine learning
Linear Regression Models -
23
Introduction to machine learning
Linear Regression Models -
d. Coefficient of relation - Pearson’s coefficient p(x,y) = Cov(x,y) / ( stnd Dev (x) X stnd
Dev (y) )
e. Generating linear model for cases where r is near 0, makes no sense. The model will
not be reliable. For a given value of X, there can be many values of Y! Nonlinear
models may be better in such cases
24
Introduction to machine learning
Linear Regression Models (Recap) -
f. Coefficient of relation - Pearson’s coefficient p(x,y) = Cov(x,y) / ( stnd Dev (x) X stnd
Dev (y) )
- ve +ve
quad quad
+ve - ve
quad quad
=0
>0
http://www.socscistatistics.com/tests/pearson/Default2.aspx
25
Introduction to machine learning
Linear Regression Models -
g. Given Y = f(x) and the scatter plot shows apparent correlation between X and Y
Let’s fit a line into the scatter which shall be our model
h. But there are infinite number of lines that can be fit in the scatter. Which one
should we consider as the model?
j. Gradient descent
methods use partial
derivatives on the
parameters (slope and
intercept) to minimize
sum of squared errors
26
Introduction to machine learning
Linear Regression Models (Recap) -
k. Whichever line we consider as the model, it will not pass through all the points.
l. The distance between a point and the line (drop a line vertically (shown in
yellow)) is the error in prediction
m. That line which gives least sum of squared errors is considered as the best line
Error = (T – (mx + C)
Sum of all errors can cancel
out and give 0
27
Introduction to machine learning
Linear Regression Models -
n. Coefficient of determinant – determines the fitness of a linear model. The closer the
points get to the line, the R^2 (coeff of determinant) tends to 1, the better the model is
Ybar
Xbar
28
Introduction to machine learning
Linear Regression Models -
o. Coefficient of determinant (Contd…)
I. There are a variety of errors for all those points that don’t fall exactly on the line.
II. It is important to understand these errors to judge the goodness of fit of the model i.e. How
representative the model is likely to be in general
III. Let us look at point P1 which is one of the given data points and associated errors due to
the model
1. P1 – Original y data point for given x
Xbar x
29
Introduction to machine learning
Linear Regression Models -
SSE
2. Hence SSR should be equal to SST i.e.
SST SSR/SST should be 1.
P2
SSR
Ybar 3. Poor fit will mean large SSE. SSR/SST will
be close to 0
30
Introduction to machine learning
Linear Regression Models -
Point B
Point B
Point A Point A
In case of point “A”, the line explains the variance of the point
Whereas point “B” the is a small area (light grey) which the line does not represent.
31
Introduction to machine learning
Linear Regression Model -
Advantages –
1. Simple to implement and easier to interpret the outputs coefficients
Disadvantages -
1. Assumes a linear relationships between dependent and independent variables. That
is, it assumes there is a straight-line relationship between them
2. Outliers can have huge effects on the regression
3. Linear regression assume independence between attributes
4. Linear regression looks at a relationship between the mean of the dependent variable
and the independent variables.
5. Just as the mean is not a complete description of a single variable, linear regression
is not a complete description of relationships among variables
6. Boundaries are linear
32
Introduction to machine learning
Linear Regression Model -
The dataset has 9 attributes listed below that define the quality
1. mpg: continuous
2. cylinders: multi-valued discrete
3. displacement: continuous
4. horsepower: continuous
5. weight: continuous
6. acceleration: continuous
7. model year: multi-valued discrete
8. origin: multi-valued discrete
9. car name: string (unique for each instance)
33