Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy 2d直方图总和不等于1_Python_Numpy_Histogram - Fatal编程技术网

Python Numpy 2d直方图总和不等于1

Python Numpy 2d直方图总和不等于1,python,numpy,histogram,Python,Numpy,Histogram,我想我误解了Numpy的Historogram2drange和bin参数 下面是一个我所期望的it工作方式的示例: d, x_r, y_r = np.histogram2d( [0, 1, 3], [0, 1, 3], bins=[3, 3], range=[[0, 3], [0, 3]], normed=True) d array([[ 0.33333333, 0. , 0. ], [ 0.

我想我误解了Numpy的Historogram2d
range
bin
参数

下面是一个我所期望的it工作方式的示例:

d, x_r, y_r = np.histogram2d(
    [0, 1, 3], 
    [0, 1, 3], 
    bins=[3, 3], 
    range=[[0, 3], [0, 3]], 
    normed=True)

d
array([[ 0.33333333,  0.        ,  0.        ],
       [ 0.        ,  0.33333333,  0.        ],
       [ 0.        ,  0.        ,  0.33333333]])
np.sum(d)
1.0
这就是我开始分崩离析的地方(增加垃圾箱数量):

我原以为:

d
array([[ 0.33333333,  0.,  0.        ,  0.,  0., 0.        ],
       [ 0.        ,  0.,  0.33333333,  0.,  0., 0.        ],
       [ 0.        ,  0.,  0.        ,  0.,  0., 0.33333333]])
如果您能帮助我理解这一点,并得到我想要的结果,我将不胜感激。谢谢。

中的赋范参数正常化如下

bin_count / sample_count / bin_area
这些都需要一段时间才能理解,在我看来,这个函数写得不是很好(变量名选择不当)

  • bin\u count
    是直方图bin中的值
  • sample\u count
    是所有bin\u计数的总和
  • bin\u区域
    是特定bin的区域
在这两种情况下,我们都可以定义上述3个变量,而无需使用赋范参数,并查看发生了什么:

案例1 如果您查看
binsx
binsy
您将看到每个箱子的面积为1

print(binsx, binsy)
#In [54]: print (binsx, binsy)
#(array([ 0.,  1.,  2.,  3.]), array([ 0.,  1.,  2.,  3.]))
因此,我们让
bin_面积=1
和2D直方图标准化

bin_count / bin_count.sum() / bin_area

#array([[ 0.33333333,  0.        ,  0.        ],
       #[ 0.        ,  0.33333333,  0.        ],
       #[ 0.        ,  0.        ,  0.33333333]])
bin_count / bin_count.sum() / bin_area

#array([[ 0.66666667,  0.        ,  0.        ,  0.        ,  0.        ,
#         0.        ],
#       [ 0.        ,  0.        ,  0.66666667,  0.        ,  0.        ,
#         0.        ],
#       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
#         0.66666667]])
案例2 现在,您可以看到您的
bin_区域减少了2倍(因为您将y-bin的数量增加了2倍)

因此,我们让
bin_area=.5
,并且标准化的hist如下所示

bin_count / bin_count.sum() / bin_area

#array([[ 0.33333333,  0.        ,  0.        ],
       #[ 0.        ,  0.33333333,  0.        ],
       #[ 0.        ,  0.        ,  0.33333333]])
bin_count / bin_count.sum() / bin_area

#array([[ 0.66666667,  0.        ,  0.        ,  0.        ,  0.        ,
#         0.        ],
#       [ 0.        ,  0.        ,  0.66666667,  0.        ,  0.        ,
#         0.        ],
#       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
#         0.66666667]])
一般情况 一般来说,您可以拥有大小可变的箱子,因此箱子面积可能是一个变量。考虑一些非均匀容器:

bin_count, binsx, binsy = np.histogram2d( [0,1,3], [0,1,3], 
    bins=([0.,1.5,3.],[0, .6, 1.7,3.]), 
    range=[[0,3],[0,3]], normed=False)
在这种情况下,明确计算每个料仓的面积:

bin_area = np.array( [ [(x1 -x0)* (y1-y0) 
    for y1,y0 in zip(binsy[1:], binsy[:-1])] 
        for x1,x0 in zip(binsx[1:], binsx[:-1]) ] )

print(bin_area)
#array([[ 0.9 ,  1.65,  1.95],
#       [ 0.9 ,  1.65,  1.95]])

bin_count / bin_count.sum() / bin_area
#array([[ 0.37037037,  0.2020202 ,  0.        ],
#       [ 0.        ,  0.        ,  0.17094017]])
事实上,如果我们将赋范参数设置为True

normed_bin_count, binsx, binsy = np.histogram2d( [0,1,3], [0,1,3], 
    bins=([0.,1.5,3.],[0, .6, 1.7,3.]), 
    range=[[0,3],[0,3]], normed=True)
print(normed_bin_count)
#array([[ 0.37037037,  0.2020202 ,  0.        ],
#       [ 0.        ,  0.        ,  0.17094017]])

你看过报纸了吗?看看标准密度和密度以及它们的警告。@sascha我确实读过文档。我将重读这些章节,并与它们一起玩。我不是一个统计学家,所以我很难理解他们。我还使用了2d函数,它没有密度参数。太好了,谢谢。所以我只需要将x和y乘以箱子的大小就可以得到我想要的结果。我认为如果你认为
bin\u count
是箱子中的样本数,
sample\u count
是样本总数,这很容易理解。@freebie如果你想要相对比例,请使用
normed=False
然后除以每个箱子按样本数计数。那不是标准的直方图。@Goyo哦,对了。干杯显然,我需要重新整理一下我的统计数据
normed_bin_count, binsx, binsy = np.histogram2d( [0,1,3], [0,1,3], 
    bins=([0.,1.5,3.],[0, .6, 1.7,3.]), 
    range=[[0,3],[0,3]], normed=True)
print(normed_bin_count)
#array([[ 0.37037037,  0.2020202 ,  0.        ],
#       [ 0.        ,  0.        ,  0.17094017]])