Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 如何从2D NumPy数组中获取直方图数据?_Python_Numpy - Fatal编程技术网

Python 如何从2D NumPy数组中获取直方图数据?

Python 如何从2D NumPy数组中获取直方图数据?,python,numpy,Python,Numpy,numpy.histogram()能否处理2D numpy数组?我好像不能让它工作?在下面的示例中,我希望numpy.histogram()函数返回一个3x2,其中2表示一个元组,该元组具有2个大小为20的1D numpy数组,分别包含必要的计数和bins数据 示例: >>> import numpy as np >>> from numpy.random import default_rng >>> from scipy.stats imp

numpy.histogram()
能否处理2D numpy数组?我好像不能让它工作?在下面的示例中,我希望
numpy.histogram()
函数返回一个3x2,其中2表示一个元组,该元组具有2个大小为20的1D numpy数组,分别包含必要的计数和bins数据

示例:

>>> import numpy as np
>>> from numpy.random import default_rng
>>> from scipy.stats import norm
>>> rg = default_rng()
>>> a = norm.rvs( size=(3, 100), random_state=rg )
>>> np.histogram( a, bins=20 )
(array([ 2,  2,  4,  6, 10, 11, 15, 19, 37, 26, 25, 28, 27, 29, 21, 22,  5,
        5,  3,  3]), array([-2.736423  , -2.47271089, -2.20899879, -1.94528668, -1.68157457,
       -1.41786247, -1.15415036, -0.89043825, -0.62672615, -0.36301404,
       -0.09930193,  0.16441017,  0.42812228,  0.69183439,  0.95554649,
        1.2192586 ,  1.48297071,  1.74668281,  2.01039492,  2.27410703,
        2.53781913]))
>>> np.histogram( a.T, bins=20 )
(array([ 2,  2,  4,  6, 10, 11, 15, 19, 37, 26, 25, 28, 27, 29, 21, 22,  5,
        5,  3,  3]), array([-2.736423  , -2.47271089, -2.20899879, -1.94528668, -1.68157457,
       -1.41786247, -1.15415036, -0.89043825, -0.62672615, -0.36301404,
       -0.09930193,  0.16441017,  0.42812228,  0.69183439,  0.95554649,
        1.2192586 ,  1.48297071,  1.74668281,  2.01039492,  2.27410703,
        2.53781913]))
编辑:

>>> import numpy as np
>>> from numpy.random import default_rng
>>> from scipy.stats import norm
>>> rg = default_rng()
>>> a = norm.rvs( size=(3, 100), random_state=rg )
>>> np.histogram( a, bins=20 )
(array([ 2,  2,  4,  6, 10, 11, 15, 19, 37, 26, 25, 28, 27, 29, 21, 22,  5,
        5,  3,  3]), array([-2.736423  , -2.47271089, -2.20899879, -1.94528668, -1.68157457,
       -1.41786247, -1.15415036, -0.89043825, -0.62672615, -0.36301404,
       -0.09930193,  0.16441017,  0.42812228,  0.69183439,  0.95554649,
        1.2192586 ,  1.48297071,  1.74668281,  2.01039492,  2.27410703,
        2.53781913]))
>>> np.histogram( a.T, bins=20 )
(array([ 2,  2,  4,  6, 10, 11, 15, 19, 37, 26, 25, 28, 27, 29, 21, 22,  5,
        5,  3,  3]), array([-2.736423  , -2.47271089, -2.20899879, -1.94528668, -1.68157457,
       -1.41786247, -1.15415036, -0.89043825, -0.62672615, -0.36301404,
       -0.09930193,  0.16441017,  0.42812228,  0.69183439,  0.95554649,
        1.2192586 ,  1.48297071,  1.74668281,  2.01039492,  2.27410703,
        2.53781913]))
我正在尝试@DavidHoffman建议的
histrogramdd()
H
结果看起来是错误的。我期望得到3行结果,但得到了6行<代码>边缘看起来正确。我做错了什么

>>> bins = np.linspace(np.array([-3,-3,-3]),  np.array([3,3,3]), num=7, axis=1 )
>>> bins
array([[-3., -2., -1.,  0.,  1.,  2.,  3.],
       [-3., -2., -1.,  0.,  1.,  2.,  3.],
       [-3., -2., -1.,  0.,  1.,  2.,  3.]])
>>> H, edges = np.histogramdd(a.T, bins=bins)
>>> H
array([[[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 2., 0., 0., 0.],
        [0., 1., 0., 0., 1., 0.],
        [0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0.],
        [0., 2., 0., 1., 1., 0.],
        [0., 1., 1., 1., 0., 0.],
        [0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 2., 0., 0.],
        [0., 1., 4., 7., 2., 0.],
        [1., 3., 5., 6., 4., 1.],
        [2., 1., 0., 2., 0., 0.],
        [0., 0., 0., 0., 0., 0.]],

       [[0., 1., 1., 1., 0., 0.],
        [0., 0., 1., 3., 1., 0.],
        [1., 2., 2., 3., 1., 0.],
        [1., 2., 4., 1., 3., 0.],
        [0., 2., 0., 0., 0., 0.],
        [0., 1., 0., 0., 1., 0.]],

       [[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 5., 2., 1., 0.],
        [0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 0., 0.]]])
>>> edges
[array([-3., -2., -1.,  0.,  1.,  2.,  3.]), array([-3., -2., -1.,  0.,  1.,  2.,  3.]), array([-3., -2., -1.,  0.,  1.,  2.,  3.])]
>>>
结果应该类似于for-loop方法的结果(由@MateenUlhaq提到)


避免for循环的一种可能方法是为
np.histogram()
定义一个包装器:

def包装器(arr):
h、 _uu=np.直方图(arr,bin=np.linspace(-3,3,num=7))
返回h
并将该包装函数传递给:

h=np。沿_轴应用_(包装器,1,a)
演示 [50]中的
:对于a中的行:
…:h_行,u=np.直方图(行,bin=np.linspace(-3,3,num=7))
…:打印(h_行)
[ 1 17 38 28 11  4]
[ 1 16 32 35 15  1]
[ 2 11 37 33 16  1]
在[51]中:np.沿_轴应用_(包装器,1,a)
出[51]:
数组([[1,17,38,28,11,4],
[ 1, 16, 32, 35, 15,  1],
[2,11,37,33,16,1]],dtype=int64)

避免for循环的一种可能方法是为
np.histogram()定义一个包装器。

def包装器(arr):
h、 _uu=np.直方图(arr,bin=np.linspace(-3,3,num=7))
返回h
并将该包装函数传递给:

h=np。沿_轴应用_(包装器,1,a)
演示 [50]中的
:对于a中的行:
…:h_行,u=np.直方图(行,bin=np.linspace(-3,3,num=7))
…:打印(h_行)
[ 1 17 38 28 11  4]
[ 1 16 32 35 15  1]
[ 2 11 37 33 16  1]
在[51]中:np.沿_轴应用_(包装器,1,a)
出[51]:
数组([[1,17,38,28,11,4],
[ 1, 16, 32, 35, 15,  1],
[2,11,37,33,16,1]],dtype=int64)

假设要单独处理2D数据的每一行。。。快速看一眼这本书就知道可能不会。快速查看上面的“A:输入数据。直方图是在展平的数组上计算的。”,这意味着当然不是。我认为您必须手动对数据进行循环。请查看@MateenUlhaq,谢谢。我知道for循环方法是有效的。不过,我还是希望NumPy有一个更快的内置功能来完成这项工作。正在尝试Historogramdd(),但遇到了一个问题。@DavidHoffman我从
Historogramdd()
得到的结果似乎有误。你能告诉我如何纠正我的错误吗?假设你想单独处理每一行2D数据。。。快速看一眼这本书就知道可能不会。快速查看上面的“A:输入数据。直方图是在展平的数组上计算的。”,这意味着当然不是。我认为您必须手动对数据进行循环。请查看@MateenUlhaq,谢谢。我知道for循环方法是有效的。不过,我还是希望NumPy有一个更快的内置功能来完成这项工作。正在尝试Historogramdd(),但遇到了一个问题。@DavidHoffman我从
Historogramdd()
得到的结果似乎有误。你能告诉我如何改正我的错误吗?谢谢。这个解决方案很好。当
bin
的值不是常数时,即不同的
bin
值将用于处理
a
的每一行时,您的方法是否有效?当然可以,只要您在
包装器
函数中定义
bin
的值(例如作为
arr
的函数)。谢谢。这个解决方案很好。当
bin
的值不是常数时,即不同的
bin
值将用于处理
a
的每一行时,您的方法是否有效?当然可以,只要您在
包装器
函数中定义
bin
的值(例如作为
arr
的函数)。