Learn More

We’re about ready to wrap up this brief course on Python for scientific computing.

In this last lecture we give some pointers to the major scientific libraries and suggestions for further reading.

NumPy

Fundamental matrix and array processing capabilities are provided by the excellent NumPy library.

For example, let’s build some arrays

import numpy as np                     # Load the library

a = np.linspace(-np.pi, np.pi, 100)    # Create even grid from -π to π
b = np.cos(a)                          # Apply cosine to each element of a
c = np.sin(a)                          # Apply sin to each element of a

Now let’s take the inner product

b @ c
4.04891256782214e-16

The number you see here might vary slightly due to floating point arithmetic but it’s essentially zero.

As with other standard NumPy operations, this inner product calls into highly optimized machine code.

It is as efficient as carefully hand-coded FORTRAN or C.

SciPy

The SciPy library is built on top of NumPy and provides additional functionality.

For example, let’s calculate \(\int_{-2}^2 \phi(z) dz\) where \(\phi\) is the standard normal density.

from scipy.stats import norm
from scipy.integrate import quad

ϕ = norm()
value, error = quad(ϕ.pdf, -2, 2)  # Integrate using Gaussian quadrature
value
0.9544997361036417

SciPy includes many of the standard routines used in

See them all here.

Graphics

The most popular and comprehensive Python library for creating figures and graphs is Matplotlib, with functionality including

  • plots, histograms, contour images, 3D graphs, bar charts etc.

  • output in many formats (PDF, PNG, EPS, etc.)

  • LaTeX integration

Example 2D plot with embedded LaTeX annotations

../_images/qs.png

Example contour plot

../_images/bn_density1.png

Example 3D plot

../_images/career_vf.png

More examples can be found in the Matplotlib thumbnail gallery.

Other graphics libraries include

Symbolic Algebra

It’s useful to be able to manipulate symbolic expressions, as in Mathematica or Maple.

The SymPy library provides this functionality from within the Python shell.

from sympy import Symbol

x, y = Symbol('x'), Symbol('y')  # Treat 'x' and 'y' as algebraic symbols
x + x + x + y
\[\displaystyle 3 x + y\]

We can manipulate expressions

expression = (x + y)**2
expression.expand()
\[\displaystyle x^{2} + 2 x y + y^{2}\]

solve polynomials

from sympy import solve

solve(x**2 + x + 2)
[-1/2 - sqrt(7)*I/2, -1/2 + sqrt(7)*I/2]

and calculate limits, derivatives and integrals

from sympy import limit, sin, diff

limit(1 / x, x, 0)
\[\displaystyle \infty\]
limit(sin(x) / x, x, 0)
\[\displaystyle 1\]
diff(sin(x), x)
\[\displaystyle \cos{\left(x \right)}\]

The beauty of importing this functionality into Python is that we are working within a fully fledged programming language.

We can easily create tables of derivatives, generate LaTeX output, add that output to figures and so on.

Pandas

One of the most popular libraries for working with data is pandas.

Pandas is fast, efficient, flexible and well designed.

Here’s a simple example, using some dummy data generated with Numpy’s excellent random functionality.

import pandas as pd
np.random.seed(1234)

data = np.random.randn(5, 2)  # 5x2 matrix of N(0, 1) random draws
dates = pd.date_range('28/12/2010', periods=5)

df = pd.DataFrame(data, columns=('price', 'weight'), index=dates)
print(df)
               price    weight
2010-12-28  0.471435 -1.190976
2010-12-29  1.432707 -0.312652
2010-12-30 -0.720589  0.887163
2010-12-31  0.859588 -0.636524
2011-01-01  0.015696 -2.242685
df.mean()
price     0.411768
weight   -0.699135
dtype: float64

Further Reading

These lectures were originally taken from a longer and more complete lecture series on Python programming hosted by QuantEcon.

The full set of lectures might be useful as the next step of your study.