将数据划分为训练集和测试集
在构建回归模型之前的最后一步!本题中,您将依次完成:确定目标变量名与特征列名、提取数据,并将其划分为训练集和测试集。
pandas 与 numpy 库已分别以 pd 和 np 形式导入。输入特征已作为 features 数据集提供,上一题中您构建的目标变量也已为您导入为 Y。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 将客户标识符列名以列表形式存储。
- 选择除客户标识符外的特征列名。
- 将特征提取为
X。 - 使用
train_test_split()函数将数据划分为训练集和测试集。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Store customer identifier column name as a list
custid = ['___']
# Select feature column names excluding customer identifier
cols = [col for col in features.___ if col not in ___]
# Extract the features as `X`
X = features[___]
# Split data to training and testing
___, test_X, train_Y, ___ = ___(X, Y, test_size=0.25, random_state=99)