Isolation Forestの始め方

Anomaly Detection in Python

Bekhruz (Bex) Tuychiev

Kaggle Master, Data Science Content Creator

調査データ

  • 回答者の一例:
    • 12歳
    • 身長160 cm
    • 体重190ポンド

帽子をかぶった12歳の少年のイラスト

Anomaly Detection in Python

多変量の異常

多変量の異常:

  • 2つ以上の属性を持つ
  • 各属性は必ずしも異常でない
  • 全属性を合わせて見たときに異常
Anomaly Detection in Python

決定木

5が素数か判定する決定木のルートと終端ノード

Anomaly Detection in Python

決定木

3階層まで成長した、5が素数か判定する決定木

Anomaly Detection in Python

Isolation Tree

iTree(アイソレーションツリー):

  • isolation treeの略
  • 決定木のランダム化版
  • 分割(枝分かれ)はランダム
  • ランダム分割は内点/外れ値の隙間で起きやすい
Anomaly Detection in Python

2次元データ例

9個の点を持つ2次元データ。うち2つは外れ値

Anomaly Detection in Python

iTreeの当て方

2次元平面で緑の線により2分割されたデータセット

2次元データに当てたiTreeのルートノード

Anomaly Detection in Python

iTreeの当て方

2本の直交する線で4分割された2次元データセット

2つのノードを持ち、2つの外れ値を見つけるiTree

Anomaly Detection in Python

iTreeの当て方

6色の線で多分割された2次元データセット

2次元データへiTreeを当てていく様子

Anomaly Detection in Python

点の分類方法

外れ値の基準:

  • ルートノードに近い
  • 分割回数が少ない

2次元データへiTreeを当てていく様子

Anomaly Detection in Python

米国Airbnbデータ

import pandas as pd

airbnb_df = pd.read_csv("airbnb.csv")
Anomaly Detection in Python

米国Airbnbデータ

airbnb_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 6 columns):
 #   Column                          Non-Null Count  Dtype  
 0   minimum_nights                  10000 non-null  int64  
 1   number_of_reviews               10000 non-null  int64  
 2   reviews_per_month               10000 non-null  float64
 3   calculated_host_listings_count  10000 non-null  int64  
 4   availability_365                10000 non-null  int64  
 5   price                           10000 non-null  int64  
dtypes: float64(1), int64(5)
Anomaly Detection in Python

fit_predict

from pyod.models.iforest import IForest

iforest = IForest() labels = iforest.fit_predict(airbnb_df) print(labels)
array([0, 0, 0, ..., 1, 0, 0])

Anomaly Detection in Python

外れ値の抽出

outliers = airbnb_df[labels == 1]

print(outliers.shape)
(1000, 6)
Anomaly Detection in Python

演習に進みましょう!

Anomaly Detection in Python

Preparing Video For Download...