Python 在我的例子中,从距离到给定点x,y排序点(x=0,y=o)
我想将数组“a”(如下所示)排序(从最短到最长)到原点或点的距离(在我的例子中为0,0),并将其存储到类似的数组类型“b”或替换数组“a” 下面给出的点是3d numpy阵列Python 在我的例子中,从距离到给定点x,y排序点(x=0,y=o),python,arrays,sorting,numpy,opencv3.0,Python,Arrays,Sorting,Numpy,Opencv3.0,我想将数组“a”(如下所示)排序(从最短到最长)到原点或点的距离(在我的例子中为0,0),并将其存储到类似的数组类型“b”或替换数组“a” 下面给出的点是3d numpy阵列 [[[ 510. 11.]] [[ 651. 276.]] [[ 269. 70.]] [[ 920. 26.]] [[ 513. 21.]] [[ 1197. 620.]] [[ 407. 268.]] [[ 452. 35.]] [
[[[ 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、OpenCV、python 3.6
如何将数组清除为空类型
#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 ??**
#这是聚类算法
对于范围内的索引(len(a):#a是上述矩阵3d np数组
#“查找距离”已定义,是欧几里德距离公式
如果findInstance(a[index][0]、a[index][1]、a[index+1][0]、a[index+1][1])<3:#计算ai和ai+1之间的欧氏距离
c、 追加(索引)
如果FindInstance(a[index][0]、a[index][1]、a[index+1][0]、a[index+1][1])>3:#计算ai和ai+1之间的欧氏距离
如果len(c)>10:
cp=np.插入(cp,c,0)
c=[]#应清除c**是否正确**
我想让它方便地以多种数组格式计算距离……它不是一行,但可以工作。有关其实现的详细信息,可以在堆栈的其他位置通过搜索“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]])
如何获得排序数组?如果我分配
b=sorted(a,key=lambda point:distance_squared(target_point[0],target_point[1],*point[0])
和print('sorted',b)
我得到排序的[array([[250,92.]],dtype=float32),array([269,70.]],dtype=float32),array([407,268.]],dtype=..…
我怎样才能得到一个类似于上面的数组,但排序为try numpy。数组(b)和delc[:]
将删除c中的所有内容,而不是c=[]
谢谢!cp=np.insert(cp,c,0)
这是将c数组复制到cp的正确方法吗?insert向数组中添加了一个新列,但它不会覆盖原始数组。Numpy的网站上有非常好的文档和示例,请务必查看!一旦我在代码中实现了这一点,我将立即与u联系!谢谢!效果非常好!!我想这是一个mis取np.argsort(艺术区(a,原图))
必须是np.argsort(e区(a,原图))
是这样吗???art
是数组名吗?对不起……art来自我的arraytool模块……我忘记删除它了,因为我从那里导入了e_dist。如果你在自己的脚本中包含e_dist,那么你就不需要我删除的“art”部分了!此代码比Beasley先生提到的代码快得多。谢谢!
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]])