Regularized linear regression

Dimensionality Reduction in Python

Jeroen Boeye

Head of Machine Learning, Faktion

Linear model concept

features to target

Dimensionality Reduction in Python

Creating our own dataset

x1 x2 x3
1.76 -0.37 -0.60
0.40 -0.24 -1.12
0.98 1.10 0.77
... ... ...
Dimensionality Reduction in Python

Creating our own dataset

x1 x2 x3
1.76 -0.37 -0.60
0.40 -0.24 -1.12
0.98 1.10 0.77
... ... ...

3 feature distributions

Dimensionality Reduction in Python

Creating our own dataset

3 feature distributions

Creating our own target feature:

$y = 20 + 5x_1 + 2x_2 + 0x_3 + error$

Dimensionality Reduction in Python

Linear regression in Python

from sklearn.linear_model import LinearRegression

lr = LinearRegression()
lr.fit(X_train, y_train)

# Actual coefficients = [5 2 0]
print(lr.coef_)
[ 4.95  1.83 -0.05]
# Actual intercept = 20
print(lr.intercept_)
19.8
Dimensionality Reduction in Python

Linear regression in Python

# Calculates R-squared
print(lr.score(X_test, y_test))
0.976
Dimensionality Reduction in Python

Linear regression in Python

from sklearn.linear_model import LinearRegression

lr = LinearRegression()
lr.fit(X_train, y_train)

# Actual coefficients = [5 2 0]
print(lr.coef_)
[ 4.95  1.83 -0.05]
Dimensionality Reduction in Python

Loss function: Mean Squared Error

predicted vs. actual

Dimensionality Reduction in Python

Loss function: Mean Squared Error

predicted vs. actual with MSE

Dimensionality Reduction in Python

Adding regularization

predicted vs. actual with MSE + formula

Dimensionality Reduction in Python

Adding regularization

predicted vs. actual with MSE + formula + annotated

Dimensionality Reduction in Python

Adding regularization

predicted vs. actual with MSE + formula + alpha

Dimensionality Reduction in Python

Lasso regressor

from sklearn.linear_model import Lasso

la = Lasso()
la.fit(X_train, y_train)

# Actual coefficients = [5 2 0]
print(la.coef_)
[4.07 0.59 0.  ]
print(la.score(X_test, y_test))
0.861
Dimensionality Reduction in Python

Lasso regressor

from sklearn.linear_model import Lasso

la = Lasso(alpha=0.05)
la.fit(X_train, y_train)

# Actual coefficients = [5 2 0]
print(la.coef_)
[ 4.91  1.76 0.  ]
print(la.score(X_test, y_test))
0.974
Dimensionality Reduction in Python

Let's practice!

Dimensionality Reduction in Python

Preparing Video For Download...