如何按降序(numpy)对2d数组的一半进行排序

2024-09-29 23:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试创建一个数组(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

^{pr2}$

但有个错误:

ValueError: could not broadcast input array from shape (5,0) into shape (5,4)

谢谢


Tags: tonumpy排序itrandom数组sortsamples

热门问题