调整直方图的分箱数量
您刚刚绘制的直方图包含 10 个分箱。这是 matplotlib 的默认设置。"平方根法则"是常用的经验法:分箱数量取样本数量的平方根。本题请再次绘制 Iris versicolor(变色鸢尾)花瓣长度的直方图,这次按照平方根法则来确定分箱数量。您可以在 plt.hist() 中通过关键字参数 bins 指定分箱数量。
绘图工具已导入,且已设置 seaborn 的默认样式。变量 versicolor_petal_length 已在您的命名空间中,包含花瓣长度的数组。
本练习是课程的一部分
Python 中的统计思维(第 1 部分)
练习说明
- 导入
numpy并命名为np。这将提供平方根函数np.sqrt()。 - 使用
len()确定数据点的数量。 - 按平方根法则计算分箱数量。
- 使用内置函数
int()将分箱数量转换为整数。 - 生成直方图,并确保使用关键字参数
bins。 - 点击 提交答案 绘制图形,看看您的成果!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import numpy
# Compute number of data points: n_data
# Number of bins is the square root of number of data points: n_bins
# Convert number of bins to integer: n_bins
# Plot the histogram
# Label axes
_ = plt.xlabel('petal length (cm)')
_ = plt.ylabel('count')
# Show histogram
plt.show()