我试图创建一个数组(10000,50),大小(我提到的大小,因为效率是很重要的),然后输入:如何排序一半的二维数组大小排序(numpy的)
- 按升序对前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]'在最后一行的末尾? – Divakar