简体   繁体   English

从一个点到所有其他点的距离总和

[英]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 cdist - 你可以使用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]])

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.

相关问题 计算矩阵中所有其他点之间的距离 - Calculate Distances Between One Point in Matrix From All Other Points 计算二维坐标系中一个点与所有其他点之间的欧几里得距离 - Calculate Euclidean Distances Between One Point in a Bidimensional Coordinate System From All Other Points 计算点相对于参考点云的距离 - Compute distances of points relatively to reference point cloud NumPy:向量化到一组点的距离之和 - NumPy: vectorize sum of distances to a set of points 增加点之间的所有距离 python - Increase all distances between points python 从字典中的点找出大圆的距离 - Finding Great circle distances from points in dictionary 最小化相互不相交的二分点对之间的距离总和 - Minimize sum of distances between mutually disjoint bipartite pair of points 有没有更快的方法来计算两点矩阵的平方距离之和? - Is there a faster way to compute the sum of squared distances of two point matrices? 如何找到与其他三个点相距特定距离的点的坐标? - How to find coordinate of a point at a specific distance from three other points? 计算从一个列表样本到所有其他列表数据样本的距离 - Calculate distances from one list sample to all other list data samples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM