Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 The where function in numpy saund1pe Unladen Swallow Posts: 3 Threads: 1 Joined: Nov 2016 Reputation: 0 Likes received: 0 #1 Nov-15-2016, 04:57 PM (This post was last modified: Nov-15-2016, 06:16 PM by snippsat. Edited 2 times in total. Edit Reason: Formatting. ) 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. Larz60+ aetate et sapientia Posts: 7,668 Threads: 289 Joined: Sep 2016 Reputation: 286 Likes received: 1059 #2 Nov-15-2016, 05:08 PM Please re-post using code tags. Indentation, (has to be correct in python) is lost otherwise saund1pe Unladen Swallow Posts: 3 Threads: 1 Joined: Nov 2016 Reputation: 0 Likes received: 0 #3 Nov-15-2016, 05:13 PM (This post was last modified: Nov-15-2016, 05:57 PM by Yoriz. Edited 2 times in total. Edit Reason: Fix formatting. Spoilers on repost. ) Sorry, I'm new to python and this forum. What do you mean? repost not necessary op fixed by moderator 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. Edit admin: Take a look at BBcode Okay, thanks. I'll repost this with correct formatting. Larz60+ aetate et sapientia Posts: 7,668 Threads: 289 Joined: Sep 2016 Reputation: 286 Likes received: 1059 #4 Nov-15-2016, 06:01 PM 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] `````` saund1pe Unladen Swallow Posts: 3 Threads: 1 Joined: Nov 2016 Reputation: 0 Likes received: 0 #5 Nov-16-2016, 06:44 PM 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? nilamo Last Thursdayist Posts: 3,186 Threads: 82 Joined: Sep 2016 Reputation: 129 Likes received: 712 #6 Nov-16-2016, 08:04 PM 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. « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)