繁体   English   中英

排序多维numpy数组

[英]Sorting a multi-dimensional numpy array

我想通过x坐标对2x2 numpy数组进行排序。 我的目标是获取一个数组,该数组从每对点中的最小X值到最大X值排序,同时使用该数组的所有值

使用以下代码行创建了数组:

rect = np.empty((4, 2, 2))

数组内部值的实际输出为:

[[[ -1000 , 97 ] #x0,y0 rect 0
   [999 , 98]]   #x1,y1 rect 0
  [[410 , -1048] #x0,y0 rect 1
   [619 , 940]]  #x1,y1 rect 1
  [[-1000, 226] 
   [999 , 227]]
  [[229 , -983]
   [55 , 1008]]]

期望的输出是按照构成矩形的每对点中X的最小值进行排序,然后考虑所有矩形,按X进行排序,如下所示:

 [[[ -1000 , 97 ] 
   [999 , 98]] 
  [[-1000, 226] 
   [999 , 227]]
  [[55 , 1008]
   [229 , -983]]
  [[410 , -1048] 
   [619 , 940]]]

如果要执行此操作而不创建其他数组副本,则可以使用argsort和indexing的组合来执行此操作。

import numpy as np
data = np.array(
 [[[ -1000 , 97 ],
   [999 , 98]],
  [[410 , -1048],
   [619 , 940]],
  [[-1000, 226] ,
   [999 , 227]],
  [[229 , -983],
   [55 , 1008]]])
def sortrect(rect):
    x = rect[:, 0]
    idx = np.argsort(x)
    rect[:] = rect[idx]

for a in data:
    sortrect(a)

minx = data[:, 0, 0]
idx = np.argsort(minx)
data[:] = data[idx]

同样的事情,没有循环,但是教学较少(对Martin的带有轴的argsort表示敬意):

idx0 = np.arange(data.shape[0])[:, np.newaxis]
idx1 = np.argsort(data[:, :, 0], axis=1)
data = data[idx0, idx1]

minx = data[:, 0, 0]
idx = np.argsort(minx)
data[:] = data[idx]

形式为out = data[idx0, idx1]的表达式表示

for all i, j:
    out[i, j] = data[idx0[i, j], idx1[i, j]].

有关更多详细信息,请参见https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing

再见循环和lambdas,欢迎速度

import numpy as np 

original_array = np.array([[[ -1000 , 97 ],[999 , 98]],
               [[410 , -1048],[619 , 940]],  #original_array1,y1 rect 1
                [[-1000, 226],[999 , 227]],
                [[229 , -983],[55 , 1008]]])

#get indices of sorted x0 and x1 (0 means first element of both arrays)
# indices are 2 arrays - [0,0,0,1], [1,1,1,0],
# so you can see that last element needs to reposition
indices = np.argsort(original_array[:,:,0],axis=1)

#create new array
new = np.empty(shape=[4,2,2])

#move on correct posisitions sub arrays
#np.arange only create artifical indices for each rect
new[:,0,:]=original_array[np.arange(original_array.shape[0]),indices[:,0],:]
new[:,1,:]=original_array[np.arange(original_array.shape[0]),indices[:,1],:]

#When subarrays are sorted, sort parent arrays
final_sorted_array = new[new[:,0,0].argsort(),:,:]
print(final_sorted_array)



 [[[-1000.    97.]
  [  999.    98.]]

 [[-1000.   226.]
  [  999.   227.]]

 [[   55.  1008.]
  [  229.  -983.]]

 [[  410. -1048.]
  [  619.   940.]]]

您可以为此使用sort函数的key参数:

l = [[[ -1000 , 97 ],[999 , 98]],
     [[410 , -1048], [619 , 940]],
     [[-1000, 226],[999 , 227]],
     [[229 , -983],[55 , 1008]]]
sorted(l, key=lambda x: (min(x[0][0], x[1][0]), max(x[0][0],x[1][0])))
>>> [[[-1000, 97],  [999, 98]],
     [[-1000, 226], [999, 227]],
     [[229, -983],  [55, 1008]],
     [[410, -1048], [619, 940]]]

排序后的lambda会创建一个包含x最小值和最大值的元组

而且,如果您使用的是Numpy,则可以编写一些可以在更高维度上更好地概括的内容:

sorted(l, key=lambda x: sorted(x[..., 0]))
>>> [array([[-1000,    97], [  999,    98]]), 
    array([[-1000,   226], [  999,   227]]), 
    array([[ 229, -983], [  55, 1008]]), 
    array([[  410, -1048], [  619,   940]])]

即使您有超过2个用于定义形状的点,该x也将起作用,并且将按最小x值进行排序

编辑:对矩形内的内部点进行排序的更正:

sorted(np.sort(l, axis=1), key=lambda x: tuple(x[..., 0]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM