python - 在 `np.histogram` 中添加更多的 bin 到底有什么作用?
问题描述
添加更多垃圾箱到底有什么作用np.histogram(data, bins=100)
?我知道它将数据划分为您指定的 bin 数量,但这究竟意味着什么?例如,我有一个直方图,我使用直方图绘制了一条最佳拟合线scipy.curve_fit
,当我增加 bin 时,它也提高了最佳拟合线的准确性。
解决方案
以下函数说明了使用 matplotlib 的区别。使用 5 个 bin 和 10 个 bin 绘制相同的数据:
import matplotlib.pyplot as plt
def plot_histogram(num_bins):
x = [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,
10, 11, 11, 13, 13, 15, 16, 17, 18, 18,
18, 19, 20, 21, 21, 23, 24, 24, 25, 25,
25, 25, 26, 26, 26, 27, 27, 27, 27, 27,
29, 30, 30, 31, 33, 34, 34, 34, 35, 36,
36, 37, 37, 38, 38, 39, 40, 41, 41, 42,
43, 44, 45, 45, 46, 47, 48, 48, 49, 50,
51, 52, 53, 54, 55, 55, 56, 57, 58, 60,
61, 63, 64, 65, 66, 68, 70, 71, 72, 74,
75, 77, 81, 83, 84, 87, 89, 90, 90, 91
]
plt.hist(x, bins=num_bins)
plt.title(f'{num_bins} bins')
plt.show()
plot_histogram(5)
plot_histogram(10)
推荐阅读
- clojurescript - 在子组件中订阅,而不是将 atom 作为参数传递。(试剂/重新构架)
- c# - 当我有一个 PagedList 时如何将零数量的产品移动到列表的末尾
? - python - 查找全球最小熊猫的索引
- spring - Spring Security 5.2 Password Flow
- javascript - Change border color when turn on dark mode via toggle button
- docker - NGINX whitelist internal docker IP
- c# - FileSystemWatcher 在 Debain Buster Raspberry Pi 中不起作用
- android - 如何使用 Redux 在 Android 上实现后退按钮处理
- javascript - 在 React 中将 props 传递给孩子
- c# - 如何使用 MSBuild NuGet 将 .csproj 文件加载到 .NET Core 3.0