1

I am trying to find values below 200 in a 569x462 ndarray I read in from a text file. I want to use numpy.where to find the values... I've tried the following code:

import numpy as np

#Load in text file from Sebago
sebago=np.loadtxt('sebagoWatershedElv.txt',skiprows=6)

#Find where elevation is less than 200
lowElv=np.where(sebago[:]<200)

But the output shows values less than 200, and some values that are greater than 200...

3
  • 2
    Do you want the indices where the values are less than 200, or the values less than 200? Commented Nov 3, 2015 at 22:28
  • I would actually like both. Commented Nov 4, 2015 at 21:34
  • Shows indices... got it. Commented Nov 7, 2015 at 1:51

2 Answers 2

2

This gives you all values that are less than 200 as one dimensional array:

lowElv = sebago[sebago < 200]
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to practice using numpy.where, but I guess I don't understand the syntax. I've used lowElv=np.where(sebago<200) and it returns values that are greater than 200. Do you know how I could get the same result as the code you've used using numpy.where?
numpy.where returns the indices. So, this sebago[np.where(sebago < 200)] gives the same result as my answer.
2

Minor aside, don't copy (or view) your array [:] unless you're doing something besides 1:1.

import numpy as np
a = np.random.randint(10, size=(8, 8))

# array([[2, 6, 5, 1, 1, 8, 0, 3],
#        [4, 7, 5, 4, 9, 6, 1, 8],
#        [8, 3, 3, 4, 2, 3, 3, 0],
#        [7, 3, 6, 3, 0, 0, 8, 6],
#        [5, 7, 7, 0, 7, 4, 8, 6],
#        [5, 9, 4, 8, 3, 2, 2, 4],
#        [3, 4, 6, 6, 5, 2, 1, 0],
#        [3, 7, 6, 4, 4, 4, 1, 3]])

Indexes where values meet a condition

If you feed the Boolean array (e.g. a <= 1) into np.where it returns a list of arrays where the length is equal to the number of dimensions (here, 2). Pass it to array and it turns the coordinate pairs into columns (e.g. (0, 3), (0, 4), which you can see are 0 or 1 in the above random data). Transposing it is handy .T.

np.array(np.where(a <= 1))
# array([[0, 0, 0, 1, 2, 3, 3, 4, 6, 6, 7],
#        [3, 4, 6, 6, 7, 4, 5, 3, 6, 7, 6]], dtype=int64)

Values meeting a condition

Just index the array with the Boolean. It flattens it and returns the values.

a[a <= 1]
# array([1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1])

How many values meet a condition

np.count_nonzero(a <= 1)
# 11

3 Comments

sebago[:] does not perform a copy, it just returns another view onto the values in sebago
@ali_m ah, you're correct. But a 1:1 view seems like pointless in this case.
For the last one, I would use np.count_nonzero instead. It is both more readable and considerably faster (x5 on my machine) than using np.sum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.