探索流失率并划分数据
在第 1 章的概览基础上,本课将更深入地进行使用机器学习进行流失预测所需的数据准备。您将先探索流失分布,并在建模前将数据划分为训练集和测试集。在此步骤中,您将了解流失率的分布情况,并对数据进行预处理,以便在训练集上构建模型,并在未使用的测试数据上评估其性能。
电信数据集已作为名为 telcom 的 pandas DataFrame 加载。目标变量列名为 Churn。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 打印
Churn列中的唯一值。 - 计算每个流失组的比例大小。
- 导入用于将数据划分为训练集和测试集的函数。
- 将数据划分为 75% 的训练集和 25% 的测试集。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Print the unique Churn values
print(___(telcom['Churn']))
# Calculate the ratio size of each churn group
telcom.___(['Churn']).size() / telcom.shape[0] * 100
# Import the function for splitting data to train and test
from sklearn.model_selection import ___
# Split the data into train and test
train, test = ___(telcom, test_size = .25)