Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy question
#1
Hi all

I have a question about extracting data from a numpy array in an efficient manner. Can anyone help?

The example is simple. I have a numpy array which is an image 100 x 100 pixels. lets call that img. Each pixel in the array has 3 attributes red, green, blue.

So take the first pixel as an example

img[0,0,0] =255 (red pixel)
img[0,0,1] =100 (blue pixel)
img[0,0,2] =0 (green pixel)

Now I want to, without using loops, make a list of array coordinates where a particular condition is met. So let say I want a list of all coordinates where the red pixel value is equal to 255. let's say hypothetically there are 3 pixels that meet this requirement at [0,0], [10,10] and [25,25]. I would want my code to go through the array and generate a new list as follows

newlist = [[0,0],[10,10],[25,25]]

Is there a way to do this in numpy?

Thanks Peter
Reply
#2
Sup dude or dudette!

I recommend the np.where() function where
import numpy as np
is the library used and definition of np

so in this example, we need to work our way up to really learn this api.

https://docs.scipy.org/doc/numpy/referen...where.html
basically in the above docs, if you read pythonese mean:
np.where(conditional value, if true return this, else return this) #-returns> list of if-else values' positions. ( Where the values are that meet the condition )

np.where(conditional value) #-returns> list of true values in the numpy array's position in the original array. ( Where the values are that meet the condition )
To cut to the chase the answers below have the possible solutions to your project and answer to your question above.
#Basically an array of the positions of the true values
np.where(a[:, :][0] == 255)

#Your answer:
new_list = img[np.where(img[:, :][0] == 255)
If you want to collaborate together, let me know. I love data ;)
Reply
#3
Hello again
I'm still having a few problems using np.where. Can someone please help? An example will assist.

first lets create a 10 x 10 grid filled with numbers

a = np.arange(1,101).reshape(10,10)
Next i want to use the np.where command to extract all items where a particular condition is met. As an example I will make the condition to be that a>97

so a looks like this
Output:
array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], [ 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.], [ 21., 22., 23., 24., 25., 26., 27., 28., 29., 30.], [ 31., 32., 33., 34., 35., 36., 37., 38., 39., 40.], [ 41., 42., 43., 44., 45., 46., 47., 48., 49., 50.], [ 51., 52., 53., 54., 55., 56., 57., 58., 59., 60.], [ 61., 62., 63., 64., 65., 66., 67., 68., 69., 70.], [ 71., 72., 73., 74., 75., 76., 77., 78., 79., 80.], [ 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.], [ 91., 92., 93., 94., 95., 96., 97., 98., 99., 100.]])
I want to put the output into a list called b. b will be a list of the positions in the grid where a>97

I should get the result that
b = [[9,7],[9,8],[9,9]
but when I type
np.where(a>97)
I get
Output:
(array([9, 9, 9], dtype=int64), array([7, 8, 9], dtype=int64))

in a tuple

How can I get the list result I am seeking above using the np.where command?

Thanks Peter
Reply
#4
yep!

np.where just returns a data structure with the locations of what you want.

so I would do something like so:


a[np.where(a>0)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  numpy newbie question bcwilly_ca 4 1,214 Feb-10-2023, 05:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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