我有一个数组x
,argsorting的结果为i
.我需要根据i
对x
(和y
,这里无关紧要)进行数百次排序.因此,对数据进行两次排序是不可能的,一切都需要通过初始排序i
来实现.
如果我取x[i]
,它会像预期那样返回排序后的x
.然而,我现在只想通过n
使用某些行(每行x
).因此x[n]
如预期返回x
的值.
我的问题是我需要通过i
对这些x[n]
进行排序(并且必须对y[n]
进行同样的处理.
# Example data
x = np.array([14, 15, 9, 6, 19, 18, 4, 11, 10, 0])
i = np.argsort(x)
n = np.array([2, 5, 7, 8])
#x[n] -> array([ 9, 18, 11, 10])
所需输出:index_sort(x, n, i) = array([ 9, 10, 11, 18])
Some simple (failed) attempts:
x[n][i]
-> Indexing error, as x is now too small.
x[i[n]] -> array([ 6, 11, 15, 18])
, Is sorted, but contains the wrong data
x[i][n]
-> Same
For more context: I'm creating a specific type of decision tree model. For each layer of the tree I need the above operation a different n
. Sorting becomes prohibitively expensive and even checking for set membership via np.isin
might be too slow as well already. My intuition (though perhaps wrong) says it should be possible to achieve this via indexing only, without ever having to sort or check for set membership.
For all these layers x
and i
remain the same, but a different n
is used each time.