Matrices with NumPy

Matrices with NumPy#

Similar to vectors, we can describe matrices with NumPy arrays. For example, the 2×2 identity matrix from earlier can be assigned as,

import numpy as np

M = np.array([[1, 0], [0, 1]])
M
array([[1, 0],
       [0, 1]])

Since this is a special type of matrix, there are also convenience functions built into NumPy to help you create them.

np.identity(2)
array([[1., 0.],
       [0., 1.]])

To construct the matrix, a list of lists is passed to the array function. These lists are the rows of the array. Therefore, to assign the variable that describes the matrix, \(\mathbf{M'}\), from Eqn. (16) the following is used.

M_dash = np.array([[0, -1], [1, 0]])

Similar to standard brackets, the square brackets that define lists can also be used to spread information over lines making it easier to read. This allows M_dash to be rewritten.

M_dash = np.array([[0, -1],
                   [1, 0]])

NumPy offers other convenient functionality for working with matrices. For example, the transpose of a matrix can be found as the .T property of the matrix.

my_matrix = np.array([[0, 4], [1, 2], [2, 7]])
my_matrix
array([[0, 4],
       [1, 2],
       [2, 7]])
my_matrix.T
array([[0, 1, 2],
       [4, 2, 7]])

It was mentioned previously, that as the size of a matrix increases, the inverse becomes harder to calculate. NumPy can help with that with the np.linalg.inv function.

large_matrix = np.random.randint(1, 10, size=(5, 5))
large_matrix
array([[4, 8, 1, 6, 7],
       [5, 5, 3, 4, 4],
       [4, 6, 1, 4, 7],
       [5, 3, 2, 1, 7],
       [6, 4, 9, 9, 5]])

Above a 5×5 matrix os random numbers between 1 and 10 (inclusive/exclusive) has been defined. The inverse can then be found.

np.linalg.inv(large_matrix)
array([[-10.38888889,   2.5       ,  14.61111111,  -5.61111111,
         -0.05555556],
       [ 10.72222222,  -2.        , -15.27777778,   5.77777778,
         -0.11111111],
       [ 11.88888889,  -2.5       , -17.11111111,   6.61111111,
          0.05555556],
       [-10.22222222,   2.        ,  14.77777778,  -5.77777778,
          0.11111111],
       [  0.88888889,  -0.5       ,  -1.11111111,   0.61111111,
          0.05555556]])

Matrix Multiplication#

NumPy enables very efficient matrix multiplication operations. However, because matrix multiplication is distinct from trying to multiply two arrays, i.e., it is not desired to multiply each pair of numbers, it is necessary to make use of a NumPy function, np.matmul, instead of the * operator. Therefore, to perform the matrix multiplication of \(\mathbf{M'}\) with the vector \(\mathbf{r}\) the following operation is used.

r = np.array([2, 3])
np.matmul(M_dash, r)
array([-3,  2])

There is also a more convenient operator that can also be used to perform the same operation.

M_dash @ r
array([-3,  2])

This is sometimes known as the “at”-operator, which will perform a matrix multiplication between two NumPy arrays. Matrix multiplication will be used heavily in the Data Science sections to come.