分类特征编码
有时数据集中包含用数值来表示分类特征的情况。
在 donors 数据集中,wealth_rating 使用数字表示捐赠者的财富水平:
- 0 = Unknown(未知)
- 1 = Low(低)
- 2 = Medium(中)
- 3 = High(高)
本练习演示如何准备此类分类特征,并考察其对逻辑回归模型的影响。donors 数据框已为您准备好。
本练习是课程的一部分
R 中的监督学习:分类
练习说明
- 使用数值型
wealth_rating创建因子变量wealth_levels,并按上述标签进行标注:向factor()传入要转换的列、各个层级,以及对应的标签。 - 使用
relevel()将基准类别改为Medium。第一个参数应为您新生成的factor列。 - 构建一个逻辑回归模型,使用
wealth_levels列预测donated,并用summary()显示结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Convert the wealth rating to a factor
donors$wealth_levels <- ___(___, levels = ___, labels = ___)
# Use relevel() to change reference category
donors$wealth_levels <- ___(___, ref = ___)
# See how our factor coding impacts the model
summary(___)