Regularizace

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

Příznaky: Jen několik

Dataset s malým počtem příznaků

Machine Learning with PySpark

Příznaky: Příliš mnoho

Dataset s velkým počtem příznaků

Machine Learning with PySpark

Příznaky: Vybrané

Výběr příznaků z datasetu s velkým počtem příznaků

Machine Learning with PySpark

Ztrátová funkce (opakování)

Lineární regrese minimalizuje MSE.

Ztrátová funkce střední kvadratické chyby

Machine Learning with PySpark

Ztrátová funkce s regularizací

Lineární regrese minimalizuje MSE.

Ztrátová funkce MSE s regularizačním členem

Přidejte regularizační člen závislý na koeficientech.

Machine Learning with PySpark

Regularizační člen

Do ztrátové funkce se přidá regularizační člen.

Regularizační člen může být:

  • Lasso — absolutní hodnota koeficientů
  • Ridge — čtverec koeficientů

Možná je také kombinace Lasso a Ridge regrese.

Síla regularizace je určena parametrem $\lambda$:

  • $\lambda = 0$ — žádná regularizace (standardní regrese)
  • $\lambda = \infty$ — úplná regularizace (všechny koeficienty jsou nulové)
Machine Learning with PySpark

Auta znovu

assembler = VectorAssembler(inputCols=[
    'mass', 'cyl', 'type_dummy', 'density_line', 'density_quad', 'density_cube'
], outputCol='features')
cars = assembler.transform(cars)
+-----------------------------------------------------------------------------+-----------+
|features                                                                     |consumption|
+-----------------------------------------------------------------------------+-----------+
|[1451.0,6.0,1.0,0.0,0.0,0.0,0.0,303.8743455497,63.63860639785,13.32745683724]|9.05       |
|[1129.0,4.0,0.0,0.0,1.0,0.0,0.0,244.2137140385,52.82580879050,11.42673778726]|6.53       |
|[1399.0,4.0,0.0,0.0,1.0,0.0,0.0,307.6753903672,67.66557958374,14.88136784335]|7.84       |
|[1147.0,4.0,0.0,1.0,0.0,0.0,0.0,264.1031545014,60.81122599620,14.00212433714]|7.84       |
+-----------------------------------------------------------------------------+-----------+
Machine Learning with PySpark

Auta: lineární regrese

Natrénujte (standardní) model lineární regrese na trénovacích datech.

regression = LinearRegression(labelCol='consumption').fit(cars_train)
# RMSE na testovacích datech
0.708699086182001

Prohlédněte si koeficienty:

regression.coefficients
DenseVector([-0.012, 0.174, -0.897, -1.445, -0.985, -1.071, -1.335, 0.189, -0.780, 1.160])
Machine Learning with PySpark

Auta: Ridge regrese

# alpha = 0 | lambda = 0.1 -> Ridge
ridge = LinearRegression(labelCol='consumption', elasticNetParam=0, regParam=0.1)
ridge.fit(cars_train)
# RMSE
0.724535609745491
# Koeficienty Ridge
DenseVector([ 0.001, 0.137, -0.395, -0.822, -0.450, -0.582, -0.806, 0.008,  0.029, 0.001])
# Koeficienty lineární regrese
DenseVector([-0.012, 0.174, -0.897, -1.445, -0.985, -1.071, -1.335, 0.189, -0.780, 1.160])
Machine Learning with PySpark

Auta: Lasso regrese

# alpha = 1 | lambda = 0.1 -> Lasso
lasso = LinearRegression(labelCol='consumption', elasticNetParam=1, regParam=0.1)
lasso.fit(cars_train)
# RMSE
0.771988667026998
# Koeficienty Lasso
DenseVector([   0.0,   0.0,    0.0, -0.056,    0.0,    0.0,    0.0, 0.026,    0.0,   0.0])
# Koeficienty Ridge
DenseVector([ 0.001, 0.137, -0.395, -0.822, -0.450, -0.582, -0.806, 0.008,  0.029, 0.001])
# Koeficienty lineární regrese
DenseVector([-0.012, 0.174, -0.897, -1.445, -0.985, -1.071, -1.335, 0.189, -0.780, 1.160])
Machine Learning with PySpark

Regularizace → jednoduchý model

Machine Learning with PySpark

Preparing Video For Download...