2

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])

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.]])

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.