Focus on solving real business tasks: Your First Implementation of Machine Learning. #Coldstart.ml
ビジネスにおける機械学習の活用事例
Menu
- Why can we use Regression for Prediction?
- A process of ML implementation
- Validation Metrics in Regression Tasks
- 実装: Boston House Price Prediction
- Conclusion & References
— — — — —
1. Why can we use Regression for Prediction?
なぜ線形回帰か
Concept
考え方
最終的に決めたい判断 y がある。ユーザーのデモグラフィック情報やサービス内での行動ログを判断材料 x として、yを予測しよう
回帰問題と分類問題どちらにも適応できる
分類問題
最終的に求めたい答えがカテゴリのとき
年齢,性別,年収…という特徴(x)をもつユーザーAが債務破綻に陥る
- 特徴(x):x1, x2, x3 ……
- 債務破綻率(y):
- 前提:y > 0.2 の時、クレジットカードを発行する
回帰問題
最終的に求めたい答えが数値の時
数学的な解釈
y = mx + c
- m = slope 傾き
- c = intercept 切片
切片:特徴がないときにも存在するからバイアス
デフォルトで値が設定されている
予測(ライン)と実際値の誤差が最小になる線を引く
(x, y)
x = [ 1,2,3,4,5 ] ( Ave(x) = 3 )
y = [ 2,4,6,4,8 ] ( Ave(y) = 4.8 )
傾きm:xの移動に対してyがどう変化していくか(重み)
切片c:xに値がない時(x=0)のyの初期値(バイアス)
m=( x-A(x) )( y-A(y) )/( x-A(x) )**2
AとBの誤差を縮めたい 全体最適にするため誤差の輪が最小になる公式を作る
- A: フォーミュラにxnをぶち込んで出る戻り値y
- B: xn実際の戻り値y
距離を相対化する方法:2乗 or 絶対値
トレーニングデータから公式を作る(m,cを決める)
テストデータで分類または回帰の精度を検証する
精度検証に使われる指標
— — — — —
2. A process of ML implementation
機械学習ワークフロー
- yを決める
- xを決める(次元削除, PCA)
- データをトレーニング/テスト用に分割する
- トレーニングする(How: 最小誤差)
- テストする(How: 評価指標)
- 実装して検証する
— — — — —
3. Validation Metrics in Regression Tasks
回帰タスクにおける評価指標
- R2 (R-squared, coefficient of determination) 決定係数
- MAE (Mean Absolute Error) 平均絶対誤差
- MSE (Mean Squared Error) 平均絶対誤差
- RMSE (Root Mean Squared Error) 平均平方絶対誤差
— — — — —
4. 実装: Boston House Price Prediction
0. Data Preparation: “Boston House Price dataset” which is scikit learn built-in dataset
1. Define Target Variable as y and Features as X 目的/説明変数の定義
- Target Variable: House price
- Features: average number of rooms, crime rate of district, property tax rate…. (we have 14 features)
This means we are going to predict { y: House Price } by depending on { X: Features }.
2. Feature Selection 特徴量の取捨選択
We need to think if we really need each feature for prediction. In machine learning, we wanna use only meaning full features for Target.
There are two ways to decide features:
- Choosing by human hands: troublesome and risky
- Dimensionality Reduction by the statistical methods: PCA, SVD etc…
About 2.Dimensionality Reduction, it will be a huge topic. So this time I will choose features manually for feeding to model depending on the degree of correlation. You can visualize correlations by plotting distribution like below.
# Using seaborn as visualization library
sns.jointplot(x="Crime Rate",y="House Price",data=df,kind="reg")
sns.jointplot(x="House Age",y="House Price",data=df,kind="reg")
3. Feature Normalization 特徴量の標準化
We need this process for improving our prediction accuracy because of statistical reason.
As you can see above graphs, every feature has a different scale of their value. For instance, {crime rate: 0~0.100 }, {house age: 1~100 } and so on.
If it goes on like this, the largest value affects more on our prediction model. To get a better prediction, we can treat all features fairly by Normalization.
# Scikitlearn has functions for Normalization
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()# We will Normalize only features, not Target variable
scaler.fit(X)
scaler.transform(X)
List of Normalization 主要なスケール標準化手法 :
MinMaxScaler, StandardSclare、RobustSclaer、Normalize…
4. Split data for training and testing machine learning model データ分割
from sklearn.model_selection import train_test_splitsklearn公式ドキュメント
5. Training and Testing your data 学習とテスト
# Import Linear Regression Model from scikit learn
from sklearn.linear_model import LinearRegression# Building prediction model "lr" by training data X, y
lr = LinearRegression().fit(X_train, y_train)# y_predict is y which feed X_test to lr
y_predict = lr.predict(X_test)# Evaluate test accuracy by test data X, y
lr.score(X_test, y_test)
6. Evaluate model by Metrics for Regression Problem 評価
To identify the accuracy of House Price prediction model, we measure each distance between prediction model by training data and actual value from test data.
You can measure the distance(=accuracy) in several ways, and we have handy functions for it in scikit-learn.
# MAE (Mean Absolute Error)
from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_test, y_predict)
>>> output: 3.2# MSE (Mean Squared Error)
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, y_predict)
>>> output: 24.90# RMSE (Root Mean Squared Error)
import numpy as np
np.sqrt(mean_squared_error(y_test, y_predict))
>>> output: 4.90# R2 (coefficient of determination)
from sklearn.metrics import r2_score
r2_score(y_test, y_predict)
>>> output: 0.70
— — — — —
Conclusion & References
- あるタスクに対して何を特徴とするかがまず大切
- 特徴を定性的にジャッジする方法と定量的に最適化する方法がある
- 上記の最小化=モデルの最適化という考え