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: 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:
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. |