Listing of Helpful Numpy Functions

This is a listing of functions in numpy/scipy that members of the group have found particularly useful. The idea isn’t to be a complete listing nor to provide detailed documentation. Rather we’re hoping that this will serve as a jumping off point for you to investigate for yourself and keep you from feeling like you’re drowning in the docs.

We’ve provided the broad topics, General, DMC, and DVR, Optimization

General

  • arange: creates an array of evenly spaced values over a given interval
  • linspace: creates an array of evenly spaced values of a given number over a given interval
  • average: takes the average of an array
  • argsort: returns the indices of the sorted array
  • argmax: returns the indices of the maximum element
  • argwhere/where: return either indices (argwhere) or elements (where) chosen that fulfill a certain condition.
  • logical operations: returns elementwise truth values of an array given two conditions using logical_and, logical_xor, etc.
  • sort/lexsort: sort will sort elements along a specified axis, lexsort allows you to do nested sorting (sort by array a, then array b)

Numerical

  • exp: calculates the exponential
  • power: raises a value to a given power (NumPy equivalent of **)
  • pi: returns the value of pi
  • rad2deg/deg2rad: converts to and from degrees/radians.
  • Also look at other constants numpy knows here

Arrays

  • zeros: creates a new array full of the value 0
  • ones: creates a new array full of the value 1
  • eye: creates an identity matrix of whatever size you need
  • array: creates a numpy array (useful for making lists into numpy arrays)
  • reshape: reshapes an array
  • delete: deletes specified elements of an array
  • transpose: tranposes the elements of an array
  • diag: either brodcasts a 1D array to the diagional of a 2D array or pulls the diagonal of a 2D array
  • column_stack: stacks 1D arrays column-wise into a 2D array
  • concatentate: concatenate arrays along a specified axis
  • in1d: checks if elements of one array are in another
  • flip: returns the reversed array. Last element is first, etc.

Linear Algebra

  • dot: computes a dot product of 2 arrays
  • inner: computes the inner porduct of 2 arrays
  • outer: computes the outer product of 2 arrays

    numpy.linalg

    Note: If you use import numpy as np in order to call these functions you will need to use np.linalg.norm. Or, you can do import numpy.linalg as la and do la.norm

  • norm: gets the norm of a vector
  • eigh: computes the eigenvalues and eigenvectors of a Hermitian matrix
  • For more see here

DMC

  • random.random: generates a random number between two inputs
  • random.normal: generates a random number chosen from a normal (gaussian) distribution.
  • unique: how many unique elements in an array, can return counts and/or indices. Good for discrete descendant weighting.

DVR

Optimization


Adding to the List

If you want to add to the list, feel free. All we’re gonna ask is that you follow a basic template. First decide which of the existing headings your function should go under, then add your function using Markdown like

* [<name of function>](<URL of documentation page>): brief description of what it does

At this point in time, that’s all we’re asking, but the hope is that it’ll keep entropy at bay.

Next: Usage Examples
Previous: NumPy 101

Got questions? Ask them on the McCoy Group Stack Overflow


Edit on GitHub