アルゴリズムの重みを調整する

Pythonで学ぶ不正検知

Charlotte Werger

Data Scientist

バランス化された重み

model = RandomForestClassifier(class_weight='balanced')
model = RandomForestClassifier(class_weight='balanced_subsample')
model = LogisticRegression(class_weight='balanced')
model = SVC(kernel='linear', class_weight='balanced', probability=True)
Pythonで学ぶ不正検知

不正検知のハイパーパラメータ調整

model = RandomForestClassifier(class_weight={0:1,1:4},random_state=1)

model = LogisticRegression(class_weight={0:1,1:4}, random_state=1)
model = RandomForestClassifier(n_estimators=10, 
                               criterion='gini', 
                               max_depth=None, 
                               min_samples_split=2, 
                               min_samples_leaf=1, 
                               max_features='auto',
                               n_jobs=-1, 
                               class_weight=None)
Pythonで学ぶ不正検知

GridSearchCV の使用

from sklearn.model_selection import GridSearchCV

# パラメータグリッドを作成 param_grid = { 'max_depth': [80, 90, 100, 110], 'max_features': [2, 3], 'min_samples_leaf': [3, 4, 5], 'min_samples_split': [8, 10, 12], 'n_estimators': [100, 200, 300, 1000] }
# 使用するモデルを定義 model = RandomForestRegressor()
# グリッドサーチを作成 grid_search_model = GridSearchCV(estimator = model, param_grid = param_grid, cv = 5, n_jobs = -1, scoring='f1')
Pythonで学ぶ不正検知

GridSearchCV で最良モデルを見つける

# 学習データでグリッドサーチを学習
grid_search_model.fit(X_train, y_train)

# 最適パラメータを取得 grid_search_model.best_params_
{'bootstrap': True,
 'max_depth': 80,
 'max_features': 3,
 'min_samples_leaf': 5,
 'min_samples_split': 12,
 'n_estimators': 100}
# best_estimator の結果を取得
grid_search.best_estimator_
grid_search.best_score_
Pythonで学ぶ不正検知

演習に進みましょう!

Pythonで学ぶ不正検知

Preparing Video For Download...