Matrix Multiplication
In this lab you will use `NumPy` functions to perform matrix multiplication and see how it can be used in the Machine Learning applications.
Packages
Load the NumPy
package to access its functions.
Definition of Matrix Multiplication
If is an matrix and is an matrix, the matrix product (denoted without multiplication signs or dots) is defined to be the matrix such that
where are the elements of matrix , are the elements of matrix , and , , . In other words, is the dot product of the -th row of and the -th column of .
Matrix Multiplication using Python
Like with the dot product, there are a few ways to perform matrix multiplication in Python. As discussed in the previous lab, the calculations are more efficient in the vectorized form. Let's discuss the most commonly used functions in the vectorized form. First, define two matrices:
You can multiply matrices and using NumPy
package function np.matmul()
:
Which will output matrix as a np.array
. Python operator @
will also work here giving the same result:
Matrix Convention and Broadcasting
Mathematically, matrix multiplication is defined only if number of the columns of matrix is equal to the number of the rows of matrix (you can check again the definition in the secition 1 and see that otherwise the dot products between rows and columns will not be defined).
Thus, in the example above (2), changing the order of matrices when performing the multiplication will not work as the above rule does not hold anymore. You can check it by running the cells below - both of them will give errors.
So when using matrix multiplication you will need to be very careful about the dimensions - the number of the columns in the first matrix should match the number of the rows in the second matrix. This is very important for your future understanding of Neural Networks and how they work.
However, for multiplying of the vectors, NumPy
has a shortcut. You can define two vectors and of the same size (which one can understand as two matrices). If you check the shape of the vector , you can see that :
Following the matrix convention, multiplication of matrices and is not defined. For matrix multiplication you would expect an error in the following cell, but let's check the output:
You can see that there is no error and that the result is actually a dot product ! So, vector was automatically transposed into the vector and matrix multiplication was calculated. While this is very convenient, you need to keep in mind such functionality in Python and pay attention to not use it in a wrong way. The following cell will return an error:
You might have a question in you mind: does np.dot()
function also work for matrix multiplication? Let's try it:
Yes, it works! What actually happens is what is called broadcasting in Python: NumPy
broadcasts this dot product operation to all rows and all columns, you get the resultant product matrix. Broadcasting also works in other cases, for example:
Mathematically, subtraction of the matrix and a scalar is not defined, but Python broadcasts the scalar, creating a np.array
and performing subtraction element by element. A practical example of matrix multiplication can be seen in a linear regression model. You will implement it in this week's assignment!
Conclusion
Congratulations on finishing this lab!