實例化模型
在接下來的一組練習中,你將診斷迴歸樹的偏差與變異問題。你在本題要定義的迴歸樹,將使用 auto 資料集的所有可用特徵,來預測汽車的每加侖英里數(mpg)。
我們已經處理好資料,並在你的工作環境中載入了特徵矩陣 X 與陣列 y。另外,也已從 sklearn.tree 匯入了 DecisionTreeRegressor 類別。
本練習屬於課程
Machine Learning with Tree-Based Models in Python
練習說明
- 從
sklearn.model_selection匯入train_test_split。 - 將資料切分為 70% 的訓練集與 30% 的測試集。
- 建立一個
DecisionTreeRegressor,max_depth為 4,且將min_samples_leaf設為 0.26。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import train_test_split from sklearn.model_selection
____
# Set SEED for reproducibility
SEED = 1
# Split the data into 70% train and 30% test
X_train, X_test, y_train, y_test = ____(____, ____, test_size=____, random_state=SEED)
# Instantiate a DecisionTreeRegressor dt
dt = ____(____=____, ____=____, random_state=SEED)