Python,numpy排序数组 [英] Python, numpy sort array

查看:101
本文介绍了Python,numpy排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy并具有一个包含一些值的数组(ndarray类型).此数组的形状为1000x1500.我重塑了

I'am using numpy and have an array (ndarray type) which contain some values. Shape of this array 1000x1500. I reshaped it

brr = np.reshape(arr, arr.shape[0]*arr.shape[1])

当我尝试

brr.reverse()
AttributeError: ‘numpy.ndarray’ object has no attribute ‘reverse’

获取错误. 我怎样才能对这个数组进行排序?

get error. How I can sort this array ?

推荐答案

如果您只想撤消它:

brr[:] = brr[::-1]

实际上,这沿轴0反转.如果数组有多个轴,则还可以在其他任何轴上还原.

Actually, this reverses along axis 0. You could also revert on any other axis, if the array has more than one.

以相反的顺序排序:

>>> arr = np.random.random((1000,1500))
>>> brr = np.reshape(arr, arr.shape[0]*arr.shape[1])
>>> brr.sort()
>>> brr = brr[::-1]
>>> brr
array([  9.99999960e-01,   9.99998167e-01,   9.99998114e-01, ...,
     3.79672182e-07,   3.23871190e-07,   8.34517810e-08])

或使用argsort:

or, using argsort:

>>> arr = np.random.random((1000,1500))
>>> brr = np.reshape(arr, arr.shape[0]*arr.shape[1])
>>> sort_indices = np.argsort(brr)[::-1]
>>> brr[:] = brr[sort_indices]
>>> brr
array([  9.99999849e-01,   9.99998950e-01,   9.99998762e-01, ...,
         1.16993050e-06,   1.68760770e-07,   6.58422260e-08])

这篇关于Python,numpy排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆