我试图生成一个密度函数,但生成的直方图的分量之和似乎不接近1
这是什么原因?如何使密度函数之和接近(即使不完全等于)1
最简单的例子:
import numpy as np
x = np.random.normal(0, 0.5, 1000) # mu, sigma, num
bins = np.linspace(min(x), max(x), num=50) # lower and upper bounds
hist, hist_bins = np.histogram(x, bins=bins, density = True)
print(np.sum(hist))
>>> 10.4614
如果未指定箱子边缘,则输出较小但仍大于1:
import numpy as np
x = np.random.normal(0, 0.5, 1000) # mu, sigma, num
hist, hist_bins = np.histogram(x, density = True)
print(np.sum(hist))
>>> 3.1332
此行为的原因is stated in the docs:
此外,还提供了一个样本,表明直方图之和不等于1.0:
因此,我们可以将此应用于您的代码段:
此外,您应该考虑如何选择垃圾箱,并确保使用
.linspace()
是一种合理的方法相关问题 更多 >
编程相关推荐