Generic filters
Exact matches only

# Linear Algebra Cheat Sheet for Machine Learning

Last Updated on August 9, 2019

### All of the Linear Algebra Operations that You Need to Usein 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.

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.

Linear Algebra Cheat Sheet for Machine Learning
Photo by Christoph Landers, some rights reserved.

## Overview

This tutorial is divided into 7 parts; they are:

1. Arrays
2. Vectors
3. Matrices
4. Types of Matrices
5. Matrix Operations
6. Matrix Factorization
7. 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

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

from numpy import array

A = array([[1,2,3],[1,2,3],[1,2,3]])

### Empty

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

from numpy import empty

A = empty([3,3])

### Zeros

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

from numpy import zeros

A = zeros([3,5])

### Ones

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

from numpy import ones

A = ones([5, 5])

## 2. Vectors

A vector is a list or column of scalars.

### Vector Norm

from numpy.linalg import norm
l2 = norm(v)

from numpy.linalg import norm

l2 = norm(v)

## 3. Matrices

A matrix is a two-dimensional array of scalars.

## 4. Types of Matrices

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

### Triangle Matrix

# lower
from numpy import tril
lower = tril(M)
# upper
from numpy import triu
upper = triu(M)

# lower

from numpy import tril

lower = tril(M)

# upper

from numpy import triu

upper = triu(M)

### Diagonal Matrix

from numpy import diag
d = diag(M)

from numpy import diag

d = diag(M)

### Identity Matrix

from numpy import identity
I = identity(3)

from numpy import identity

I = identity(3)

## 5. Matrix Operations

Matrix operations are often used as elements in broader calculations.

### Matrix Inversion

from numpy.linalg import inv
B = inv(A)

from numpy.linalg import inv

B = inv(A)

### Matrix Trace

from numpy import trace
B = trace(A)

from numpy import trace

B = trace(A)

### Matrix Determinant

from numpy.linalg import det
B = det(A)

from numpy.linalg import det

B = det(A)

### Matrix Rank

from numpy.linalg import matrix_rank
r = matrix_rank(A)

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

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

from scipy.linalg import lu

P, L, U = lu(A)

### QR Decomposition

from numpy.linalg import qr
Q, R = qr(A, ‘complete’)

from numpy.linalg import qr

Q, R = qr(A, ‘complete’)

### Eigendecomposition

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

from numpy.linalg import eig

values, vectors = eig(A)

### Singular-Value Decomposition

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

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

from numpy import mean
result = mean(v)

from numpy import mean

result = mean(v)

### Variance

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

from numpy import var

result = var(v, ddof=1)

### Standard Deviation

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

from numpy import std

result = std(v, ddof=1)

### Covariance Matrix

from numpy import cov
sigma = cov(v1, v2)

from numpy import cov

sigma = cov(v1, v2)

### Linear Least Squares

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

from numpy.linalg import lstsq

b = lstsq(X, y)

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

## 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?

## Get a Handle on Linear Algebra for Machine Learning!

#### Develop a working understand of linear algebra

…by writing lines of code in python

Discover how in my new Ebook:
Linear Algebra for Machine Learning

It provides self-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more…