Can numpy bincount work with 2D arrays?

前端 未结 2 1272
生来不讨喜
生来不讨喜 2020-12-03 17:21

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 i

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 17:34

    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 "", line 1, in 
        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]])
    

提交回复
热议问题