构建预测因子洞察图表
在上一个练习中,您已经学会了如何计算预测因子洞察图表中的发生率列。本练习中,您还将添加分组大小,并将上述步骤封装到一个函数中,用于返回给定变量的预测因子洞察图表。
本练习是课程的一部分
Python 预测分析入门
练习说明
- 按
variable对 basetable 分组。 - 通过计算目标发生率和分组大小来得到预测因子洞察图表。
- 使用函数
create_pig_table来计算变量 "gender" 的预测因子洞察图表。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Function that creates predictor insight graph table
def create_pig_table(basetable, target, variable):
# Create groups for each variable
groups = basetable[[target,variable]].____(____)
# Calculate size and target incidence for each group
pig_table = groups[____].agg(Incidence = '____', Size = '____').reset_index()
# Return the predictor insight graph table
return pig_table
# Calculate the predictor insight graph table for the variable gender
pig_table_gender = ____(basetable, "target", ____)
# Print the result
print(pig_table_gender)