A Complete Beginners Guide to Matrix Multiplication for Data Science with Python Numpy | by GreekDataGuy | Towards Data Science

Linear algebra is the basis of machine learning from logistic regressions to deep neural networks.
If you’ve been doing data science for a while but don’t understand the math behind it, matrix multiplication is the best place to start. It’s approachable, practical, and familiarizes you with the mathematical objects of machine learning: scalars, vectors and matrices.
Oh yeah, and Numpy makes it a walk in the park.
By the end of this article, you’ll have a practical understanding of matrix multiplication.
Here we’ll contrast matrices with scalars.
A matrix is a 2D array, where each element in the array has 2 indices. For example, [[1, 2], [3, 4]] is a matrix, and the index of 1 is (0,0).
We can prove this using Python and Numpy.
import numpy as npA = [[1, 2],      [3, 4]]np.array(A)[0,0]=> 1
When talking about the shape of matrices, we say “rows x columns”. We would say the 1st matrix below has a shape of 2x2, and the 2nd has a shape of 3x2.
[[1, 2],  [3, 4]][[10, 20], [11, 21], [12, 22]]
In contrast, a scalar is just a number, like the number 5.
The simple form of matrix multiplication is called scalar multiplication, multiplying a scalar by a matrix.
Scalar multiplication is generally easy. Each value in the input matrix is multiplied by the scalar, and the output has the same shape as the input matrix.
Let’s do the above example but with Python’s Numpy.
a = 7B = [[1,2],     [3,4]]np.dot(a,B)=> array([[ 7, 14],=>        [21, 28]])
One more scalar multiplication example. This time a scalar multiplying a 3x1 matrix.
And in Numpy.
a = 4 B = [[1],[2],[3]]np.dot(a,B)=> array([[ 4],=>        [ 8],=>        [12]])
Order doesn’t matter with scalar multiplication. “scalar x matrix” and “matrix x scalar” give the same result. But not so when multiplying 2 matrices.
Now to the fun part. Multiply a 2D matrix by a 2D matrix.
There are a few things to keep in mind.
Order matters now. AB != BAMatrices can be multiplied if the number of columns in the 1st equals the number of rows in the 2ndMultiplication is the dot product of rows and columns. Rows of the 1st matrix with columns of the 2nd
Order matters now. AB != BA
Matrices can be multiplied if the number of columns in the 1st equals the number of rows in the 2nd
Multiplication is the dot product of rows and columns. Rows of the 1st matrix with columns of the 2nd
In the above image, 19 in the (0,0) index of the outputted matrix is the dot product of the 1st row of the 1st matrix and the 1st column of the 2nd matrix.
Let’s replicate the result in Python.
A = [[1,2],     [3,4]]B = [[5,6],     [7,8]]np.dot(A,B)=> array([[19, 22],=>        [43, 50]])
And let’s hammer home the point about order. Try np.dot(A,B) and then try np.dot(B,A).
np.dot(A,B)=> array([[19, 22],=>        [43, 50]])np.dot(B,A)=> array([[23, 34],=>        [31, 46]])
Now notice how the output is different! You’ll find the same result calculating it on paper (as unintuitive as it is if you’re not familiar with matrices).
Now we’ll multiply a 2x3 matrix with a 3x2 matrix.
And in Python with Numpy.
A = [[1,2,3],     [4,5,6]]B = [[10,11],     [20,21],     [30,31]]np.dot(A,B)=> array([[140, 146],=>        [320, 335]])
Last example. Multiply a 1x5 with a 5x1 matrix.
Notice how all the calculation we do here are simply to return a single cell in the output matrix.
And once again, in Python.
A = [[1,2,3,4,5]]B = [[10],     [20],     [30],     [40],          [50]]np.dot(A,B)=> array([[550]])
That covers 3 unique cases of matrix multiplication and should give you a general sense of how it works. I’ve shown all the calculations in each step of the diagrams so it’s easy for you to follow along.
Matrix multiplication (and linear algebra) is the basis for deep learning and machine learning.
While you don’t need it to plug and play with Sklearn, having a mental picture of how it works will help you understand it’s models. And with that understanding comes an increased efficiency in tuning and tweaking those models for better performance.