Sorting values in an array: 'reverse' is an invalid keyword argument for this function
14,238
You couldn't use the key
or reverse
keyword arguments according to numpy documentation. You can sort the array in ascending order and then reverse it with the [::-1]
slice or use the reversed()
view.
Comments
-
S Verhoef almost 2 years
I try to sort values in an array in descending order. If I try it in ascending order it works, but when i do it descending I get an error.
e = np.array([[5.,3.,8.],[6.,7.,1.],[4.,8.,2.]]) e.sort()
result:
e = array([[3.,5.,8.],[1.,6.,7.],[2.,4.,8.]])
now in reverse order:
e.sort(reverse=True)
result:
TypeError: 'reverse' is an invalid keyword argument for this function
I also tried
e.sort(key=itemgetter(1))
afterfrom operator import itemgetter
but the same error appears ('reverse' is replaced by 'key').Why is this the case? Why does it not work? Why this error (this is the way to use key or reverse right)?
-
S Verhoef over 6 yearsI've found my result here: link
f = -np.sort(-e)
worked perfect!
-
-
S Verhoef over 6 yearsI've also some nan values in my dataset which should stay at the end of the array. By using fliplr these would be put in front. Otherwise this would be perfect.
-
S Verhoef over 6 yearsNo column is of special interest, all rows should be sorted indivudually.
-
mgilson over 6 years@SVerhoef -- I guess I don't understand what you're getting at with they
key=itemgetter(1)
then ...