Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
撤消或反转argsort(),python_Python_Arrays_Sorting_Numpy - Fatal编程技术网

撤消或反转argsort(),python

撤消或反转argsort(),python,python,arrays,sorting,numpy,Python,Arrays,Sorting,Numpy,给定一个数组“a”,我想按列对数组排序sort(a,axis=0)对数组执行一些操作,然后撤消排序。我的意思不是重新排序,而是基本上颠倒每个元素的移动方式。我假设argsort()是我所需要的,但是我不清楚如何使用argsort()的结果对数组进行排序,或者更重要的是应用argsort() 这里有更多的细节 我有一个数组a,shape(a)=rXc我需要对每个列进行排序 aargsort = a.argsort(axis=0) # May use this later aSort = a.so

给定一个数组“a”,我想按列对数组排序
sort(a,axis=0)
对数组执行一些操作,然后撤消排序。我的意思不是重新排序,而是基本上颠倒每个元素的移动方式。我假设
argsort()
是我所需要的,但是我不清楚如何使用
argsort()
的结果对数组进行排序,或者更重要的是应用
argsort()

这里有更多的细节

我有一个数组
a
shape(a)=rXc
我需要对每个列进行排序

aargsort = a.argsort(axis=0)  # May use this later
aSort = a.sort(axis=0)
现在平均每行

aSortRM = asort.mean(axis=1)
现在用行平均值替换一行中的每个列。 还有比这更好的方法吗

aWithMeans = ones_like(a)
for ind in range(r)  # r = number of rows
    aWithMeans[ind]* aSortRM[ind]

现在我需要撤销我在第一步中所做的排序

我不确定如何在
numpy
中最好地实现这一点,但是,在纯Python中,推理是:

aargsort
持有一个
range(len(a))
的排列,告诉您
aSort
的项来自何处——很像纯Python中的:

>>> x = list('ciaobelu')
>>> r = range(len(x))
>>> r.sort(key=x.__getitem__)
>>> r
[2, 4, 0, 5, 1, 6, 3, 7]
>>> 
i、 例如,
sorted(x)
的第一个参数是
x[2]
,第二个参数是
x[4]
,依此类推

因此,给定已排序的版本,您可以通过“将项目放回其来源”来重建原始版本:


当然,在
numpy
中会有更紧凑、更快速的方法来表达这一点(不幸的是,我对Python本身的了解不如对Python本身的了解;-),但我希望这有助于展示您需要执行的“将东西放回原位”操作的基本逻辑。

我无法遵循您的示例,但更抽象的问题——即如何对数组排序,然后反向排序——很简单

import numpy as NP
# create an 10x6 array to work with
A = NP.random.randint(10, 99, 60).reshape(10, 6)
# for example, sort this array on the second-to-last column, 
# breaking ties using the second column (numpy requires keys in
# "reverse" order for some reason)
keys = (A[:,1], A[:,4])
ndx = NP.lexsort(keys, axis=0)
A_sorted = NP.take(A, ndx, axis=0)
从排序后的数组“重建”数组是很简单的,因为请记住,您首先使用了索引数组(“ndx”)对数组进行排序

# ndx array for example above:  array([6, 9, 8, 0, 1, 2, 4, 7, 3, 5])

换句话说,A_排序中的第4行是原始数组中的第1行,A等。

对于您实际试图解决的问题,可能有比这更好的解决方案(执行argsort通常排除实际排序的需要),但现在您可以:

>>> import numpy as np
>>> a = np.random.randint(0,10,10)
>>> aa = np.argsort(a)
>>> aaa = np.argsort(aa)
>>> a # original
array([6, 4, 4, 6, 2, 5, 4, 0, 7, 4])
>>> a[aa] # sorted
array([0, 2, 4, 4, 4, 4, 5, 6, 6, 7])
>>> a[aa][aaa] # undone
array([6, 4, 4, 6, 2, 5, 4, 0, 7, 4])

对于所有仍在寻找答案的人:

In [135]: r = rand(10)

In [136]: i = argsort(r)

In [137]: r_sorted = r[i]

In [138]: i_rev = zeros(10, dtype=int)

In [139]: i_rev[i] = arange(10)

In [140]: allclose(r, r_sorted[i_rev])

Out[140]: True

比赛已经很晚了,但这里:

import numpy as np
N = 1000 # or any large integer
x = np.random.randn( N )
I = np.argsort( x )
J = np.argsort( I )
print( np.allclose( x[I[J]] , x ) )
>> True
基本上,argsort是argsort,因为反向排序的第n个元素是J[n]=k:I[k]=n。也就是说,I[J[n]=n,因此J对I进行排序。

index=np。argsort(a)
提供排序索引,因此
x=a[index]
是排序数组
y=b[索引]
将数组向前推到排序域
c[index]=z
z
从已排序域拉回到源域中的
c

比如说,

import numpy as np

n = 3
a = np.random.randint(0,10,n) # [1, 5, 2]
b = np.random.randn(n) # [-.1, .5, .2]
c = np.empty_like(a)

# indices that sort a: x=a[indices], x==np.sort(a) is all True
indices = np.argsort(a) # [0,2,1]

# y[i] is the value in b at the index of the i-th smallest value in a
y = b[indices] # [-.1, .2, .5]

# say z[i] is some value related to the i-th smallest entry in a 
z = np.random.randn(n) # [-1.1, 1.2, 1.3]
c[indices] = z # inverted the sorting map, c = [-1.1, 1.3, 1.2] 

为什么不能在任何转换之前复制数组:
a.copy()
,或者使用
aSort=numpy.sort(axis=0)
(这将返回已排序的副本)?顺便说一句,
a.sort()
不返回任何内容,因此没有必要指定它的返回值。@J.F.Sebastian,谢谢你说得对,我修正了它。我实际上想对每一列进行单独排序,我在顶部更正了我的代码,但我需要使用np.sort(a,axis=0),因此INX将是np.argsort(x,axis=0)这是迄今为止最好的解决方案。这是一个很好的答案,但它与其他解决方案有何不同?谢谢Dexter。都一样。更清晰,更普遍的测试,并实际解释,而不是仅仅给出。但我显然有偏见。但是与@paul.BTW一样,
zero(10,dtype=int)
可以被
zero\u替换,就像(i)
import numpy as np

n = 3
a = np.random.randint(0,10,n) # [1, 5, 2]
b = np.random.randn(n) # [-.1, .5, .2]
c = np.empty_like(a)

# indices that sort a: x=a[indices], x==np.sort(a) is all True
indices = np.argsort(a) # [0,2,1]

# y[i] is the value in b at the index of the i-th smallest value in a
y = b[indices] # [-.1, .2, .5]

# say z[i] is some value related to the i-th smallest entry in a 
z = np.random.randn(n) # [-1.1, 1.2, 1.3]
c[indices] = z # inverted the sorting map, c = [-1.1, 1.3, 1.2]