简体   繁体   中英

numpy.where - Weird behaviour: new elements spawning from nowhere?

I'm trying to use numpy.where to remove [0,0,0] elements from an array. The test array has only one [0,0,0] element placed in the first position. It's shaped (800,3), so it should be (799,3) (after reshaping) or (2397,) after the selection. However the resulting array is (2937,).

My code:

array[np.where(array != [0,0,0])]

Is this some kind of bug?

PD: dtype = 'uint8', in case it's useful.

You are checking all elements in all columns, you need to use all along the first axis to find rows that are not all 0 :

(arr != 0).all(1)

>>> np.where((arr != 0).all(1))[0].shape
(799,)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM