訓練集-測試集切分
在本章中,你會繼續使用 ANSUR 資料集。在建立模型之前,應先決定要預測的特徵。本題中,你要預測性別。
你需要先從資料集中取出包含此特徵的欄,接著將資料切分為訓練集與測試集。訓練集用來訓練模型,測試集用來評估模型在未見過資料上的效能。
ansur_df 已經為你預先載入。
本練習屬於課程
Python 的降維
練習說明
- 從
sklearn.model_selection匯入train_test_split函式。 - 將
'Gender'欄指派給 y。 - 從 DataFrame 中移除
'Gender'欄,並將結果指派給X。 - 將測試集比例設為 30%,也就是進行 70% 訓練、30% 測試的資料切分。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import train_test_split()
from ____.____ import ____
# Select the Gender column as the feature to be predicted (y)
y = ansur_df[____]
# Remove the Gender column to create the training data
X = ansur_df.____(____, ____)
# Perform a 70% train and 30% test data split
X_train, X_test, y_train, y_test = ____(X, y, ____=____)
print(f"{X_test.shape[0]} rows in test set vs. {X_train.shape[0]} in training set, {X_test.shape[1]} Features.")