Focus on solving real business tasks: Your First Implementation of Machine Learning. #Coldstart.ml

ビジネスにおける機械学習の活用事例

Akira Takezawa
Coldstart.ml
5 min readMar 18, 2019

--

Menu

  1. Why can we use Regression for Prediction?
  2. A process of ML implementation
  3. Validation Metrics in Regression Tasks
  4. 実装: Boston House Price Prediction
  5. Conclusion & References

— — — — —

1. Why can we use Regression for Prediction?

なぜ線形回帰か

Concept

考え方

最終的に決めたい判断 y がある。ユーザーのデモグラフィック情報やサービス内での行動ログを判断材料 x として、yを予測しよう

回帰問題と分類問題どちらにも適応できる

分類問題

最終的に求めたい答えがカテゴリのとき

年齢,性別,年収…という特徴(x)をもつユーザーAが債務破綻に陥る

  • 特徴(x):x1, x2, x3 ……
  • 債務破綻率(y):
  • 前提:y > 0.2 の時、クレジットカードを発行する

回帰問題

最終的に求めたい答えが数値の時

数学的な解釈

https://hackernoon.com/visualizing-linear-regression-with-pytorch-9261f49edb09

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

機械学習ワークフロー

Photo by Jo Szczepanska on Unsplash
  1. yを決める
  2. xを決める(次元削除, PCA)
  3. データをトレーニング/テスト用に分割する
  4. トレーニングする(How: 最小誤差)
  5. テストする(How: 評価指標)
  6. 実装して検証する

— — — — —

3. Validation Metrics in Regression Tasks

回帰タスクにおける評価指標

Photo by Fleur Treurniet on Unsplash
  1. R2 (R-squared, coefficient of determination) 決定係数
  2. MAE (Mean Absolute Error) 平均絶対誤差
  3. MSE (Mean Squared Error) 平均絶対誤差
  4. RMSE (Root Mean Squared Error) 平均平方絶対誤差

— — — — —

4. 実装: Boston House Price Prediction

Photo by Jesse Roberts on Unsplash

0. Data Preparation: “Boston House Price dataset” which is scikit learn built-in dataset

DataFrame of Boston House Price 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:

  1. Choosing by human hands: troublesome and risky
  2. 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.

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.

List of Normalization 主要なスケール標準化手法 :

MinMaxScaler, StandardSclare、RobustSclaer、Normalize…

4. Split data for training and testing machine learning model データ分割

5. Training and Testing your data 学習とテスト

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.

— — — — —

Conclusion & References

  • あるタスクに対して何を特徴とするかがまず大切
  • 特徴を定性的にジャッジする方法と定量的に最適化する方法がある
  • 上記の最小化=モデルの最適化という考え

References

--

--

Akira Takezawa
Coldstart.ml

Data Scientist, Rakuten / a discipline of statistical causal inference and time-series modeling / using Python and Stan, R / MLOps is my current concern