划分员工数据
在分析中,过拟合是一个常见问题。模型在其训练所用的数据集上表现良好,但在新数据上泛化能力较差。
为确保模型具备泛化能力,通常需要进行训练集/测试集划分:您先使用训练集开发模型,之后在测试集上进行验证。
在本练习中,您将把 target 和 features 分别按 75%/25% 的比例划分为训练集和测试集。
本练习是课程的一部分
HR Analytics:用 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)