Geregulariseerde lineaire regressie

Dimensionality Reduction in Python

Jeroen Boeye

Head of Machine Learning, Faktion

Concept lineair model

features naar target

Dimensionality Reduction in Python

Eigen dataset maken

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

Eigen dataset maken

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

3 featureverdelingen

Dimensionality Reduction in Python

Eigen dataset maken

3 featureverdelingen

Onze doelfeature maken:

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

Dimensionality Reduction in Python

Lineaire regressie 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

Lineaire regressie in Python

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

Lineaire regressie 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

Lossfunctie: Mean Squared Error

voorspeld vs. echt

Dimensionality Reduction in Python

Lossfunctie: Mean Squared Error

voorspeld vs. echt met MSE

Dimensionality Reduction in Python

Regularisatie toevoegen

voorspeld vs. echt met MSE + formule

Dimensionality Reduction in Python

Regularisatie toevoegen

voorspeld vs. echt met MSE + formule + annotaties

Dimensionality Reduction in Python

Regularisatie toevoegen

voorspeld vs. echt met MSE + formule + 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

Laten we oefenen!

Dimensionality Reduction in Python

Preparing Video For Download...