開始使用免費開始

Bagging:使用 scikit-learn 的作法

現在把 scikit-learnBaggingClassifier 用在 Pokémon 資料集上。

你用自行實作的 bagging 集成模型得到約 0.63 的 F1 分數。

BaggingClassifier() 能否超越它?來試試看吧!

本練習屬於課程

Python 的 Ensemble 方法

檢視課程

練習說明

  • 建立基礎模型 clf_dt:一棵「受限」的決策樹,最大深度為 4。
  • 以該決策樹作為基底估計器,建立一個含 21 個估計器的 bagging 分類器。
  • 預測測試集的標籤。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Instantiate the base model
clf_dt = ____

# Build the Bagging classifier
clf_bag = ____(____, ____, random_state=500)

# Fit the Bagging model to the training set
clf_bag.fit(X_train, y_train)

# Predict the labels of the test set
pred = ____

# Show the F1-score
print('F1-Score: {:.3f}'.format(f1_score(y_test, pred)))
編輯並執行程式碼