Python Forum
The where function in numpy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: The where function in numpy (/thread-938.html)



The where function in numpy - saund1pe - Nov-15-2016

Hello,

This isn't necessarily a homework problem or even about a class that I'm taking. I'm trying to teach myself python for graduate level work and I'm using a book to try and teach myself. I came across code in the book and I'm a little confused about. The code is as follows:
import Numpy as N
a = N.reshape(N.arange(8), (2,2,2) )
condition = N.logical_and(a>3, a<6)
answer_indices = N.where(condition)
answer = (a*2)[answer_indices]
Now, what I'm confused about is when you print answer_indices it comes out as:
Output:
(array([1, 1]), array([0, 0]), array([0,1]))
and I'm not entirely sure why that is. I may be a little confused with 3-D arrays in general so could somebody explain to me why this comes out this way?

Thanks.


RE: The where function in numpy - Larz60+ - Nov-15-2016

Please re-post using code tags.
Indentation, (has to be correct in python) is lost otherwise


RE: The where function in numpy - saund1pe - Nov-15-2016

Sorry, I'm new to python and this forum. What do you mean?

Edit admin:
Take a look at BBcode

Okay, thanks. I'll repost this with correct formatting.


RE: The where function in numpy - Larz60+ - Nov-15-2016

First of all, this code won't run (as written), numpy is not capitalized
That aside, you should put print statements into the code until you understand how it all works.
That will help tremendously
Without explanation, here's waht I'm talking about:
import numpy as N

a = N.reshape(N.arange(8), (2,2,2) )
print('a: {}'.format(a))

condition = N.logical_and(a>3, a<6)
print('condition: {}'.format(condition))

answer_indices = N.where(condition)
print('answer_indices: {}'.format(answer_indices))

answer = (a*2)[answer_indices]
print('answer: {}'.format(answer))
results:
Output:
a: [[[0 1]   [2 3]]  [[4 5]   [6 7]]] condition: [[[False False]   [False False]]  [[ True  True]   [False False]]] answer_indices: (array([1, 1], dtype=int64), array([0, 0], dtype=int64), array([0, 1], dtype=int64)) answer: [ 8 10]



RE: The where function in numpy - saund1pe - Nov-16-2016

Hey, thanks for the response. So, I understand the output for 'a' and 'condition', and even 'answer'. What I don't understand is the output for 'answer_indices'. What do the three arrays in 'answer_indices' represent?


RE: The where function in numpy - nilamo - Nov-16-2016

Sometimes, StackOverflow/Google is a better place to look for information :P  
http://stackoverflow.com/a/5642525 Wrote:
Quote:How do they achieve internally that you are able to pass something like x > 5 into a method?
The short answer is that they don't.

Any sort of logical operation on a numpy array returns a boolean array. (i.e. __gt__, __lt__, etc all return boolean arrays where the given condition is true).

E.g.

x = np.arange(9).reshape(3,3)
print x > 5
yields:

Output:
array([[False, False, False],       [False, False, False],       [ True,  True,  True]], dtype=bool)
This is the same reason why something like if x > 5: raises a ValueError if x is a numpy array. It's an array of True/False values, not a single value.

Furthermore, numpy arrays can be indexed by boolean arrays. E.g. x[x>5] yields [6 7 8], in this case.

Honestly, it's fairly rare that you actually need numpy.where but it just returns the indicies where a boolean array is True. Usually you can do what you need with simple boolean indexing.