我正在尝试创建一个数组(10000,50)大小(我提到大小因为效率很重要),然后:
-
按升序排序前5000行
-
按降序对接下来的5000行进行排序 .
这是我的代码:
samples = 10 # I'm going to increase it 10000
sampleLength = 4 # I'm going to increase it 50
halfSamples = int(samples/2)
xx = numpy.multiply(10, numpy.random.random((samples, sampleLength)))
xx[0:halfSamples,0:sampleLength]=numpy.sort(xx[0:halfSamples,0:sampleLength],axis=1)
xx[halfSamples:samples,0:sampleLength]=numpy.sort(xx[halfSamples:samples,0:sampleLength],axis=1)
这按照升序对数组的一半进行排序,我唯一找不到的是在我的最后一行中给出的参数,以降序排列 .
我试过基于这个链接:Reverse sort a 2d numpy array in python
xx[halfSamples:samples,0:sampleLength]=numpy.sort(xx[halfSamples:samples,0:sampleLength:-1],axis=1)
但是得到了一个错误:
ValueError: could not broadcast input array from shape (5,0) into shape (5,4)
谢谢
1 回答
使用.sort方法对数组进行排序可能会更快,而不是返回副本的np.sort . 您可以使用负步长索引第二个维度,以按降序对最后5000行的列进行排序: