Python Forum
The where function in numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The where function in numpy
#4
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]
Reply


Messages In This Thread
The where function in numpy - by saund1pe - Nov-15-2016, 04:57 PM
RE: The where function in numpy - by Larz60+ - Nov-15-2016, 05:08 PM
RE: The where function in numpy - by saund1pe - Nov-15-2016, 05:13 PM
RE: The where function in numpy - by Larz60+ - Nov-15-2016, 06:01 PM
RE: The where function in numpy - by saund1pe - Nov-16-2016, 06:44 PM
RE: The where function in numpy - by nilamo - Nov-16-2016, 08:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 2,176 May-15-2023, 01:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020