Linear regression

Foundations of Probability in Python

Alexander A. Ramírez M.

CEO @ Synergy Vision

Linear functions

Linear function plot

Foundations of Probability in Python

Linear function parameters

Linear function plot

$$ y = \color{red}{slope}*x + \color{blue}{intercept} $$

Foundations of Probability in Python

Linear function with random perturbations

Linear function with random perturbations plot

$$ y = slope*x + intercept + \color{red}{random\_number} $$

Foundations of Probability in Python

Start from the data and find a model that fits

Hours of study vs score plot

Foundations of Probability in Python

What model will fit the data?

Fitting data with constant, linear, quadratic and logarithmic models

What would be the criteria to determine which is the best model?

Foundations of Probability in Python

What model will fit the data? (Cont.)

Linear model animation plot

Foundations of Probability in Python

Residuals plot

Foundations of Probability in Python

Residuals plot and minimizing error

Foundations of Probability in Python

Probability and statistics in action

Linear model residuals plot

Foundations of Probability in Python

Calculating linear model parameters

# Import LinearRegression
from sklearn.linear_model import LinearRegression
# sklearn linear model
model = LinearRegression()
model.fit(hours_of_study, scores)
# Get parameters
slope = model.coef_[0]
intercept = model.intercept_
# Print parameters
print(slope, intercept)
(1.496703900384545, 52.44845266434719)
Foundations of Probability in Python

Predicting scores based on hours of study

# Score prediction
score = model.predict(np.array([[15]]))
print(score)
[74.89901117]
Foundations of Probability in Python

Plotting the linear model

$$ $$

import matplotlib.pyplot as plt
plt.scatter(hours_of_study, scores)
plt.plot(hours_of_study_values, model.predict(hours_of_study_values))
plt.show()
Foundations of Probability in Python

Linear model fit plot

Foundations of Probability in Python

Let's practice with linear models

Foundations of Probability in Python

Preparing Video For Download...