We will see some very important concepts in this chapter. The dot product is used in every equation explaining data science algorithms so it’s worth the effort to understand it. Then we will see some properties of this operation. Finally, we will to get some intuition on the link between matrices and systems of linear equations.
import numpy as np
Source
# Avoid inaccurate floating values (for inverse matrices in dot product for instance)
# See https://stackoverflow.com/questions/24537791/numpy-matrix-inversion-rounding-errors
np.set_printoptions(suppress=True)
The standard way to multiply matrices is not to multiply each element of one with each elements of the other (this is the element-wise product) but to calculate the sum of the products between rows and columns. The matrix product, also called dot product, is calculated as following:
The dot product between a matrix and a vector
The number of columns of the first matrix must be equal to the number of rows of the second matrix. Thus, if the dimensions, or the shape of the first matrix, is (m×n) the second matrix need to be of shape (n×x). The resulting matrix will have the shape (m×x).
It is a good habit to check the dimensions of the matrix so see what is going on. We can see in this example that the shape of A is (3×2) and the shape of b is (2×1). So the dimensions of C are (3×1).
We will now see some interesting properties of the matrix multiplication. It will become useful as we move forward in the chapters. Using simple examples for each property will provide a way to check them while we get used to the Numpy functions.
This is an important part of why linear algebra can be very useful to solve variety of problems. Here we will see that it can be use to represent system of equations.
A system of equations is a set of multiple equations (at least 1). For instance we could have:
A system of equations is defined by its number of equations and its number of unknowns. In our example above, the system has 2 equations and 2 unknowns (x and y). In addition we call this a system of linear equations because each equations is linear. It is easy to see that in 2 dimensions: we will have one straight line per equation and the dimensions are the unknowns. Here is the plot of the first one:
Representation of a linear equation
In our system of equations, the unknowns are the dimensions and the number of equations is the number of lines (in 2D) or $n$-dimensional planes.
The unknowns (what we want to find to solve the system) are the variables x1 and x2 corresponding to the previous variables x and y. It is exactly the same form as with the last example but with all the variables on the same side. y=2x+1 becomes −2x+y=1 with x corresponding to x1 and y corresponding to x2. We will have n unknowns and m equations.
The variables are named x1,x2,⋯,xn by convention because we will see that it can be summarised in the vector x.
The dot product of A and x gives a set of equations. Here is a simple example:
Matrix form of a system of linear equations
We have a set of two equations with two unknowns. So the number of rows of A gives the number of equations and the number of columns gives the number of unknowns.
In order to end up with this system when we multiply A and x we need A to be a matrix containing the weights of each variable. The weight of x1 is 2 and the weights of x2 is -1:
This system of equations is thus very simple and contains only 1 equation (A has 1 row) and 2 variables (A has 2 columns).
To summarise, A will be the matrix of dimensions m×n containing scalars multiplying these variables (here x1 is multiplied by 2 and x2 by -1). The vector x contains the variables x1 and x2. And the right hand term is the constant b:
We will see at the end of the next chapter that this compact way of writing sets of linear equations can be very usefull. It provides actually a way to solve the equations.