绘制分组大小
预测因子洞察图用于展示具有预测力的变量信息。每个变量会将总体划分为若干组。预测因子洞察图包含一条折线,用于显示每组的平均目标发生率,以及一组柱形,用于显示各组的人群规模。 在本练习中,您将学习如何在给定预测因子洞察图表格的情况下,编写并调用一个函数来绘制预测因子洞察图。
本练习是课程的一部分
Python 预测分析入门
交互式实操练习
通过完成这段示例代码来试试这个练习。
# The function to plot a predictor insight graph
def plot_pig(pig_table, variable):
# Plot formatting
plt.ylabel("Size", rotation=0, rotation_mode="anchor", ha="right")
# Plot the bars with sizes
pig_table["____"].plot(kind="bar", width=0.5, color="lightgray", edgecolor="none")
# Plot the incidence line on secondary axis
pig_table["____"].plot(secondary_y=True)
# Plot formatting
plt.xticks(np.arange(len(pig_table)), pig_table[variable])
plt.xlim([-0.5, len(pig_table) - 0.5])
plt.ylabel("Incidence", rotation=0, rotation_mode="anchor", ha="left")
# Show the graph
plt.show()