numpy的:将argsort到一个数组 [英] numpy: applying argsort to an array

查看:105
本文介绍了numpy的:将argsort到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

argsort () 函数返回指数的矩阵,可用于索引原始数组,这样的结果将匹配排序()的结果。

有没有应用这些指数的方法吗?我有两个阵列,一个是用于获得的排序顺序的阵列,并且另一个是一些相关的数据。

Is there a way to apply those indices? I have two arrays, one is the array used for obtaining the sort order, and another is some associated data.

我想计算 assoc_data [array1.argsort()] ,但似乎并没有工作。

I would like to compute assoc_data[array1.argsort()] but that doesn't seem to work.

下面是一个例子:

z=array([1,2,3,4,5,6,7])
z2=array([z,z*z-7])
i=z2.argsort()


z2=array([[ 1,  2,  3,  4,  5,  6,  7],
          [-6, -3,  2,  9, 18, 29, 42]])
i =array([[1, 1, 1, 0, 0, 0, 0],
          [0, 0, 0, 1, 1, 1, 1]])

我想申请我到Z2(或相关的数据另一个数组),但我不知道怎么做。

I would like to apply i to z2 (or another array with associated data) but I'm not sure how to do so.

推荐答案

这可能是矫枉过正,但是这将在次情况下工作:

This is probably overkill, but this will work in the nd case:

import numpy as np
axis = 0
index = list(np.ix_(*[np.arange(i) for i in z2.shape]))
index[axis] = z2.argsort(axis)
z2[index]

# Or if you only need the 3d case you can use np.ogrid.

axis = 0
index = np.ogrid[:z2.shape[0], :z2.shape[1], :z2.shape[2]]
index[axis] = z2.argsort(axis)
z2[index]

这篇关于numpy的:将argsort到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆