numpy bincount 可以处理二维数组吗? [英] Can numpy bincount work with 2D arrays?

查看:65
本文介绍了numpy bincount 可以处理二维数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了我无法理解的 numpy bincount 行为.我想以行方式将二维数组中的值合并,并查看下面的行为.为什么它可以与 dbArray 一起使用,但与 simarray 一起失败?

<预><代码>>>>数据库数组数组([[1, 0, 1, 0, 1],[1, 1, 1, 1, 1],[1, 1, 0, 1, 1],[1, 0, 0, 0, 0],[0, 0, 0, 1, 1],[0, 1, 0, 1, 0]])>>>N.apply_along_axis(N.bincount,1,dbArray)数组([[2, 3],[0, 5],[1, 4],[4, 1],[3, 2],[3, 2]], dtype=int64)>>>模拟阵列数组([[2, 0, 2, 0, 2],[2, 1, 2, 1, 2],[2, 1, 1, 1, 2],[2, 0, 1, 0, 1],[1, 0, 1, 1, 2],[1, 1, 1, 1, 1]])>>>N.apply_along_axis(N.bincount,1,simarray)回溯(最近一次调用最后一次):文件<pyshell#31>",第 1 行,在 <module> 中N.apply_along_axis(N.bincount,1,simarray)文件C:\Python27\lib\site-packages\numpy\lib\shape_base.py",第 118 行,在 apply_along_axisoutarr[tuple(i.tolist())] = resValueError:无法将输入数组从形状 (2) 广播到形状 (3)

解决方案

问题在于 bincount 并不总是返回相同形状的对象,尤其是在缺少值时.例如:

<预><代码>>>>m = np.array([[0,0,1],[1,1,0],[1,1,1]])>>>np.apply_along_axis(np.bincount, 1, m)数组([[2, 1],[1, 2],[0, 3]])>>>[np.bincount(m[i]) for i in range(m.shape[1])][数组([2, 1]), 数组([1, 2]), 数组([0, 3])]

有效,但是:

<预><代码>>>>m = np.array([[0,0,0],[1,1,0],[1,1,0]])>>>米数组([[0, 0, 0],[1, 1, 0],[1, 1, 0]])>>>[np.bincount(m[i]) for i in range(m.shape[1])][数组([3]), 数组([1, 2]), 数组([1, 2])]>>>np.apply_along_axis(np.bincount, 1, m)回溯(最近一次调用最后一次):文件<ipython-input-49-72e06e26a718>",第 1 行,在 <module>np.apply_along_axis(np.bincount, 1, m)文件/usr/local/lib/python2.7/dist-packages/numpy/lib/shape_base.py",第117行,在apply_along_axisoutarr[tuple(i.tolist())] = resValueError:无法将输入数组从形状 (2) 广播到形状 (1)

不会.

您可以使用 minlength 参数并使用 lambdapartial 或其他方式传递它:

<预><代码>>>>np.apply_along_axis(lambda x: np.bincount(x, minlength=2), axis=1, arr=m)数组([[3, 0],[1, 2],[1, 2]])

I am seeing behaviour with numpy bincount that I cannot make sense of. I want to bin the values in a 2D array in a row-wise manner and see the behaviour below. Why would it work with dbArray but fail with simarray?

>>> dbArray
array([[1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 0, 1, 1],
       [1, 0, 0, 0, 0],
       [0, 0, 0, 1, 1],
       [0, 1, 0, 1, 0]])
>>> N.apply_along_axis(N.bincount,1,dbArray)
array([[2, 3],
       [0, 5],
       [1, 4],
       [4, 1],
       [3, 2],
       [3, 2]], dtype=int64)
>>> simarray
array([[2, 0, 2, 0, 2],
       [2, 1, 2, 1, 2],
       [2, 1, 1, 1, 2],
       [2, 0, 1, 0, 1],
       [1, 0, 1, 1, 2],
       [1, 1, 1, 1, 1]])
>>> N.apply_along_axis(N.bincount,1,simarray)

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    N.apply_along_axis(N.bincount,1,simarray)
  File "C:\Python27\lib\site-packages\numpy\lib\shape_base.py", line 118, in apply_along_axis
    outarr[tuple(i.tolist())] = res
ValueError: could not broadcast input array from shape (2) into shape (3)

解决方案

The problem is that bincount isn't always returning the same shaped objects, in particular when values are missing. For example:

>>> m = np.array([[0,0,1],[1,1,0],[1,1,1]])
>>> np.apply_along_axis(np.bincount, 1, m)
array([[2, 1],
       [1, 2],
       [0, 3]])
>>> [np.bincount(m[i]) for i in range(m.shape[1])]
[array([2, 1]), array([1, 2]), array([0, 3])]

works, but:

>>> m = np.array([[0,0,0],[1,1,0],[1,1,0]])
>>> m
array([[0, 0, 0],
       [1, 1, 0],
       [1, 1, 0]])
>>> [np.bincount(m[i]) for i in range(m.shape[1])]
[array([3]), array([1, 2]), array([1, 2])]
>>> np.apply_along_axis(np.bincount, 1, m)
Traceback (most recent call last):
  File "<ipython-input-49-72e06e26a718>", line 1, in <module>
    np.apply_along_axis(np.bincount, 1, m)
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/shape_base.py", line 117, in apply_along_axis
    outarr[tuple(i.tolist())] = res
ValueError: could not broadcast input array from shape (2) into shape (1)

won't.

You could use the minlength parameter and pass it using a lambda or partial or something:

>>> np.apply_along_axis(lambda x: np.bincount(x, minlength=2), axis=1, arr=m)
array([[3, 0],
       [1, 2],
       [1, 2]])

这篇关于numpy bincount 可以处理二维数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆