Jan
Jan

Reputation: 1479

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?

Upvotes: 0

Views: 394

Answers (2)

Glrs
Glrs

Reputation: 1080

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.

Upvotes: 1

gzc
gzc

Reputation: 8639

There is a straight way:

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

Upvotes: 1

Related Questions