開始使用免費開始

受限與不受限的決策樹

在這個練習中,我們要重回上一章使用的 Pokémon 資料集。目標仍然是預測某個 Pokémon 是否為傳說等級。

你將建立兩個獨立的決策樹分類器。第一個模型中,你會設定參數 min_samples_leafmin_samples_split,但不設定最大深度,讓樹可以在沒有額外限制下完整展開。

第二個模型中,你會透過限制決策樹的深度來加入一些約束。最後比較兩個模型後,你會更理解「弱」學習器的概念。

本練習屬於課程

Python 的 Ensemble 方法

檢視課程

動手互動練習

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

# Build unrestricted decision tree
clf = ____
clf.fit(X_train, y_train)

# Predict the labels
pred = clf.predict(X_test)

# Print the confusion matrix
cm = confusion_matrix(y_test, pred)
print('Confusion matrix:\n', cm)

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