find and delete from more-dimensional numpy array

后端 未结 3 1150
梦毁少年i
梦毁少年i 2020-11-29 10:51

I have two numpy-arrays:

p_a_colors=np.array([[0,0,0],
                     [0,2,0],
                     [119,103,82],
                     [122,122,122],
          


        
3条回答
  •  我在风中等你
    2020-11-29 11:15

    You are getting the indices wrong. The expression p_a_colors==p_rem evaluates to an empty array, because the two arrays are never equal (they have different shapes!). If you want to use np.delete, you need a more correct list of indices.

    On the other hand, this can be more easily done with indices:

    >>> idx = np.array([p_a_colors[i] not in p_rem for i in
                        range(p_a_colors.shape[0])], dtype='bool')
    >>> p_a_colors[idx]
    array([[0, 0, 0],
           [0, 2, 0],
           [3, 2, 4]])
    

    Or, inspired by the suggestion of @Jaime, you can also create the indices with np.in1d, here in one line:

    >>> idx = ~np.all(np.in1d(p_a_colors, p_rem).reshape(p_a_colors.shape), 
                      axis=1)
    >>> p_a_colors[idx]
    array([[0, 0, 0],
           [0, 2, 0],
           [3, 2, 4]])
    

    If you must use np.delete, just convert the list of indices from bool to a sequence:

    >>> idx = np.array([p_a_colors[i] in p_rem for i in 
                              range(p_a_colors.shape[0])])
    >>> idx = np.arange(p_a_colors.shape[0])[idx]
    >>> np.delete(p_a_colors, idx, axis=0)
    array([[0, 0, 0],
           [0, 2, 0],
           [3, 2, 4]])
    

提交回复
热议问题