Making developers awesome at machine learning
Making developers awesome at machine learning
The Python numerical computation library called NumPy provides manylinear algebra functions that may be useful as a machine learning practitioner.
In this tutorial, you will discover the key functions for working with vectors and matrices that you may find useful as a machine learning practitioner.
This is a cheat sheet and all examples are short and assume you are familiar with the operation being performed.
You may want to bookmark this page for future reference.
Kick-start your project with my new bookLinear Algebra for Machine Learning, includingstep-by-step tutorials and thePython source code files for all examples.
Let’s get started.

Linear Algebra Cheat Sheet for Machine Learning
Photo byChristoph Landers, some rights reserved.
This tutorial is divided into 7 parts; they are:
Take my free 7-day email crash course now (with sample code).
Click to sign-up and also get a free PDF Ebook version of the course.
There are many ways to create NumPy arrays.
1 2 | fromnumpyimportarray A=array([[1,2,3],[1,2,3],[1,2,3]]) |
1 2 | fromnumpyimportempty A=empty([3,3]) |
1 2 | fromnumpyimportzeros A=zeros([3,5]) |
1 2 | fromnumpyimportones A=ones([5,5]) |
A vector is a list or column of scalars.
1 | c=a+b |
1 | c=a-b |
1 | c=a *b |
1 | c=a/b |
1 2 | c=a.dot(b) c=a@b |
1 | c=a *2.2 |
1 2 | fromnumpy.linalgimportnorm l2=norm(v) |
A matrix is a two-dimensional array of scalars.
1 | C=A+B |
1 | C=A-B |
1 | C=A *B |
1 | C=A/B |
1 2 | C=A.dot(B) C=A@B |
1 2 | C=A.dot(b) C=A@b |
1 2 | C=A.dot(2.2) C=A *2.2 |
Different types of matrices are often used as elements in broader calculations.
1 2 3 4 5 6 | # lower fromnumpyimporttril lower=tril(M) # upper fromnumpyimporttriu upper=triu(M) |
1 2 | fromnumpyimportdiag d=diag(M) |
1 2 | fromnumpyimporteye I=eye(3) |
Matrix operations are often used as elements in broader calculations.
1 | B=A.T |
1 2 | fromnumpy.linalgimportinv B=inv(A) |
1 2 | fromnumpyimporttrace B=trace(A) |
1 2 | fromnumpy.linalgimportdet B=det(A) |
1 2 | fromnumpy.linalgimportmatrix_rank r=matrix_rank(A) |
Matrix factorization, or matrix decomposition, breaks a matrix down into its constituent parts to make other operations simpler and more numerically stable.
1 2 | fromscipy.linalgimportlu P,L,U=lu(A) |
1 2 | fromnumpy.linalgimportqr Q,R=qr(A,'complete') |
1 2 | fromnumpy.linalgimporteig values,vectors=eig(A) |
1 2 | fromscipy.linalgimportsvd U,s,V=svd(A) |
Statistics summarize the contents of vectors or matrices and are often used as components in broader operations.
1 2 | fromnumpyimportmean result=mean(v) |
1 2 | fromnumpyimportvar result=var(v,ddof=1) |
1 2 | fromnumpyimportstd result=std(v,ddof=1) |
1 2 | fromnumpyimportcov sigma=cov(v1,v2) |
1 2 | fromnumpy.linalgimportlstsq b=lstsq(X,y) |
This section provides more resources on the topic if you are looking to go deeper.
In this tutorial, you discovered the key functions for linear algebra that you may find useful as a machine learning practitioner.
Are there other key linear algebra functions that you use or know of?
Let me know in the comments below.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

...by writing lines of code in python
Discover how in my new Ebook:
Linear Algebra for Machine Learning
It providesself-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more...
Skip the Academics. Just Results.
Very helpful! thanks for compiling and sharing this cheat sheet.
Thanks for this quick cheat sheet. Very useful one!
Hello.
Thanks for this summary.
A small remark:
Matrix-Scalar Multiplication doesn’t work exclusively like that (at least in python 3.6)
you are able to use either A*scalar(k) or np.dot (k).
And it makes more sense to respect original math notations and not to abuse the function by using a scalar with it.
Welcome!
I'mJason Brownlee PhD
and Ihelp developers get results withmachine learning.
Read more
TheLinear Algebra for Machine Learning EBook is where you'll find theReally Good stuff.