許多顆決策樹組成的森林
在這個練習中,你將練習使用以自助抽樣(bootstrapping)為基礎的決策樹,也就是 Random Forest。和上一個練習一樣,你會將它的準確率與使用交叉驗證調整超參數後的模型做比較。
這次你還會調一個額外的超參數 max_features,用來控制模型在每次分裂時要考慮多少個特徵。當未特別設定時,預設為 auto。面試時要記得,Decision Tree 預設會考慮所有特徵;而 Random Forest 通常考慮特徵數量的平方根。
特徵矩陣 X、目標標籤 y,以及 sklearn.model_selection 中的 train_test_split 都已經為你匯入。
本練習屬於課程
用 Python 練習機器學習面試題
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import modules
from sklearn.ensemble import ____
from sklearn.metrics import accuracy_score
# Train/test split
X_train, X_test, y_train, y_test = train_test_split(____, ____, test_size=0.30, random_state=123)
# Instantiate, Fit, Predict
loans_rf = ____()
loans_rf.____(____, ____)
y_pred = loans_rf.____(____)
# Evaluation metric
print("Random Forest Accuracy: {}".format(____(____,____)))