NumPy has several functions that can search through arrays, as follows:
argmax
function gives the indices of the maximum values of an array.>>> a = np.array([2, 4, 8]) >>> np.argmax(a) 2
nanargmax
function does the same but ignores NaN values.>>> b = np.array([np.nan, 2, 4]) >>> np.nanargmax(b) 2
argmin
and nanargmin
functions provide similar functionality but pertaining to minimum values.argwhere
function searches for non-zero values and returns the corresponding indices grouped by element.>>> a = np.array([2, 4, 8]) >>> np.argwhere(a <= 4) array([[0], [1]])
searchsorted
function tells you the index in an array where a specified value could be inserted to maintain the sort order. It uses binary search, which is a O(log n) algorithm. We will see this function in action shortly.extract
function retrieves values from an array based on a condition.