Scientific and Engineering Libraries, Part 1

Weeks 12 and 13 are about using external scientific and engineering libraries that are not inside core Python, but are popular enough that we should learn them too.

Numpy

NumPy is a Python library for easy and efficient numerical computation of multi-dimensional arrays (vectors, matrices, etc.) You can install it via either pip or conda with, for example the following command:

pip install numpy

We first have to import numpy:

Numpy works with lists (and lists of lists, etc.) and the core data type is something called numpy.ndarray, which we can create using the np.array() construction function:

This is how we can create a 2D matrix:

A numpy array holds integers or floating point numbers, which can be checked by looking at its dtype:

We can get the dimensions of a numpy array using .shape:

.ndim gives the number of dimensions:

A numpy array can be reshaped into a different shape using .reshape(new_shape). This will create a new array with the requested shape.

To get the number of elements inside an array, use .size:

Similarly to lists in Python, we can access elements inside an array using the same square bracket syntax:

As a special trick, we can access the same index as above using the following syntax:

To get all of the elements of the second columns (counting from zero):

To get all of the elements of the first row (counting from zero):

We can also create arrays that are automatically filled with a specific value:

We can create arrays that with .arange, that mimics the behavior of range, as follows:

One difference is that .arange allows us to give a real-valued step size:

For equally-spaced sampled points, we can also use .linspace:

Operations between arrays

NumPy comes with lots of operations that can be done between arrays. The simplest is addition / subtraction:

We can also do logical comparison between arrays: These will be done element-wise, i.e, each element in the first array will be compared with the element in the second array that has the same index:

There's also lots of ready-to-use functions that are in numpy, including:

Most of these can also be called as a class method (a.mean() vs np.mean(a)).

Some operations can be specified to be done along an axis (i.e, row-wise, column-wise, etc.) To do that, we specify the axis parameter:

.hsplit and .vsplit are for partitioning an array into sub-arrays:

Counterpart to .hsplit and .vsplit are .hstack and .vstack for combining arrays into a single array.

A for loop with numpy arrays will loop over each row:

We can transpose an array by using .T or .transpose(). Transposition makes it that each element at index (row, col) is now at index (col, row):

This lets us iterate over the rows of the original matrix, for example:

Nested for loops allows us the "unravel" an array as follows:

NumPy provides .flat to do the same thing:

Linear algebra with NumPy

NumPy also comes with a lot of tools for linear algebra.

As an example, suppose we have the following equations:

2x + 5y + z = 5
x + 2y + 3z = 3
3x + 3y + 5z = 10

We can find x, y, and z by representing the equations with the following matrices:

np.linalg.solve will provide a solution if it exists:

We can check the solution by computing the dot product with each row of the first matrix:

In general, we can compute the inverse of a matrix (if it exists) with np.linalg.inv:

The inverse of a matrix, when multiplied with original matrix, should produce an identity matrix:

There are many more functions that are available, such as:

SciPy

SciPy is another external Python library for scientific computing. It has multiple libraries for various scientific computation tools, including but not limited to:

See the textbook for further details about SciPy.