训练-测试集拆分
在本章中,您将继续使用 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.")