python - how to get the index of the largest n values in a multi-dimensional numpy array -
this question has answer here:
i want index of largest n values in multi-dimensional numpy array. index of largest n values in one-dimensional numpy array, found this. after test in interactive shell in python, seems bottleneck.argpartsort
can't effect on multi-dimensional numpy array. index of largest value in multi-dimensional numpy array, found this. can't largest n. method can give translate multi-dimensional numpy array list of {value:index}
(index present tuple), , sort list value, , index it. there more easier or more performance?
i don't have access bottleneck
, in example using argsort
, should able use in same way:
#!/usr/bin/env python import numpy np n = 4 = np.random.random(20).reshape(4, 5) print(a) # convert 1d array a_1d = a.flatten() # find indices in 1d array idx_1d = a_1d.argsort()[-n:] # convert idx_1d indices arrays each dimension x_idx, y_idx = np.unravel_index(idx_1d, a.shape) # check got largest values. x, y, in zip(x_idx, y_idx): print(a[x][y])
Comments
Post a Comment