【问题标题】:Sorting points from distance to a given point x,y here in my case (x=0,y=o)在我的例子中,从距离到给定点 x,y 排序点 (x=0,y=o)
【发布时间】:2018-03-16 05:54:50
【问题描述】:

我想将数组“a”(如下所示)排序(从最短到最长)到距原点或点(在我的情况下为 0,0)的距离,并将其存储到类似的数组类型“b”中' 或替换数组 'a'

以下给定点是 3d numpy 数组

[[[  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.]]]

我找不到任何有关此类 3d np 数组排序的有用信息

ps:这些点 'a' 是从 Goodfeaturestotrack 、OPEN CV 、python 3.6 获得的

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

 #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 ??**

【问题讨论】:

  • del c[:] 将删除 c 中的所有内容,而不是 c= []
  • 谢谢! cp=np.insert(cp,c,0) 这是将 c 数组复制到 cp 的正确方法吗?
  • insert 将新列添加到数组中,但不会覆盖原始数组。 Numpy 实际上在他们的网站上有很好的文档和示例
  • 肯定会检查出来的!

标签: python arrays sorting numpy opencv3.0


【解决方案1】:

我喜欢方便地计算几种数组格式的距离...它不是单行的,但它可以工作。通过搜索“einsum”和 numpy 作为关键字,可以在堆栈的其他地方找到有关其实现的详细信息。 根据 np 要求导入 numpy,这只是 def,你需要 2 个数组

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

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

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 获取排序值,如下例所示

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]])

【讨论】:

  • 一旦我在我的代码中实现了这个,我会回复你的!谢谢!
  • 就像一个魅力! !猜错了np.argsort(art.e_dist(a, orig))一定是np.argsort(e_dist(a, orig))是这样吗?? art 是数组名吗?
  • 对不起...艺术来自我的 arraytool 模块...我忘了删除它,因为我从那里导入了 e_dist。如果您在自己的脚本中包含 e_dist,则不需要“艺术”部分
  • 是的,我确实删除了它!这段代码比 Beasley 先生提到的代码很多。谢谢!
【解决方案2】:
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]))

【讨论】:

  • 如何获取排序好的数组?如果我分配b=sorted(a,key=lambda point:distance_squared(target_point[0],target_point[1],*point[0]))print ( 'sorted',b) 我得到sorted [array([[ 250., 92.]], dtype=float32), array([[ 269., 70.]], dtype=float32), array([[ 407., 268.]], dtype=..... 我怎样才能得到一个类似上面的数组但排序
  • 试试 numpy.array(b)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多