首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的例子中,从距离到给定的点x,y排序点(x=0,y=o)

在我的例子中,从距离到给定的点x,y排序点(x=0,y=o)
EN

Stack Overflow用户
提问于 2017-10-04 18:19:13
回答 2查看 2.9K关注 0票数 0

我希望排序(最短到最长)数组'a‘(如下所示)到原点或点的距离(在我的例子中为0,0),并将其存储到类似的数组类型'b’或替换数组'a‘。

下面给出的点是三维numpy数组。

代码语言:javascript
复制
[[[  510.    11.]]

 [[  651.   276.]]

 [[  269.    70.]]

 [[  920.    26.]]

 [[  513.    21.]]

 [[ 1197.   620.]]

 [[  407.   268.]]

 [[  452.    35.]]

 [[  435.     3.]]

 [[  520.    20.]]

 [[ 1151.   499.]]

 [[  104.    26.]]

 [[  754.    28.]]

 [[  263.   111.]]

 [[  731.    12.]]

 [[  972.   200.]]

 [[ 1186.   614.]]

 [[  437.     2.]]

 [[ 1096.    68.]]

 [[  997.   201.]]

 [[ 1087.   200.]]

 [[  913.   201.]]

 [[ 1156.   510.]]

 [[  994.   230.]]

 [[  793.    29.]]

 [[  514.    19.]]]

我找不到任何关于这种三维np数组排序的有用信息。

ps:这些积分'a‘是从Goodfeaturestotrack,OPEN CV,python 3.6获得的。

以及如何将数组清除为空类型?

代码语言:javascript
复制
 #this is  clustering algorithm    
    for index in range(len(a): #a is the above matrix 3d np array
#find distance was already defined and is euclidean distance formula
            if findDistance(a[index][0], a[index][1], a[index + 1][0], a[index + 1][1]) < 3: #calculation euclidean distance between ai and ai+1
                c.append(index)
            if findDistance(a[index][0], a[index][1], a[index + 1][0], a[index + 1][1]) > 3: #calculation euclidean distance between ai and ai+1
                if len(c) > 10:
                    cp = np.insert(cp, c, 0)

                    c = [] # should clear c **is this correct ??**
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-04 21:14:21

我喜欢用它来计算几种数组格式的距离.这不是一条条线,但很管用。有关其实现的详细信息可以通过搜索'einsum‘和numpy作为关键字在堆栈的其他地方找到。导入np所需的numpy,这只是def,需要2个数组

代码语言:javascript
复制
import numpy as np

def e_dist(a, b, metric='euclidean'):

"""Distance calculation for 1D, 2D and 3D points using einsum
: a, b   - list, tuple, array in 1,2 or 3D form
: metric - euclidean ('e','eu'...), sqeuclidean ('s','sq'...),
:-----------------------------------------------------------------------
"""
a = np.asarray(a)
b = np.atleast_2d(b)
a_dim = a.ndim
b_dim = b.ndim
if a_dim == 1:
    a = a.reshape(1, 1, a.shape[0])
if a_dim >= 2:
    a = a.reshape(np.prod(a.shape[:-1]), 1, a.shape[-1])
if b_dim > 2:
    b = b.reshape(np.prod(b.shape[:-1]), b.shape[-1])
diff = a - b
dist_arr = np.einsum('ijk,ijk->ij', diff, diff)
if metric[:1] == 'e':
    dist_arr = np.sqrt(dist_arr)
dist_arr = np.squeeze(dist_arr)
return dist_arr

然后,如果需要,可以对结果进行排序,例如

代码语言:javascript
复制
a = np.random.randint(0, 10, size=(10,2))

orig = np.array([0,0])

    e_dist(a, orig)

    array([  4.12,   9.9 ,   7.07,   6.08,   3.16,  10.63,   8.54,   7.28,   7.21,
             6.08])
    np.sort(e_dist(a, orig))

    array([  3.16,   4.12,   6.08,   6.08,   7.07,   7.21,   7.28,   8.54,   9.9 ,
            10.63])

增编

我应该补充说,您可以使用argsort获得排序的值,如下所示

代码语言:javascript
复制
np.argsort(e_dist(a, orig))
array([4, 0, 3, 9, 2, 8, 7, 6, 1, 5], dtype=int64)

idx = np.argsort(art.e_dist(a, orig))

closest = a[idx]
array([[3, 1],
       [1, 4],
       [1, 6],
       [6, 1],
       [5, 5],
       [4, 6],
       [2, 7],
       [8, 3],
       [7, 7],
       [7, 8]])
票数 1
EN

Stack Overflow用户

发布于 2017-10-04 18:30:35

代码语言:javascript
复制
def distance_squared(x1,y1,x2,y2):
    return (x1-x2)**2 + (y1-y2)**2
target_point = 0,0
sorted(a,key=lambda point:distance_squared(target_point[0],target_point[1],*point[0]))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46571624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档