開始使用免費開始

匯出決策樹

在決策樹分類任務中,過度擬合通常來自過深的樹。從訓練集與測試集的正確率比較可以看出,你的結果出現過度擬合。從樹狀圖的視覺化也能得到同樣的結論。

在本練習中,你會把決策樹匯出成文字檔,之後即可用來進行視覺化。

本練習屬於課程

HR 分析:用 Python 預測員工流失

檢視課程

練習說明

  • sklearn.tree 子模組匯入 export_graphviz() 函式。
  • model 擬合到訓練資料。
  • 將視覺化匯出至 tree.dot 檔案。

動手互動練習

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

# Import the graphical visualization export function
from sklearn.____ import export_graphviz

# Apply Decision Tree model to fit Features to the Target
model.____(features_train,target_train)

# Export the tree to a dot file
____(model,"tree.____")
編輯並執行程式碼