数据集中两点之间的最大距离并识别这些点 [英] Max Distance between 2 points in a data set and identifying the points

查看:38
本文介绍了数据集中两点之间的最大距离并识别这些点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个点的 x,y,z 坐标组成的矩阵.我想找到极值点,即相距最远的两点.

I have a matrix consisting of x,y,z coordinates of several points. I would like to find the extremum points i.e. the two points that are farthest apart.

我可以在 matlab 中找到一种方法,但我需要在 Python 中使用它

I could figure out a way in matlab, but i need it in Python

这是matlab中的代码

Here is the code in matlab

A = randint(500,3,[-5 5]);
D=pdist(A);
D=squareform(D);
[N,I]=max(D(:));
[I_row, I_col] = ind2sub(size(D),I);

pdist 给出点对之间的距离(i,j).squareform 给出矩阵输出在最后两个步骤中,我尝试找到矩阵 I_row、I_col 的索引..

pdist gives the distance between pairs of points(i,j). squareform gives the matrix output In last two steps I attempt to find the indices of the matrix I_row, I_col..

点 I_row 和 I_col 具有最大距离..

Points I_row and I_col have the max distance..

谁能建议我使用 Python 的有效方法,因为我所有的其他代码都使用 Python.

Could anybody suggest me an efficient way in python as all my other codes are in Python.

推荐答案

如果你有 scipy,你就有大部分 matlab 核心函数的完全等效的:

If you have scipy, you have exact equivalent for most of matlab core functions :

from numpy import random, nanmax, argmax, unravel_index
from scipy.spatial.distance import pdist, squareform

A = random.randint(-5,5, (500,3))
D = pdist(A)
D = squareform(D);
N, [I_row, I_col] = nanmax(D), unravel_index( argmax(D), D.shape )

您也可以使用 itertools 在纯 python 中获取它:

You can also get it in pure python using itertools :

from itertools import combinations
from random import randint

A = [[randint(-5,5) for coord in range(3)] for point in range(500)]

def square_distance(x,y): return sum([(xi-yi)**2 for xi, yi in zip(x,y)])    

max_square_distance = 0
for pair in combinations(A,2):
    if square_distance(*pair) > max_square_distance:
        max_square_distance = square_distance(*pair)
        max_pair = pair

这篇关于数据集中两点之间的最大距离并识别这些点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆