How do I get indices of N maximum values in a NumPy array?
Newer NumPy versions (1.8 and up) have a function called argpartition
for this. To get the indices of the four largest elements, do
>>> a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> a
array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> ind = np.argpartition(a, -4)[-4:]
>>> ind
array([1, 5, 8, 0])
>>> top4 = a[ind]
>>> top4
array([4, 9, 6, 9])
Unlike argsort
, this function runs in linear time in the worst case, but the returned indices are not sorted, as can be seen from the result of evaluating a[ind]
. If you need that too, sort them afterwards:
>>> ind[np.argsort(a[ind])]
array([1, 8, 5, 0])
To get the top-k elements in sorted order in this way takes O(n + k log k) time.
Get indices of N maximum values in a numpy array without sorting them?
Use numpy.argpartition()
:
k = 3
np.argpartition(arr, len(arr) - k)[-k:]
Adjust k
index to whatever you need.
NOTE: returned indices are not guaranteed to be in the "sorted order" - just that anything past index k
is larger than the value at position k
in the sorted array.
NOTE 2: If you do need the returned indices to be sorted themselves, then simply add a numpy.sort()
to the above command:
np.sort(np.argpartition(arr, len(arr) - k)[-k:])
numpy.argpartition()
provides significant performance gains over full sort
especially for large arr
. In the above example you do a full sort only over the selected indices (not all).
How to get first 5 maximum values from numpy array in python?
You can do this (each step is commented for clarity):
import numpy as np
x = np.array([3, 4, 2, 1, 7, 8, 6, 5, 9])
y = x.copy() # <----optional, create a copy of the array
y = np.sort(x) # sort array
y = y[::-1] # reverse sort order
y = y[0:5] # take a slice of the first 5
print(y)
The result:
[9 8 7 6 5]
how to get the index of the largest n values in a multi-dimensional numpy array
I don't have access to bottleneck
, so in this example I am using argsort
, but you should be able to use it in the same way:
#!/usr/bin/env python
import numpy as np
N = 4
a = np.random.random(20).reshape(4, 5)
print(a)
# Convert it into a 1D array
a_1d = a.flatten()
# Find the indices in the 1D array
idx_1d = a_1d.argsort()[-N:]
# convert the idx_1d back into indices arrays for each dimension
x_idx, y_idx = np.unravel_index(idx_1d, a.shape)
# Check that we got the largest values.
for x, y, in zip(x_idx, y_idx):
print(a[x][y])
A fast way to find the largest N elements in an numpy array
The bottleneck
module has a fast partial sort method that works directly with Numpy arrays: bottleneck.partition()
.
Note that bottleneck.partition()
returns the actual values sorted, if you want the indexes of the sorted values (what numpy.argsort()
returns) you should use bottleneck.argpartition()
.
I've benchmarked:
z = -bottleneck.partition(-a, 10)[:10]
z = a.argsort()[-10:]
z = heapq.nlargest(10, a)
where a
is a random 1,000,000-element array.
The timings were as follows:
bottleneck.partition()
: 25.6 ms per loopnp.argsort()
: 198 ms per loopheapq.nlargest()
: 358 ms per loop
How to get the index of maximum values along 2d in a 4d numpy array
argmax
only accepts scalar axis values (some other functions allow tuples). But we can reshape B
:
In [18]: B.reshape(6,2,2).argmax(axis=0)
Out[18]:
array([[5, 1],
[4, 0]])
and convert those back to 2d indices:
In [21]: np.unravel_index(_18,(2,3))
Out[21]:
(array([[1, 0],
[1, 0]]),
array([[2, 1],
[1, 0]]))
those values could be reordered in various ways, for example:
In [23]: np.transpose(_21)
Out[23]:
array([[[1, 2],
[1, 1]],
[[0, 1],
[0, 0]]])
To find N Maximum indices of a numpy array whose corresponding values should greater than M in another array
You are almost there. This should do what you want:
speed_20 = np.where(Speed > 20)[0]
sort = np.argsort(-Brake[speed_20])
result = speed_20[sort[:N]]
Python: Find most big Top-n values' index in List or numpy.ndarry
Sort the index by value,
def most_find(sequence, n):
lst = sorted(range(len(sequence)), key=lambda x:sequence[x], reverse=True)
return lst[:n]
a = [1, 5, 6, 2, 3]
result = most_find(a, 3)
print(result)
How to find multiple indices in numpy array
To find the first value that is closest to your input, you could look at the index where the absolute difference is lowest. Something like so:
closest_idx = np.abs(array - 3.3).argmin()
To then find the indices of all values that have the same (closest) value:
closest_val = array[min_idx]
all_closest_idx = np.argwhere(array == closest_val)
Get the indices of N highest values in an ndarray
You can use numpy.argpartition
on flattened version of array first to get the indices of top k
items, and then you can convert those 1D indices as per the array's shape using numpy.unravel_index
:
>>> arr = np.arange(100*100*100).reshape(100, 100, 100)
>>> np.random.shuffle(arr)
>>> indices = np.argpartition(arr.flatten(), -2)[-2:]
>>> np.vstack(np.unravel_index(indices, arr.shape)).T
array([[97, 99, 98],
[97, 99, 99]])
)
>>> arr[97][99][98]
999998
>>> arr[97][99][99]
999999
Related Topics
Why Does the Expression 0 ≪ 0 == 0 Return False in Python
How to Prettyprint a Json File
How to Properly Determine the Current Script Directory
Converting Unix Timestamp String to Readable Date
How to Iterate Over Files in a Given Directory
In Python, How to Determine If an Object Is Iterable
Bare Asterisk in Function Arguments
Using @Property Versus Getters and Setters
How to Step Through Python Code to Help Debug Issues
What Is the Result of % in Python
How to Add New Keys to a Dictionary
How to Fix "Runtimeerror: Package Fails to Pass a Sanity Check" For Numpy and Pandas
How to Send an Email With Gmail as Provider Using Python
Convert a Pandas Dataframe to a Dictionary
"Unicode Error "Unicodeescape" Codec Can't Decode Bytes... Cannot Open Text Files in Python 3