二元特征编码
重设数据类型是数据预处理中的重要一步。在本练习中,您将把 'Vmail_Plan' 和 'Churn' 两个特征分别从 'yes'/'no' 映射为 1/0。
您在视频中看到过两种方法——一种使用 pandas,另一种使用 scikit-learn。对于这种相对简单的任务,建议直接使用 pandas,因此本练习也会采用 pandas。而如果您要构建机器学习流水线(本课程不涉及),可以考虑使用 LabelEncoder()。在数据科学实践中,完成同一任务往往有不止一种方法,关键是为您的应用选择最合适、最高效的方案。
本练习是课程的一部分
营销分析:用 Python 预测客户流失
练习说明
- 在
telco的'Vmail_Plan'列中,将'no'替换为0,将'yes'替换为1。 - 对
'Churn'列执行相同的操作。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Replace 'no' with 0 and 'yes' with 1 in 'Vmail_Plan'
telco['Vmail_Plan'] = telco['____'].____(____)
# Replace 'no' with 0 and 'yes' with 1 in 'Churn'
telco['Churn'] = ____
# Print the results to verify
print(telco['Vmail_Plan'].head())
print(telco['Churn'].head())