ランダムフォレスト② (決定木の数を変更)

ハイパーパラメータで生成する決定木の数を変更してみます。

決定木の数を変更

n_estimatorsの値を10→3に変更し、ランダムフォレストのモデルを構築します。

[Google Colaboratory]

1
rf_change_param = RandomForestRegressor(n_estimators=3, max_depth=20, random_state=0).fit(X_train,y_train)

[実行結果]

残差プロット (n_estimators=3)

予測値を算出し、残差プロットを表示します。

[Google Colaboratory]

1
2
3
4
5
6
7
y_train_pred = rf_change_param.predict(X_train)
y_test_pred = rf_change_param.predict(X_test)

y_train_pred = np.expand_dims(y_train_pred, 1)
y_test_pred = np.expand_dims(y_test_pred, 1)

residual_plot(y_train_pred, y_train, y_test_pred, y_test)

[実行結果]

決定木の数が10本のときとあまり変化は見られません。

精度評価スコア (n_estimators=3)

精度評価スコアを表示します。

[Google Colaboratory]

1
2
3
4
print("訓練データスコア")
get_eval_score(y_train,y_train_pred)
print("テストデータスコア")
get_eval_score(y_test,y_test_pred)

[実行結果]

決定木の数が10本(前回記事)のときとほぼ変わらない結果となっています。

基本的に決定木の数を増やしていくほど精度は上がっていきますが、その精度向上は徐々に収束していきます。

また、ランダムフォレストモデルでは対象データが膨大になったり、決定木の数や深さを増やすほど、処理時間が延びCPU負荷も増えますので注意が必要です。