The sum of the distances from a point to all other points

I have two lists

available_points = [[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]

and

solution = [[3,5], [2,1]]

I am trying to put a point in available_points and add it to the solution , for which the sum of the Euclidean distances from this point to all points in the solution is the largest.

So I would get this

solution = [[3,5], [2,1], [51,35]]


I was able to select the starting 2 most distant points like this, but not sure how to proceed.

 import numpy as np from scipy.spatial.distance import pdist, squareform available_points = np.array([[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]) D = squareform(pdist(available_points) I_row, I_col = np.unravel_index(np.argmax(D), D.shape) solution = available_points[[I_row, I_col]] 

which gives me

solution = array([[1, 2], [51, 35]])

+5
source share
4 answers

You can use cdist -

 In [1]: from scipy.spatial.distance import cdist In [2]: max_pt=available_points[cdist(available_points, solution).sum(1).argmax()] In [3]: np.vstack((solution, max_pt)) Out[3]: array([[ 3, 5], [ 2, 1], [51, 35]]) 
+1
source

Since you mark numpy

 import numpy as np solution=np.array(solution) available_points=np.array(available_points) l=[] for x in solution: l.append(np.linalg.norm(available_points-x, keepdims=True,axis=1)) np.append(solution,[available_points[np.argmax(np.array(l).sum(0))]],axis=0) Out[237]: array([[ 3, 5], [ 2, 1], [51, 35]]) 
+2
source

You can use the maximum function to find the maximum in the list of available "access points", and then add the maximum number of "available_search" to the list of "solution"! I am also attaching an exit screenshot!

 available_points = [[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]; solution = [[3,5], [2,1]] solution.append(max(available_points)); print(solution); 

output

+1
source

I figured it out with cdist

 from scipy.spatial.distance import cdist d = cdist(solution, available_points) distances = [] for q in range(len(available_points)): y = d[:,q] distances.append(sum(y)) # Largest Distance max_point = available_points[distances.index(max(distances))] # Update datasets solution = np.append(solution, [max_point], axis=0) universe = np.delete(available_points, max_index, 0) 
0
source

Source: https://habr.com/ru/post/1274651/


All Articles