【发布时间】:2021-04-14 11:10:21
【问题描述】:
我正在尝试生成密度函数,但生成的直方图的分量之和似乎不接近 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
如果我没有指定 bin 边缘,则输出会更小但仍大于 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
【问题讨论】: