简体   繁体   English

按元素排序多个numpy数组

[英]Sorting multiple numpy arrays elementwise

Suppose I have 3 arrays (I know the number of arrays beforehand) that I want to sort, element by element. 假设我有3个要逐元素排序的数组(我事先知道数组的数量)。 For example, suppose I have: 例如,假设我有:

import numpy as np

x = np.array([
[100, 200, 300],
[400, 500, 600],
[700, 800, 900]
])

y = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])

z = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])

And I'd want to get the order for each element: 我想获得每个元素的顺序:

[
[[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]],
[[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]],
[[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]], [[0, 2, 1], [0, 2, 1], [0, 2, 1]]
]

In this case all the positions are on the same order to keep the example simple (0 > 2 > 1), but in reality each element on each array could be bigger than its counterpart on another array. 在这种情况下,所有位置都以相同的顺序排列,以使示例保持简单(0> 2> 1),但实际上,每个数组上的每个元素都可能大于另一个数组上的对应元素。 I'm also working with higher dimension arrays (3d rather than 2d), but I think the general idea would be the same. 我也在使用高维数组(3d而不是2d),但是我认为总体思路是相同的。

I believe I need to reshape the arrays and then np.argsort them (or just regular np.sort , I'm more interested in the values rather than where they came from, though if the overhead of having the origin isn't high, it might be useful down the line). 我相信我需要重塑数组,然后np.argsort它们进行np.argsort (或只是常规np.sort ,我对值更感兴趣,而不是它们来自何处,尽管如果拥有原点的开销不高,可能对您很有帮助)。 But I'm not sure how to reshape them such that I can apply the operation. 但是我不确定如何重塑它们,以便可以应用该操作。 All arrays are guaranteed to have the same shape, and despite my example here, it will always be an even number. 保证所有数组都具有相同的形状,尽管这里有我的示例,但它始终是偶数。

I'm not sure if the format I have in mind would be good for indexing after I have the order, but at the moment I can't figure out a better way to group the results. 我不确定我下定单后想到的格式是否适合索引,但是目前我无法找到一种更好的方法来对结果进行分组。 I believe I can just split them later with something like answer[...,0] for the maximum indexes (or maximum values), answer[...,1] for the second and so on. 我相信我可以稍后将它们拆分,例如使用answer[...,0]表示最大索引(或最大值),使用answer[...,1]表示第二个索引,依此类推。 Though if I have the indexes, I'd have to map them later on to the arrays. 尽管如果我有索引,我将不得不稍后将它们映射到数组。 Shouldn't be that hard I think, I'd just have to np.stack them together I believe. 我认为应该没那么难,我只需np.stack它们np.stack在一起就可以了。

Any suggestions? 有什么建议么?

You can use dstack() to bind them together over the last axis and then use np.sort() over the expected axis to sort the array out: 您可以使用dstack()在最后一个轴上将它们绑定在一起,然后在期望的轴上使用np.sort()来整理数组:

In [10]: arr = np.dstack((x, y, z))

In [11]: arr.sort(2)

In [12]: arr
Out[12]: 
array([[[  1,  10, 100],
        [  2,  20, 200],
        [  3,  30, 300]],

       [[  4,  40, 400],
        [  5,  50, 500],
        [  6,  60, 600]],

       [[  7,  70, 700],
        [  8,  80, 800],
        [  9,  90, 900]]])

And if you want the result in a descending order: 如果您希望结果按降序排列:

In [13]: arr[:,:,::-1]
Out[13]: 
array([[[100,  10,   1],
        [200,  20,   2],
        [300,  30,   3]],

       [[400,  40,   4],
        [500,  50,   5],
        [600,  60,   6]],

       [[700,  70,   7],
        [800,  80,   8],
        [900,  90,   9]]])

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

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