user1050619
user1050619

Reputation: 20916

numpy where condition output explained

Im trying to understand numpy where condition.

>>> import numpy as np
>>> x = np.arange(9.).reshape(3, 3)
>>> x
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))

IN the above case, what does the output actually mean, array([0,1,2]) I actually see in the input what is array([2,2,2])

Upvotes: 2

Views: 182

Answers (3)

NaN
NaN

Reputation: 2332

You might also want to know where those values appear visually in your array. In such cases, you can return the array's value where the condition is True and a null value where they are false. In the example below, the value of x is returned at the position where x>5, otherwise assign -1.

x = np.arange(9.).reshape(3, 3)
np.where(x>5, x, -1)
array([[-1., -1., -1.],
       [-1., -1., -1.],
       [ 6.,  7.,  8.]])

Upvotes: 1

BigEd
BigEd

Reputation: 21

Three elements found, located at (2,0),(2,1),(2,2)..

By the way, tryhelp(np.where()) will help you a lot.

Upvotes: 0

Md Johirul Islam
Md Johirul Islam

Reputation: 5162

Th first array indicates the row number and the second array indicates the corresponding column number.

If the array is following:

array([[ 0.,  1.,  2.],
   [ 3.,  4.,  5.],
   [ 6.,  7.,  8.]])

Then the following

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

Can be interpreted as

array(2,0) => 6
array(2,1)  => 7
array (2,2) => 8

Upvotes: 3

Related Questions