Use the sort and argsort functions to retrieve array elements and indexes in descending order with numpy.
However, since the sort function sorts in ascending order and the argsort function returns an array of indexes sorted in ascending order, use the slice writing method [:: -1] to reverse the order, that is, in descending order.
python
import numpy as np
x = np.array([18, 7, 55, 31])
for i in range(len(x)):
print np.argsort(x)[::-1][i], np.sort(x)[::-1][i]
The output is as follows.
2 55
3 31
0 18
1 7
Recommended Posts