1

我需要同时对两个数组进行排序,或者更确切地说,我需要对其中一个数组进行排序,并在排序时将其关联数组的相应元素带入其中。也就是说,如果数组是 [(5, 33), (4, 44), (3, 55)] 并且我按第一个轴排序(标记在 dtype='alpha' 下面),那么我想要: [(3.0, 55.0 ) (4.0, 44.0) (5.0, 33.0)]。这些是非常大的数据集,我需要先排序(对于 nlog(n) 速度),然后再执行一些其他操作。我不知道如何以正确的方式合并我的两个单独的数组以使排序算法正常工作。我认为我的问题很简单。我尝试了三种不同的方法:

import numpy
x=numpy.asarray([5,4,3])
y=numpy.asarray([33,44,55])

dtype=[('alpha',float), ('beta',float)]

values=numpy.array([(x),(y)])
values=numpy.rollaxis(values,1)
#values = numpy.array(values, dtype=dtype)
#a=numpy.array(values,dtype=dtype)
#q=numpy.sort(a,order='alpha')
print "Try 1:\n", values

values=numpy.empty((len(x),2))
for n in range (len(x)):
        values[n][0]=y[n]
        values[n][1]=x[n]
print "Try 2:\n", values
#values = numpy.array(values, dtype=dtype)
#a=numpy.array(values,dtype=dtype)
#q=numpy.sort(a,order='alpha')

###
values = [(x[0], y[0]), (x[1],y[1]) , (x[2],y[2])]
print "Try 3:\n", values
values = numpy.array(values, dtype=dtype)
a=numpy.array(values,dtype=dtype)
q=numpy.sort(a,order='alpha')

print "Result:\n",q

我注释掉了第一次和第二次尝试,因为它们会产生错误,我知道第三次会起作用,因为这反映了我在 RTFM 时看到的情况。给定数组 x 和 y (它们非常大,仅显示示例)如何构造可以由 numpy.sort 正确调用的数组(称为值)?

*** Zip 很好用,谢谢。额外的问题:我以后如何将排序后的数据再次解压缩到两个数组中?

4

6 回答 6

6

I think what you want is the zip function. If you have

x = [1,2,3]
y = [4,5,6]

then zip(x,y) == [(1,4),(2,5),(3,6)]

So your array could be constructed using

a = numpy.array(zip(x,y), dtype=dtype)
于 2009-02-18T10:19:56.290 回答
3

for your bonus question -- zip actually unzips too:

In [1]: a = range(10)
In [2]: b = range(10, 20)
In [3]: c = zip(a, b)
In [4]: c
Out[4]: 
[(0, 10),
 (1, 11),
 (2, 12),
 (3, 13),
 (4, 14),
 (5, 15),
 (6, 16),
 (7, 17),
 (8, 18),
 (9, 19)]
In [5]: d, e = zip(*c)
In [6]: d, e
Out[6]: ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (10, 11, 12, 13, 14, 15, 16, 17, 18, 19))
于 2009-02-18T20:11:46.803 回答
3

Simon suggested argsort as an alternative approach; I'd recommend it as the way to go. No messy merging, zipping, or unzipping: just access by index.

idx = numpy.argsort(x)
ans = [ (x[idx[i]],y[idx[i]]) for i in idx]
于 2009-07-05T18:09:08.297 回答
3

zip() might be inefficient for large arrays. numpy.dstack() could be used instead of zip:

ndx = numpy.argsort(x)
values = numpy.dstack((x[ndx], y[ndx]))
于 2010-04-10T07:44:08.143 回答
2

我认为您只需要在制作最终 ndarray 时指定要排序的轴。或者,对原始数组之一进行 argsort,您将拥有一个索引数组,可用于在 x 和 y 中查找,这可能意味着您根本不需要值。

(现在 scipy.org 似乎无法访问,否则我会向您发布一些文档的链接)

Given that your description doesn't quite match your code snippet it's hard to say with certainty, but I think you have over-complicated the creation of your numpy array.

于 2009-02-18T10:18:49.333 回答
1

I couldn't get a working solution using Numpy's sort function, but here's something else that works:

import numpy
x = [5,4,3]
y = [33,44,55]
r = numpy.asarray([(x[i],y[i]) for i in numpy.lexsort([x])])

lexsort returns the permutation of the array indices which puts the rows in sorted order. If you wanted your results sorted on multiple keys, e.g. by x and then by y, use numpy.lexsort([x,y]) instead.

于 2009-02-18T10:27:04.447 回答