Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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])

when I trying

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

get error. How I can sort this array ?

share|improve this question
    
sort or reverse? –  Rohit Jain Feb 14 '13 at 12:52
    
what are you expecting reverse() to do? –  NPE Feb 14 '13 at 12:52
    
Doesn't solve your problem, but to make your code a little neater and more efficient you can use arr.size instead of arr.shape[0]*arr.shape[1]. –  Junuxx Feb 14 '13 at 12:59
    
I want to sort it descending. Btw sort() give the same error –  user2046488 Feb 14 '13 at 13:12

2 Answers 2

up vote 5 down vote accepted

If you just want to reverse it:

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

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

To sort in reverse order:

>>> 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])

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])
share|improve this answer
    
No, it is unsorted, I have to sort it descending –  user2046488 Feb 14 '13 at 13:10
    
Thanks! Tried argsort() with no luck. Did not knew about [::-1] –  user2046488 Feb 14 '13 at 13:36

Try this for sorting in descending order ,

import numpy as np
a = np.array([1,3,4,5,6])
print -np.sort(-a)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.