Matrices and Linear Algebra in Python Using NumPy
Linear algebra is a fantastic field, and immensely useful. There are some great tools out there to help us out with it, a particularly widely known such tool being Matlab.
But not all of us have Matlab. And if those of us without it, what are we to do?
How about Python? Particularly, how about the NumPy module for Python?
NumPy, and also SciPy, are modules for Python designed to be very helpful for mathematical computation. They are not included in the plain vanilla version of Python you’d download from the official Python site, so you have to install them yourself. The official NumPy (say it out loud – ‘numpy,’ hehe) page can be found here:
and the download is here:
Instructions for download based on your operating system can be found on the NumPy site. I recommend trying to install it with Python 2.7, not Python 3. I, myself, at least, found this to be the easier path.
This article introduces the matrix class of the NumPy module. It will show how to perform many basic operations on matrices useful for linear algebra. Code could be written in regular Python that could perform these operations, but there is absolutely no point in re-inventing the wheel.
Once you’ve gotten NumPy installed, import it.
>>> import numpy as np
And now you have a powerful tool at your disposal! Let’s see what it can do. To make a matrix:
>>> A = np.matrix( [ [5, 2, 3], [2, 3, 3], [-1, 2, 2] ] )
And now we have the matrix A. The output for matrices look like this:
>>> A matrix([[ 5, 2, 3], [ 2, 3, 3], [-1, 2, 2]])
With the NumPy module, you can create a matrix only of 2 dimensions or less, but of any number of rows and any number of columns. Arrays, which will be discussed later, can be n-dimensional. For now, let’s make a friend matrix for A to play with.
>>> B = np.matrix( [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] )
If you wanted to add these?
>>> A+B matrix([[ 6, 4, 6], [ 6, 8, 9], [ 6, 10, 11]])
>>> A*B matrix([[34, 44, 54], [35, 43, 51], [21, 24, 27]])
If you were to try to multiply matrices in vanilla Python using lists, you would go through a lot more work than this to get the answer. Following is a list of other often-needed matrix operations:
A.I Returns the inverse of A
np.trace(A) Returns the trace of A
A.T Returns the transpose of A
np.linalg.det(A) Returns the determinant of A
np.linalg.matrix_rank(A) Returns the rank of A
At times, such as if you wish to perform row operations, you may want to handle just one row of the matrix. To do so, point to the rows index, like so:
>>> A matrix([[-1, 2, 2]])
The indexing starts at 0, so the first row is A. To handle just a single column, simply apply the transverse function:
>>> A.T.T matrix([, , ])
And to retrieve just a single element, the solution is to index the row, transverse the row, and, again, index the desired row:
>>> A.T matrix([])
One-by-one matrices can be converted to scalar quantities with int(). You should be careful to specify your matrix as float, however, if you are going to be using anything other than natural numbers on it.
>>> A = np.matrix( [ [5, 2, 3], [2, 3, 3], [-1, 2, 2] ], dtype=float)
And if you would need a matrix of more than 2 dimensions, or if from the start you are only dealing with vectors, you might take a look at the array class.
>>> a = np.array([5, 2, 3]) >>> a array([ 5, 2, 3])
The dot and cross products can be applied to arrays and matrices alike, but dot product of vectors returns an integer, while the dot product of two 1-dimensional matrices returns a matrix with a single element. Here is the syntax for the dot and cross products of two vectors:
>>> b = np.array([-1,3,2]) >>> np.dot(a,b) 7 >>> np.cross(a,b) array([ -5, -13, 17])
Using arrays as vectors, you can certain apply the usual operands, + * – / on them for vector arithmetic. As I said earlier, arrays can be of arbitrarily many dimensions. For example, here is a three-dimensional array:
>>> a = np.array( [ [ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ] ) >>> a array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
And a four-dimensional array:
>>> a = np.array( [ [ [ [1, 2], [3, 4] ], [ [5, 6], [7, 8] ] ], [ [ [11, 22], [33, 44] ], [ [55, 66], [77, 88] ] ] ] ) >>> a array([[[[ 1, 2], [ 3, 4]], [[ 5, 6], [ 7, 8]]], [[[11, 22], [33, 44]], [[55, 66], [77, 88]]]])
I show the output for these so that you can see how NumPy chooses to display more than two dimensions in a two dimensional space. These operations made so easy by NumPy offer a lot of possibilities to the one who wishes to walk in the world of linear algebra but does not have access to Matlab.