Last Updated on

### All of the Linear Algebra Operations that You Need to Use

in NumPy for Machine Learning.

The Python numerical computation library called NumPy provides many linear 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.

Discover vectors, matrices, tensors, matrix types, matrix factorization, PCA, SVD and much more in my new book, with 19 step-by-step tutorials and full source code.

Let’s get started.

## Overview

This tutorial is divided into 7 parts; they are:

- Arrays
- Vectors
- Matrices
- Types of Matrices
- Matrix Operations
- Matrix Factorization
- Statistics

### Need help with Linear Algebra for Machine Learning?

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.

## 1. Arrays

There are many ways to create NumPy arrays.

### Array

1 2 |
from numpy import array A = array([[1,2,3],[1,2,3],[1,2,3]]) |

### Empty

1 2 |
from numpy import empty A = empty([3,3]) |

### Zeros

1 2 |
from numpy import zeros A = zeros([3,5]) |

### Ones

1 2 |
from numpy import ones A = ones([5, 5]) |

## 2. Vectors

A vector is a list or column of scalars.

### Vector Addition

1 |
c = a + b |

### Vector Subtraction

1 |
c = a - b |

### Vector Multiplication

1 |
c = a * b |

### Vector Division

1 |
c = a / b |

### Vector Dot Product

1 |
c = a.dot(b) |

### Vector-Scalar Multiplication

1 |
c = a * 2.2 |

### Vector Norm

1 2 |
from numpy.linalg import norm l2 = norm(v) |

## 3. Matrices

A matrix is a two-dimensional array of scalars.

### Matrix Addition

1 |
C = A + B |

### Matrix Subtraction

1 |
C = A - B |

### Matrix Multiplication (Hadamard Product)

1 |
C = A * B |

### Matrix Division

1 |
C = A / B |

### Matrix-Matrix Multiplication (Dot Product)

1 |
C = A.dot(B) |

### Matrix-Vector Multiplication (Dot Product)

1 |
C = A.dot(b) |

### Matrix-Scalar Multiplication

1 |
C = A.dot(2.2) |

## 4. Types of Matrices

Different types of matrices are often used as elements in broader calculations.

### Triangle Matrix

1 2 3 4 5 6 |
# lower from numpy import tril lower = tril(M) # upper from numpy import triu upper = triu(M) |

### Diagonal Matrix

1 2 |
from numpy import diag d = diag(M) |

### Identity Matrix

1 2 |
from numpy import identity I = identity(3) |

## 5. Matrix Operations

Matrix operations are often used as elements in broader calculations.

### Matrix Transpose

1 |
B = A.T |

### Matrix Inversion

1 2 |
from numpy.linalg import inv B = inv(A) |

### Matrix Trace

1 2 |
from numpy import trace B = trace(A) |

### Matrix Determinant

1 2 |
from numpy.linalg import det B = det(A) |

### Matrix Rank

1 2 |
from numpy.linalg import matrix_rank r = matrix_rank(A) |

## 6. Matrix Factorization

Matrix factorization, or matrix decomposition, breaks a matrix down into its constituent parts to make other operations simpler and more numerically stable.

### LU Decomposition

1 2 |
from scipy.linalg import lu P, L, U = lu(A) |

### QR Decomposition

1 2 |
from numpy.linalg import qr Q, R = qr(A, 'complete') |

### Eigendecomposition

1 2 |
from numpy.linalg import eig values, vectors = eig(A) |

### Singular-Value Decomposition

1 2 |
from scipy.linalg import svd U, s, V = svd(A) |

## 7. Statistics

Statistics summarize the contents of vectors or matrices and are often used as components in broader operations.

### Mean

1 2 |
from numpy import mean result = mean(v) |

### Variance

1 2 |
from numpy import var result = var(v, ddof=1) |

### Standard Deviation

1 2 |
from numpy import std result = std(v, ddof=1) |

### Covariance Matrix

1 2 |
from numpy import cov sigma = cov(v1, v2) |

### Linear Least Squares

1 2 |
from numpy.linalg import lstsq b = lstsq(X, y) |

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

### NumPy API

### Other Cheat Sheets

- Python For Data Science Cheat Sheet, DataCamp (PDF)
- Linear algebra explained in four pages (PDF)
- Linear Algebra Cheat Sheet

## Summary

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.

Very helpful! thanks for compiling and sharing this cheat sheet.

I’m glad to hear that it helps.

Thanks Jason, it is very helpful. I like it!

Thanks Matthew!

Thanks for this quick cheat sheet. Very useful one!

Thanks.

Thanks Jason, this is super helpful.

I’m glad to hear that!

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.

Thanks.