Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Searching


NumPy has several functions that can search through arrays:

  • The argmax() function gives the indices of the maximum values of an array:

    >>> a = np.array([2, 4, 8])
    >>> np.argmax(a)
    2
    
  • The nanargmax() function does the same, but ignores NaN values:

    >>> b = np.array([np.nan, 2, 4])
    >>> np.nanargmax(b)
    2
    
  • The argmin() and nanargmin() functions provide similar functionality but pertaining to minimum values. The argmax() and nanargmax() functions are also available as methods of the ndarray class.

  • The 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]])
    
  • The searchsorted() function tells you the index in an array where a specified value belongs to maintain the sort order. It uses binary search (see https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search), which is...