[英]Sum of 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'm trying to pop a point in available_points
and append it to solution
for which the sum of euclidean distances from that point, to all points in the solution
is the greatest. 我试图在
available_points
弹出一个点并将其附加到solution
中,从该点到欧几里德距离的总和,以及solution
中的所有点都是最大的。
So, I would get this 所以,我会得到这个
solution = [[3,5], [2,1], [51,35]]
I was able to select the initial 2 furthest points like this, but not sure how to proceed. 我能够选择这样的最初的2个最远点,但不知道如何继续。
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]])
Since you tag numpy
因为你标记
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]])
You can use the max function to find the maximum in a 'available_points' list & then append the maximum of 'available_points' list to 'solution' list ! 您可以使用max函数在'available_points'列表中找到最大值,然后将'available_points'列表的最大值附加到'solution'列表中! I am also attached the screenshot of the output !
我还附上了输出的截图!
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);
I figured it out with using cdist
我用
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.