Isolation Forest 하이퍼파라미터 튜닝

Python으로 배우는 이상치 탐지

Bekhruz (Bex) Tuychiev

Kaggle Master, Data Science Content Creator

Contamination 튜닝

  • 정해진 튜닝 방법은 없음
  • 다음에 의존해야 함:
    • 직관
    • EDA 인사이트
    • 도메인 지식
    • 비즈니스 기대치
Python으로 배우는 이상치 탐지

설문 예시

  • 유사 설문 조사 검토
  • 최하·최상위 비율 파악
  • 값 임의 선택보다 조사 기반이 더 좋음
Python으로 배우는 이상치 탐지

Big Mart 매출 데이터

import pandas as pd

big_mart = pd.read_csv("big_mart_sales.csv")
big_mart.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7060 entries, 0 to 7059
Data columns (total 5 columns):
 #   Column            Non-Null Count  Dtype  
 0   weight            7060 non-null   float64
 1   fat_content       7060 non-null   object 
 2   type              7060 non-null   object 
 3   max_retail_price  7060 non-null   float64
 4   sales             7060 non-null   float64
dtypes: float64(3), object(2)
Python으로 배우는 이상치 탐지

범주형 인코딩

big_mart = pd.get_dummies(big_mart)
   weight  max_retail_price      sales  fat_content_low_fat    fat_content_regular
0    9.30          249.8092  3735.1380                    1                      0
1    5.92           48.2692   443.4228                    0                      1
2   17.50          141.6180  2097.2700                    1                      0
3   19.20          182.0950   732.3800                    0                      1
4    8.93           53.8614   994.7052                    1                      0
Python으로 배우는 이상치 탐지

evaluate_outlier_classifier

def evaluate_outlier_classifier(model, data):
    # 레이블 얻기
    labels = model.fit_predict(data)

    # 정상치 반환
    return data[labels == 0]
Python으로 배우는 이상치 탐지

evaluate_regressor

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import root_mean_squared_error
Python으로 배우는 이상치 탐지

evaluate_regressor

def evaluate_regressor(inliers):
    X = inliers.drop("sales", axis=1) 
    y = inliers[['sales']]


X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=10) lr = LinearRegression() lr.fit(X_train, y_train)
preds = lr.predict(X_test) rmse = root_mean_squared_error(y_test, preds) return round(rmse, 3)
Python으로 배우는 이상치 탐지

Contamination 튜닝

contaminations = [0.05, 0.1, 0.2, 0.3]
scores = dict()


for c in contaminations: # 현재 c로 IForest 생성 iforest = IForest(contamination=c, random_state=10)
# 현재 IForest로 정상치 추출 inliers = evaluate_outlier_classifier(iforest, big_mart)
# RMSE 계산 후 scores에 저장 scores[c] = evaluate_regressor(inliers)
Python으로 배우는 이상치 탐지

출력 확인

print(scores)
{0.05: 1148.555, 0.1: 1147.48, 0.2: 1082.307, 0.3: 1029.33}
Python으로 배우는 이상치 탐지

다중 하이퍼파라미터 튜닝

estimators = [100, 200, 300,]
max_samples = [0.6, 0.8, 1]
scores = dict()
Python으로 배우는 이상치 탐지

데카르트 곱

from itertools import product

list(product(estimators, max_samples))
[(100, 0.6),
 (100, 0.8),
 (100, 1),
 (200, 0.6),
 (200, 0.8),
 (200, 1),
 (300, 0.6),
 (300, 0.8),
 (300, 1)]
Python으로 배우는 이상치 탐지

루프 내부

estimators = [100, 200, 300,]
max_samples = [0.6, 0.8, 1]
scores = dict()


for e, m in product(estimators, max_samples):
# IForest 인스턴스화 iforest = IForest(n_estimators=e, max_samples=m, contamination=.3)
# 현재 IForest로 정상치 추출 inliers = evaluate_outlier_classifier(iforest, big_mart) # RMSE 계산 후 scores에 저장 scores[(e, m)] = evaluate_regressor(inliers)
Python으로 배우는 이상치 탐지

출력 확인

print(scores)
{(100, 0.6): 959.398,
 (100, 0.8): 986.056,
 (100, 1): 1195.875,
 (200, 0.6): 947.628,
 (200, 0.8): 933.115,
 (200, 1): 1195.875,
 (300, 0.6): 949.412,
 (300, 0.8): 935.962,
 (300, 1): 1195.875}
Python으로 배우는 이상치 탐지

병렬 실행

# n_jobs=-1로 더 빠른 계산
iforest = IForest(n_estimators=1000, n_jobs=-1)

iforest.fit(big_mart)
Python으로 배우는 이상치 탐지

Ayo berlatih!

Python으로 배우는 이상치 탐지

Preparing Video For Download...