Movatterモバイル変換


[0]ホーム

URL:


SALE!Use codeBF40 for 40% off everything!
Hurry, sale ends soon!Click to see the full catalog.

Navigation

MachineLearningMastery.com

Making developers awesome at machine learning

Making developers awesome at machine learning

Linear Algebra Cheat Sheet for Machine Learning

All of the Linear Algebra Operations that You Need to Use
in NumPy for 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

Linear Algebra Cheat Sheet for Machine Learning
Photo byChristoph 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

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

Empty

1
2
fromnumpyimportempty
A=empty([3,3])

Zeros

1
2
fromnumpyimportzeros
A=zeros([3,5])

Ones

1
2
fromnumpyimportones
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
2
c=a.dot(b)
c=a@b

Vector-Scalar Multiplication

1
c=a *2.2

Vector Norm

1
2
fromnumpy.linalgimportnorm
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
2
C=A.dot(B)
C=A@B

Matrix-Vector Multiplication (Dot Product)

1
2
C=A.dot(b)
C=A@b

Matrix-Scalar Multiplication

1
2
C=A.dot(2.2)
C=A *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
fromnumpyimporttril
lower=tril(M)
# upper
fromnumpyimporttriu
upper=triu(M)

Diagonal Matrix

1
2
fromnumpyimportdiag
d=diag(M)

Identity Matrix

1
2
fromnumpyimporteye
I=eye(3)

5. Matrix Operations

Matrix operations are often used as elements in broader calculations.

Matrix Transpose

1
B=A.T

Matrix Inversion

1
2
fromnumpy.linalgimportinv
B=inv(A)

Matrix Trace

1
2
fromnumpyimporttrace
B=trace(A)

Matrix Determinant

1
2
fromnumpy.linalgimportdet
B=det(A)

Matrix Rank

1
2
fromnumpy.linalgimportmatrix_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
fromscipy.linalgimportlu
P,L,U=lu(A)

QR Decomposition

1
2
fromnumpy.linalgimportqr
Q,R=qr(A,'complete')

Eigendecomposition

1
2
fromnumpy.linalgimporteig
values,vectors=eig(A)

Singular-Value Decomposition

1
2
fromscipy.linalgimportsvd
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
fromnumpyimportmean
result=mean(v)

Variance

1
2
fromnumpyimportvar
result=var(v,ddof=1)

Standard Deviation

1
2
fromnumpyimportstd
result=std(v,ddof=1)

Covariance Matrix

1
2
fromnumpyimportcov
sigma=cov(v1,v2)

Linear Least Squares

1
2
fromnumpy.linalgimportlstsq
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

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.

Get a Handle on Linear Algebra for Machine Learning!

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 providesself-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more...

Finally Understand the Mathematics of Data

Skip the Academics. Just Results.

See What's Inside

13 Responses toLinear Algebra Cheat Sheet for Machine Learning

  1. AzhaarFebruary 23, 2018 at 5:22 am#

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

  2. MatthewFebruary 23, 2018 at 1:09 pm#

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

    • Jason BrownleeFebruary 24, 2018 at 9:08 am#

      Thanks Matthew!

    • GerardoOctober 28, 2021 at 12:17 pm#

      Thank you for sharing! Good info inside

  3. Balaji VenkateswaranFebruary 23, 2018 at 4:08 pm#

    Thanks for this quick cheat sheet. Very useful one!

  4. AkhtarFebruary 23, 2018 at 5:13 pm#

    Thanks Jason, this is super helpful.

  5. DamianMay 4, 2018 at 10:32 pm#

    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.

  6. Donald LacombeOctober 26, 2021 at 1:41 am#

    One can save a few keystrokes by using the @ symbol for the dot product:

    A@b

    • Adrian Tam
      Adrian TamOctober 27, 2021 at 2:27 am#

      Yes, you’re right. But only available in Python 3.5 and afterwards.

Leave a ReplyClick here to cancel reply.

Never miss a tutorial:


LinkedIn   Twitter   Facebook   Email Newsletter   RSS Feed

Loving the Tutorials?

TheLinear Algebra for Machine Learning EBook is where you'll find theReally Good stuff.

>> See What's Inside


[8]ページ先頭

©2009-2025 Movatter.jp