gpt4 book ai didi

python - 使用指定边缘归一化的 Numpy 直方图 python

转载 作者:行者123 更新时间:2023-12-01 05:39:09 25 4
gpt4 key购买 nike

我确信这个主题以前曾出现过,但我似乎找不到真正的解决方案。

如您所知,当指定边缘并施加条件时,numpy.histogram 会出现错误

edges = array, where the array (or list) contains the coordinates of the edges.

在文档中,建议使用“Density”= True。然而,在 numpy 网站上,这是正确的标准化直方图的条件

 "Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen"

那么,有谁知道如何生成列表

 values = histogram(list, bins = array (or list))

来自标准化分布?

谢谢

布莱斯

最佳答案

这没有问题,它完全做了它应该做的事情;)

正如密度参数的文档中所述:

the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function.

正如您在示例中看到的:

>>> a = np.arange(5)
>>> hist, bin_edges = np.histogram(a, density=True)
>>> hist.sum()
2.4999999999999996
>>> np.sum(hist*np.diff(bin_edges))
1.0

因此,如果您希望密度=True 且 hist 的总和等于 1,则必须创建大小为 1 的箱。对于正态分布,您可以执行以下操作:

>>> a = np.random.randn(100)
>>> a.std(), a.mean(), a.max() - a.min()
(1.0468524976176077, -0.04129171877871838, 6.049084778833512)
>>> low, high = np.floor(a.min()), np.ceil(a.max())
>>> bins = np.linspace(low, high, high - low + 1)
>>> hist, edges = np.histogram(a, bins=bins, density=True)
>>> hist.sum()
1.0

关于python - 使用指定边缘归一化的 Numpy 直方图 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18082536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com