Python Forum
Addressing with np.where
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Addressing with np.where
#1
Hi everyone

I am rewriting my program to eliminate for loops and using numpy arrays to do so.

I am stuck at the following problem.

First here is my numpy array called 'second'.
print (second)
[[[ 5.    1.75]
  [13.75  4.5 ]
  [11.25  8.25]
  [11.25  3.25]
  [13.75 17.5 ]
  [16.25 15.75]]

 [[ 5.    4.75]
  [13.75  7.5 ]
  [11.25 11.25]
  [11.25  6.25]
  [13.75 20.5 ]
  [16.25 18.75]]

 [[16.25 16.  ]
  [25.   18.75]
  [22.5  22.5 ]
  [22.5  17.5 ]
  [25.   31.75]
  [27.5  30.  ]]

 [[53.75 23.5 ]
  [62.5  26.25]
  [60.   30.  ]
  [60.   25.  ]
  [62.5  39.25]
  [65.   37.5 ]]

 [[34.25 14.5 ]
  [43.   17.25]
  [40.5  21.  ]
  [40.5  16.  ]
  [43.   30.25]
  [45.5  28.5 ]]

 [[43.25 34.75]
  [52.   37.5 ]
  [49.5  41.25]
  [49.5  36.25]
  [52.   50.5 ]
  [54.5  48.75]]

 [[23.75 46.  ]
  [32.5  48.75]
  [30.   52.5 ]
  [30.   47.5 ]
  [32.5  61.75]
  [35.   60.  ]]

 [[ 9.5   9.25]
  [18.25 12.  ]
  [15.75 15.75]
  [15.75 10.75]
  [18.25 25.  ]
  [20.75 23.25]]]
Its shape is

Output:
second.shape Out[181]: (8, 6, 2)
As you can see there are two coordinates at each point in this 8x6 array.

I also have a list called file2 with a list of coordinates

file2=[[5,2],[10,10],[60,25],[40,10],[55,11],[1,15],[67,66],[45,34]] 
I am trying to code a list of positions in second where the following is satisfied.

Return a list of positions in second where the position in second [m,n] is within x-1<m<x+1 and y-1<n<y+1 where [m,n] is a set of coordinates in file2.

Coding the above is a nightmare. The answer I expect to get is

Output:
{8,1,3],[0,0,3]
which of course represents the positions

[0,0],[1,0] and [3,3]

I found I could use

np.in1d(second,file2)
and this gives
Output:
Out[209]: array([False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, True, False, True, False, True, False, True, False, True, False, False, False, False, False, False, False, False, False, False, False, False])
but this is addressing each x and y amount separately rather than the two together.

What I really need is some variation on

np.in1d(second,file2)

which gives a bit more latitude i.e the x or y value can be within the range x+/-1
or y+/-1 and addresses
x and y at the same time i,e both x and y are within the specified range.

Can anyone help?

cheers Peter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FCC API: addressing unexpected error kiton 8 6,649 Jun-29-2017, 08:40 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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