-1

We are trying to create group of co-ordinates which are in close range of each other. To create this group we need to compare x, y co-ordinates of numpy array with the 1st element in the arrary.

If they match we store that in one in an existing array and if not we create a new one and store them there.

We have tried it with a for loop and extract x and y seperately. But, it didnt work.


mport numpy as np 

coordinates = [(580.4625,201.87183),(580.4625,201.87183),(167.84459,701.64935),
              (167.84459,701.64935),(167.84459,694.65247),(167.84459,694.65247),
              (979.09344,725.6387),(979.09344,725.6387),(979.09344,725.6387),
              (979.09344,725.6387),(200.81406,1271.3956),(200.81406,1271.3956),
              (200.81406,1271.3956),(1005.0694,1266.398),(1005.0694,1266.398),
              (1005.0694,1266.398),(180.83257,1568.2635),(180.83257,1568.2635),
              (994.0796,1591.2533),(539.5005,1862.1327)] 

seen = set() 

newlist = [] 

for item in coordinates: 
    t = tuple(item) 
    if t not in seen: 
        newlist.append(item) 
        seen.add(t) 

npArray = np.array(newlist) 
newlist = npArray.astype(int) 
npArray2 = np.array(newlist) 
coordinates1 = npArray2.astype(int) 

for i in coordinates1[0]: 
    print(i[0])

We are trying to get the 1st element of the co-ordinates in above message in a seperate variable.

Currently we are getting an error: IndexError: invalid index to scalar variable.

We are new to python, so any guidence will be helpful, even if pointer to a better way of doing this.

Thanks!


Thank you for all the answers. I think my mistake is in not explaning the question. I will try here again:

coordinates = [(580.4625,201.87183),(580.4625,201.87183),(167.84459,701.64935),
              (167.84459,701.64935),(167.84459,694.65247),(167.84459,694.65247),
              (979.09344,725.6387),(979.09344,725.6387),(979.09344,725.6387),
              (979.09344,725.6387),(200.81406,1271.3956),(200.81406,1271.3956),
              (200.81406,1271.3956),(1005.0694,1266.398),(1005.0694,1266.398),
              (1005.0694,1266.398),(180.83257,1568.2635),(180.83257,1568.2635),
              (994.0796,1591.2533),(539.5005,1862.1327)] 

We are trying to create groups of co-ordinates which are in proximity/range of 30 points + or - of the 1st co-odinate in the above list.

So, for that we will need to compare all co-ordinates (x, y) one by one with the 1st co-rdiante and if they are in above said range, we will assign them to an group(eg.G1)/array with 1st co-ordiante as the 1st element in that group.

If not in range, we will create another group(eg.G2)/array and insert the co-ordiante not in range in this new group.

This way we go further comparing one by one each element in above list until all co-ordinates are compared and assigned to one of the groups.

Hope, I made this more clear.

Many thanks for your help!

Best Regards,

trivedi
  • 1
  • 3
  • So you want to compare each (x, y) coordinates to the first value of your coordinate list: `(580.4625,201.87183)`. If the (x, y) is close to this value, then it is placed in e.g. `L1` and if it is not, it is placed in e.g. `L2`. Am I correct? How do you define if (x, y) is close to `(580.4625,201.87183)`? Within a circle of radius `z`? – Mathieu Oct 14 '19 at 11:10
  • Possible duplicate of [Find unique rows in numpy.array](https://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array) – Nils Werner Oct 14 '19 at 11:18
  • By the way, the first for loop can be replaced by `set(coordinates)`. But we can't guide you in the right direction until we get what you want to achieve. Maybe you could provide an expected input/output. – Mathieu Oct 14 '19 at 11:19
  • yes you are right. We define closeness by within a 30 radius from given coordinates and this is not a list I just convert list into in a Numpy Array to fetch integer values. @ma – trivedi Oct 14 '19 at 11:22
  • Output be like: comparing array[580 201] with each other coordinates and try to find that coordinates lies in range of 550 to 610 for x and 171 to 231 for y if they are in then we create a group of that coordinates otherwise we create a new array then recursively we can check it. array after searching --> [580 201,[560 214],[552 230]] . – trivedi Oct 14 '19 at 11:33
  • @trivedi I just understood the second comment, I'll edit my answer to include the recursive approach which will create all clusters of points. However, what if a point is in range of 2 different clusters? – Mathieu Oct 14 '19 at 11:49
  • Well we see later but if you do this than it will be very helpful for me. Thank You – trivedi Oct 14 '19 at 12:02

3 Answers3

0

Try, it works,

    for i in coordinates1: 
        print(i[0])

as the shape of numpy array coordinates1 is (9,2), there is no need of coordinates[0] to access the x coordinate.

Pooja Sonkar
  • 132
  • 10
0

Since you are looking for points within a circle, here you can find a method to do it. You will need to adapt it to your needs which are not very clear.

import numpy as np

coordinates = [(580.4625,201.87183),(580.4625,201.87183),(167.84459,701.64935),
              (167.84459,701.64935),(167.84459,694.65247),(167.84459,694.65247),
              (979.09344,725.6387),(979.09344,725.6387),(979.09344,725.6387),
              (979.09344,725.6387),(200.81406,1271.3956),(200.81406,1271.3956),
              (200.81406,1271.3956),(1005.0694,1266.398),(1005.0694,1266.398),
              (1005.0694,1266.398),(180.83257,1568.2635),(180.83257,1568.2635),
              (994.0796,1591.2533),(539.5005,1862.1327)]

reference = (580.4625,201.87183)
z = 30 # Radius

L1 = list()
L2 = list()

for coord in coordinates:
    # coord is a tuple of 2 values, x and y.
    x = coord[0]
    y = coord[1]

    # Compute distance in 2D space
    d = np.sqrt((x-reference[0])**2+(y-reference[1])**2)

    # Add to the correct list
    if d <= z:
        L1.append(coord)
    else:
        L2.append(coord)

Additionally, those steps can be done with list comprehension which gives you a one-liner:

L1 = [coord for coord in coordinates if np.sqrt((coord[0]-reference[0])**2+(coord[1]-reference[1])**2) <= z]
L2 = [coord for coord in coordinates if not np.sqrt((coord[0]-reference[0])**2+(coord[1]-reference[1])**2) <= z]

Now, assuming you want to loop on the coordinates, and each time create the L1 list of near-by points, i.e. create the cluster of points within a z = 30 radius of the considered coordinate, you can create a list cluster and add each time the list of near-by points.

import numpy as np

coordinates = [(580.4625,201.87183),(580.4625,201.87183),(167.84459,701.64935),
              (167.84459,701.64935),(167.84459,694.65247),(167.84459,694.65247),
              (979.09344,725.6387),(979.09344,725.6387),(979.09344,725.6387),
              (979.09344,725.6387),(200.81406,1271.3956),(200.81406,1271.3956),
              (200.81406,1271.3956),(1005.0694,1266.398),(1005.0694,1266.398),
              (1005.0694,1266.398),(180.83257,1568.2635),(180.83257,1568.2635),
              (994.0796,1591.2533),(539.5005,1862.1327)]

z = 30 # Radius
clusters = list()

for ref in coordinates:
    # coord is a tuple of 2 values, x and y.
    x = ref[0]
    y = ref[1]

    clusters.append([coord for coord in coordinates \
          if np.sqrt((coord[0]-x)**2+(coord[1]-y)**2) <= z])

With this method, if a point is within range of 2 clusters, it will be included in both.

Addtionnaly, some points are present twice in coordinates. To delete duplicates, you can use the set.

coordinates = list(set(coordinates))

Example of cluster. The element (167.84459,701.64935) leads to the cluster [(167.84459, 701.64935), (167.84459, 694.65247)] (with duplicates taken of).

enter image description here

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • No, this is not useful for me as I just convert list to the numpy array and I have to compare that coordinates are lies in specific range then we append them in to array. For example we have coordinate array([580 180],[512 323] and so on. After that we take [580 180] so that we create group of 30 which will find x_min, x_max, y_min, y_max for range and if other coordinates lies in that range we append those arrays in main array and if not then we create new array. We don't want to find any circle radius. – trivedi Oct 14 '19 at 11:57
  • @trivedi Your comment here and the one on your main post are not very helpful in understanding your problem. On the main post, you say that this is what you need (see edit); and on this one you say it's not useful. However, it's the same approach that I simply modify a bit to loop on the coordinates. – Mathieu Oct 14 '19 at 12:04
  • can you send your email id? – trivedi Oct 14 '19 at 12:09
  • @trivedi Obviously no. Make a post in which the problem is clearly stated, with a minimum example to repoduce (and I insist on reproduce, no need for actual data) your problem, explain what you try to do, what the input is representing, and what the expected output is. – Mathieu Oct 14 '19 at 12:13
  • @trivedi I just noticed this sentence " array[580 201] [...] coordinates lies in range of 550 to 610 for x and 171 to 231 ". Compared to one of your previous comment, this is not a circle of radius 30 but a square centered around 580, 201 with an edge of 60. Which one it is you want, square or circle? The approach is similar anyway, it's simply a matter of changing the inclusion condition. – Mathieu Oct 14 '19 at 12:18
-1

I didn't get well what you meant, but try this:

import numpy as np 

coordinates = [(580.4625,201.87183),(580.4625,201.87183),(167.84459,701.64935),
              (167.84459,701.64935),(167.84459,694.65247),(167.84459,694.65247),
              (979.09344,725.6387),(979.09344,725.6387),(979.09344,725.6387),
              (979.09344,725.6387),(200.81406,1271.3956),(200.81406,1271.3956),
              (200.81406,1271.3956),(1005.0694,1266.398),(1005.0694,1266.398),
              (1005.0694,1266.398),(180.83257,1568.2635),(180.83257,1568.2635),
              (994.0796,1591.2533),(539.5005,1862.1327)] 

seen = set()
newlist = [] 

for item in coordinates: 
    t = tuple(item) 
    if t not in seen: 
        newlist.append(item) 
        seen.add(t) 

npArray = np.array(newlist) 
newlist = npArray.astype(int) 

for i in newlist: 
    print(i)
Markus
  • 2,265
  • 5
  • 28
  • 54
Marco
  • 1
  • 2