简体   繁体   中英

numpy.where for row index which that row is not all zero

I have a large matrix which some rows are all zero. I want to get the index of the row that is not all zero. I tried

 idx = np.where(mymatrix[~np.all(mymatrix != 0, axis=1)])

and got

 (array([  21,   21,   21, ..., 1853, 3191, 3191], dtype=int64),
  array([3847, 3851, 3852, ..., 4148, 6920, 6921], dtype=int64))

Is the first array the row index? Is there more straightforward way to get only row index?

有一个简单的方法:

np.where(np.any(arr != 0, axis=1))

You are actually close enough to the solution yourself. You need to think a bit what you do inside the np.where() .

I get this matrix as an example:

array([[1, 1, 1, 1], [2, 2, 2, 2], [0, 0, 0, 0], [3, 3, 3, 3]])

# This will give you back a boolean array of whether your
# statement is true or false per raw
np.all(mymatrix != 0, axis=1)

array([ True, True, False, True], dtype=bool)

Now if you give that to the np.where() it will return your desired output:

np.where(np.all(mymatrix != 0, axis=1))

(array([0, 1, 3]),)

What you do wrong is try to accessing the matrix with the bool matrix you get.

# This will give you the raws without zeros.
mymatrix[np.all(mymatrix != 0, axis=1)]

array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]])

# While this will give you the raws with only zeros
mymatrix[~np.all(mymatrix != 0, axis=1)]

Given an array like this, np.where() is not able to return the indices. It doesn't know what you ask for.

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