切分員工資料
在分析中,過度擬合是常見問題。當模型在開發時使用的資料集上表現良好,但無法在其之外的情境泛化時,就會發生過度擬合。
為了確保模型具有泛化能力,通常會進行訓練/測試切分:你會用訓練樣本來開發模型,之後再用測試樣本進行驗證。
在本練習中,你將把 target 與 features 依序切分為訓練集與測試集,比例為 75%/25%。
本練習屬於課程
HR 分析:用 Python 預測員工流失
練習說明
- 從
sklearn.model_selection模組匯入train_test_split - 使用
train_test_split()將資料集切分為訓練集與測試集 - 將 25% 的觀測值分配到測試集
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the function for splitting dataset into train and test
from sklearn.model_selection import ____
# Use that function to create the splits both for target and for features
# Set the test sample to be 25% of your observations
target_train, target_test, features_train, features_test = ____(target,features,____=0.25,random_state=42)