Creating a matrix is the right way to start
Many of the same techniques you use with vectors also work with matrixes. To create a basic matrix, you simply use the array()
function as you would with a vector, but you define additional dimensions. A dimension is a direction in the matrix. For example, a two-dimensional matrix contains rows (one direction) and columns (a second direction). The array call myMatrix = np.array([[1,2,3], [4,5,6], [7,8,9]])
produces a matrix containing three rows and three columns, like this:
[[1 2 3] [4 5 6] [7 8 9]]
Note how you embed three lists within a container list to create the two dimensions. To access a particular array element, you provide a row and column index value, such as myMatrix[0, 0]
to access the first value of 1
. You can find a full listing of vector and matrix array-creation functions at https://numpy.org/doc/stable/reference/routines.array-creation.html
.
The NumPy package supports an actual matrix
class. The matrix
class supports special features that make it easier to perform matrix-specific tasks. You discover these features later in the chapter. For now, all you really need to know is how to create a matrix of the matrix
data type. The easiest method is to make a call similar to the one you use for the array
function, but using the mat
function instead, such as myMatrix = np.mat([[1,2,3], [4,5,6], [7,8,9]])
, which produces the following matrix:
[[1 2 3] [4 5 6] [7 8 9]]
To determine that this actually is a matrix, try print(type(myMatrix))
, which outputs . You can also convert an existing array to a matrix using the asmatrix()
function. Use the asarray()
function to convert a matrix
object back to an array
form.
The only problem with the matrix
class is that it works on only two-dimensional matrixes. If you attempt to convert a three-dimensional matrix to the matrix
class, you see an error message telling you that the shape is too large to be a matrix.
Multiplying two matrixes involves the same concerns as multiplying two vectors (as discussed in the “ Performing vector multiplication” section, earlier in this chapter). The following code produces an element-by-element multiplication of two matrixes:
a = np.array([[1,2,3],[4,5,6]])b = np.array([[1,2,3],[4,5,6]]) print(a*b)
The output looks like this:
[[ 1 4 9] [16 25 36]]
Note that a
and b
are the same shape: two rows and three columns. To perform an element-by-element multiplication, the two matrixes must be the same shape. Otherwise, you see an error message telling you that the shapes are wrong. As with vectors, the multiply()
function also produces an element-by-element result.
Dot products work completely differently with matrixes. In this case, the number of columns in matrix a
must match the number of rows in matrix b
. However, the number of rows in matrix a
can be any number, and the number of columns in matrix b
can be any number as long as you multiply a
by b
. For example, the following code produces a correct dot product:
a = np.array([[1,2,3],[4,5,6]])b = np.array([[1,2,3],[3,4,5],[5,6,7]]) print(a.dot(b))
with an output of:
[[22 28 34] [49 64 79]]
Note that the output contains the number of rows found in matrix a
and the number of columns found in matrix b
. So how does this all work? To obtain the value found in the output array at index [0,0] of 22, you sum the values of a[0,0] * b[0,0] (which is 1), a[0,1] * b[1,0] (which is 6), and a[0,2] * b[2,0] (which is 15) to obtain the value of 22. The other entries work precisely the same way.
To perform an element-by-element multiplication using two matrix
objects, you must use the numpy
multiply()
function.
Defining advanced matrix operations
This book takes you through all sorts of interesting matrix operations, but you use some of them commonly, which is why they appear in this chapter. When working with arrays, you sometimes get data in a shape that doesn't work with the algorithm. Fortunately, numpy
comes with a special reshape()
function that lets you put the data into any shape needed. In fact, you can use it to reshape a vector into a matrix, as shown in the following code:
changeIt = np.array([1,2,3,4,5,6,7,8])print(changeIt) changeIt = changeIt.reshape(2,4)print(changeIt) changeIt = changeIt.reshape(2,2,2)print(changeIt)
When you run this code, you see these outputs (spaces added for clarity):
[1 2 3 4 5 6 7 8] [[1 2 3 4] [5 6 7 8]] [[[1 2] [3 4]] [[5 6] [7 8]]]
The starting shape of changeIt
is a vector, but using the reshape()
function turns it into a matrix. In addition, you can shape the matrix into any number of dimensions that work with the data. However, you must provide a shape that fits with the required number of elements. For example, calling changeIt.reshape(2,3,2)
will fail because there aren't enough elements to provide a matrix of that size.
You may encounter two important matrix operations in some algorithm formulations. They are the transposition and inverse of a matrix. Transposition occurs when a matrix of shape n x m is transformed into a matrix m x n by exchanging the rows with the columns. Most texts indicate this operation by using the superscript T, as in A T. You see this operation used most often for multiplication in order to obtain the right dimensions. When working with numpy
, you use the transpose
function to perform the required work. For example, when starting with a matrix that has two rows and four columns, you can transpose it to contain four rows with two columns each, as shown in this example:
changeIt = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])print(changeIt) changeIt = np.transpose(changeIt)print(changeIt)
The outputs look like this:
[[1 2 3 4] [5 6 7 8]] [[1 5] [2 6] [3 7] [4 8]]
You apply matrix inversion to matrixes of shape m x m, which are square matrixes that have the same number of rows and columns. This operation is quite important because it allows the immediate resolution of equations involving matrix multiplication, such as y = bX
, where you know vector y and matrix X, and you have to discover the values in the vector b. Because most scalar numbers (exceptions include zero) have a number whose multiplication results in a value of 1, the idea is to find a matrix inverse whose multiplication will result in a special matrix called the identity matrix. To see an identity matrix in numpy
, use the identity
function, like this:
Читать дальше