理解变量间的差异
现在,您将通过柱状图来分析各个变量的平均值和标准差。这个步骤与之前的分析相辅相成,您将以可视化方式探索变量量纲与方差的差异。
pandas 库已以 pd 导入,matplotlib.pyplot 以 plt 导入。wholesale 数据集已加载为 pandas 的 DataFrame;每一列的平均值和标准差分别加载为名为 averages 和 std_devs 的 pandas Series。请在控制台中先行查看它们。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 创建一个包含
wholesale列名的列表,以及另一个从 0 到wholesale列数的有序取值列表。 - 绘制
averages(灰色)和std_devs(橙色),并将 x 轴平移 0.2。 - 将
x_ix作为刻度、x_names作为标签,并将其旋转 90 度。 - 添加图例并显示图表。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create column names list and same length integer list
x_names = wholesale.___
x_ix = np.arange(wholesale.shape[1])
# Plot the averages data in gray and standard deviations in orange
plt.bar(x=x_ix-___, height=averages, color='grey', label='Average', width=0.4)
plt.bar(x=x_ix+___, height=std_devs, color='orange', label='Standard Deviation', width=0.4)
# Add x-axis labels and rotate
plt.xticks(ticks=___, labels=x_names, rotation=90)
# Add the legend and display the chart
plt.legend()
plt.___()