更進一步的 bagging 模型
在探索了半導體資料後,現在來建立一個 bagging 分類器,根據輸入特徵來預測 'Pass/Fail' 標籤。
前處理後的資料集已放在你的工作區中,名稱為 uci_secom,我們也已為你建立好訓練集與測試集。
由於目標具有嚴重的類別不平衡,請在這裡使用 "balanced" 的邏輯斯回歸作為基底估計器。
我們也會透過設定參數 solver='liblinear' 來縮短 LogisticRegression 的計算時間,這個求解器比預設值更快。
本練習屬於課程
Python 的 Ensemble 方法
練習說明
- 建立一個邏輯斯回歸作為基底分類器,參數為:
class_weight='balanced'、solver='liblinear',以及random_state=42。 - 以該邏輯斯回歸為基底估計器建立 bagging 分類器,將最大特徵數指定為
10,並啟用 out-of-bag 分數。 - 列印 out-of-bag 分數,並與準確率比較。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build a balanced logistic regression
clf_lr = ____
# Build and fit a bagging classifier
clf_bag = ____(____, ____, ____, random_state=500)
clf_bag.fit(X_train, y_train)
# Evaluate the accuracy on the test set and show the out-of-bag score
pred = clf_bag.predict(X_test)
print('Accuracy: {:.2f}'.format(accuracy_score(y_test, pred)))
print('OOB-Score: {:.2f}'.format(____))
# Print the confusion matrix
print(confusion_matrix(y_test, pred))