可以numpy.histogram()
处理2D NumPy数组吗?我似乎无法正常工作?在下面的示例中,我期望numpy.histogram()
函数返回3x2,其中2表示具有2个1D numpy数组(大小为20)的元组,分别包含必要的count和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]))
编辑:
我正在尝试histrogramdd()
@DavidHoffman建议的。该H
结果看起来是错误的。我期望有3行结果,但已经有6行。edges
看起来很正确。我究竟做错了什么?
>>> 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循环方法的结果(@MateenUlhaq提到)。
>>> bins = np.linspace( -3, 3, num=7 )
>>> bins
array([-3., -2., -1., 0., 1., 2., 3.])
>>> for i in a:
np.histogram( i, bins=bins )
(array([ 5, 10, 41, 32, 10, 2]), array([-3., -2., -1., 0., 1., 2., 3.]))
(array([ 3, 10, 37, 37, 11, 2]), array([-3., -2., -1., 0., 1., 2., 3.]))
(array([ 5, 17, 26, 33, 18, 1]), array([-3., -2., -1., 0., 1., 2., 3.]))
避免for循环的一种可能方法是为定义包装器np.histogram()
:
def wrapper(arr):
h, _ = np.histogram(arr, bins=np.linspace(-3, 3, num=7))
return h
并将该包装器函数传递给np.apply_along_axis()
:
h = np.apply_along_axis(wrapper, 1, a)
In [50]: for row in a:
...: h_row, _ = np.histogram(row, bins=np.linspace(-3, 3, num=7))
...: print(h_row)
[ 1 17 38 28 11 4]
[ 1 16 32 35 15 1]
[ 2 11 37 33 16 1]
In [51]: np.apply_along_axis(wrapper, 1, a)
Out[51]:
array([[ 1, 17, 38, 28, 11, 4],
[ 1, 16, 32, 35, 15, 1],
[ 2, 11, 37, 33, 16, 1]], dtype=int64)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句