NumPy

To quote from the NumPy website,

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object; sophisticated (broadcasting) functions; tools for integrating C/C++ and Fortran code; useful linear algebra, Fourier transform, and random number capabilities.

Using NumPy on Flux

To use NumPy from any version of Python for which it is installed, use

import numpy

in your Python script or at the Python prompt. Once you have done that, NumPy functions are available as numpy.function(). It is not required, but it is conventional to use

import numpy as np

after which NumPy functions are available as np.function(). You will see many examples that use the import as np convention if you search for NumPy examples on the web. The Tentative NumPy Tutorial (tentative since 2012) is a good place to get a systematic overview.

Here are some very short, annotated, examples taken from the Wikipedia NumPy page.

The basic NumPy data structure is an array, which can be N-dimensional. To create an array, provide a list of homogeneous elements – that is, they all need to be of the same type: float, integer, etc.

import numpy as np
x = np.array([1, 2, 3])

Many uses of NumPy involve linear algebra, and many of the linear algebra functions are in a sublibrary called linalg. Functions can be called directly; for example, to create a variable Xprime that is the inverse of X, one could use

Xprime = np.linalg.inv(X)

The inv() function is part of the linalg namespace, and it is often convenient to import the function into the local namespace, so it can be more simply used. That can be done with

from numpy.linalg import inv
Xprime = inv(X)

Here is an example of solving a linear equation ax = b. We begin by defining a and b, then we use the imported function solve(). We will show this as if the commands are being run interactively from the Python prompt. To put them into a script, simply remove the >>> prompt. The line without the prompt is Python output.

>>> import numpy as np
>>> from numpy.linalg import solve
>>> a = np.array([[1, 2, 3], [3, 4, 6.7], [5, 9.0, 5]])
>>> b =  array([3, 2, 1])
>>> solve(a, b)
array([-4.83050847,  2.13559322,  1.18644068])

To assign the solution, x, of ax = b to a variable x, use

>>> x = solve(a, b)
>>> x
array([-4.83050847,  2.13559322,  1.18644068])

Additional NumPy documentation

There are many good resources on the web on NumPy and how to use it, here is a list of just a few.

SciPy

SciPy is a collection of libraries useful for scientific computing. Typically, the sublibrary that is needed will be imported. Here is a short example, using a function from the scipy.stats library. Put the following commands into a file called desc_statistics.py

import numpy as np
from scipy.stats import norm

np.random.seed(1234567)

X =  norm.rvs(10, 2, size=1000)
print "nDescriptive statistics for Xn"
print " Minimum:   %8.4f" % X.min()
print " Maximum:   %8.4f" % X.max()
print " Mean   :   %8.4f" % X.mean()
print " Std Dev:   %8.4fn" % X.std()
X.sort()
print "Extreme observations"
print " Smallest    Largest"
for i in range(5):  
    print "%8.4f    %8.4f" % (X[i], X[-1 * (5-i)])
print ""

First, the libraries are imported; next the seed value for the random number generator is set; then a vector of random variates from a N(10,1) distribution is created; the next lines print various descriptive statistics for X. Finally, X is sorted, and the smallest and largest observations are printed. Run this using

$ python desc_stats.py 

Descriptive statistics for X

 Minimum:     3.8404
 Maximum:    17.7447
 Mean   :    10.0804
 Std Dev:     1.9532

Extreme observations
 Smallest    Largest
  3.8404     15.3799
  4.1145     15.6617
  4.1344     15.7702
  4.2882     15.7948
  4.8485     17.7447

Additional SciPy documentation

There are many good resources on the web on NumPy and how to use it, here is a list of just a few.