回帰

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

消費量 vs 質量:散布図

燃費と質量の散布図

Machine Learning with PySpark

消費量 vs 質量:フィット

燃費と質量の散布図(線形フィット)

Machine Learning with PySpark

消費量 vs 質量:別のフィット

燃費と質量の散布図(線形フィットと別案)

Machine Learning with PySpark

消費量 vs 質量:残差

燃費と質量の散布図(線形フィットと残差)

Machine Learning with PySpark

損失関数

 

 

二乗誤差平均(MSE)の損失関数

MSE = Mean Squared Error(平均二乗誤差)

Machine Learning with PySpark

損失関数:観測値

 

 

二乗誤差平均(MSE)の損失関数

$y_i$ — 観測値

Machine Learning with PySpark

損失関数:モデル値

 

 

二乗誤差平均(MSE)の損失関数

$y_i$ — 観測値

$\hat{y_i}$ — 予測値(モデル)

Machine Learning with PySpark

損失関数:平均

 

 

二乗誤差平均(MSE)の損失関数

$y_i$ — 観測値

$\hat{y_i}$ — 予測値(モデル)

Machine Learning with PySpark

予測子の結合

mass、cyl、type_dummyでconsumptionを予測します。

予測子を1列に統合します。

+------+---+-------------+----------------------------+-----------+
|mass  |cyl|type_dummy   |features                    |consumption|
+------+---+-------------+----------------------------+-----------+
|1451.0|6  |(5,[0],[1.0])|(7,[0,1,2],[1451.0,6.0,1.0])|9.05       |
|1129.0|4  |(5,[2],[1.0])|(7,[0,1,4],[1129.0,4.0,1.0])|6.53       |
|1399.0|4  |(5,[2],[1.0])|(7,[0,1,4],[1399.0,4.0,1.0])|7.84       |
|1147.0|4  |(5,[1],[1.0])|(7,[0,1,3],[1147.0,4.0,1.0])|7.84       |
|1111.0|4  |(5,[3],[1.0])|(7,[0,1,5],[1111.0,4.0,1.0])|9.05       |
+------+---+-------------+----------------------------+-----------+
Machine Learning with PySpark

回帰モデルの構築

from pyspark.ml.regression import LinearRegression

regression = LinearRegression(labelCol='consumption')

cars_train(学習データ)で学習します。

regression = regression.fit(cars_train)

cars_test(テストデータ)で予測します。

predictions = regression.transform(cars_test)
Machine Learning with PySpark

予測の確認

+-----------+------------------+
|consumption|prediction        |
+-----------+------------------+
|7.84       |8.92699470743403  |
|9.41       |9.379295891451353 |
|8.11       |7.23487264538364  |
|9.05       |9.409860194333735 |
|7.84       |7.059190923328711 |
|7.84       |7.785909738591766 |
|7.59       |8.129959405168547 |
|5.11       |6.836843743852942 |
|8.11       |7.17173702652015  |
+-----------+------------------+

予測値と実測値の散布図

Machine Learning with PySpark

RMSEの計算

from pyspark.ml.evaluation import RegressionEvaluator

# Find RMSE (Root Mean Squared Error)
RegressionEvaluator(labelCol='consumption').evaluate(predictions)
0.708699086182001

RegressionEvaluatorは次の指標も算出可能です:

  • mae(平均絶対誤差)
  • r2($R^2$)
  • mse(平均二乗誤差)
Machine Learning with PySpark

消費量 vs 質量:切片

モデルの切片を示すプロット

Machine Learning with PySpark

切片の確認

regression.intercept
4.9450616833727095

これは(仮想的に)次の場合の燃料消費量です:

  • mass = 0
  • cyl = 0
  • 車種が 'Van'
Machine Learning with PySpark

消費量 vs 質量:傾き

モデルの傾きを示すプロット

Machine Learning with PySpark

係数の確認

regression.coefficients
DenseVector([0.0027, 0.1897, -1.309, -1.7933, -1.3594, -1.2917, -1.9693])
mass        0.0027
cyl         0.1897

Midsize    -1.3090
Small      -1.7933
Compact    -1.3594
Sporty     -1.2917
Large      -1.9693
Machine Learning with PySpark

数値予測のための回帰

Machine Learning with PySpark

Preparing Video For Download...