linear algebra and scientific pythonxiaowei/ai_materials... · linear algebra for machine learning....

44
Linear Algebra and Scientific Python Dr. Xiaowei Huang https://cgi.csc.liv.ac.uk/~xiaowei/

Upload: others

Post on 04-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Linear Algebra and Scientific Python

Dr. Xiaowei Huang

https://cgi.csc.liv.ac.uk/~xiaowei/

Page 2: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Topics

• Linear algebra• Scalars, vectors, matrices, tensors

• Multiplying matrices/vectors

• Introduction to Scientific Python • Numpy

• Scipy

• Matplotlib

Page 3: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Linear Algebra For Machine Learning

Page 4: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Scalar

• Single number

• Represented in lower-case italic x • E.g., let be the slope of the line

• Defining a real-valued scalar

• E.g., let be the number of units • Defining a natural number scalar

Page 5: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Vector

• An array of numbers

• Arranged in order

• Each no. identified by an index

• Vectors are shown in lower-case bold

• If each element is in R then x is in Rn

• We think of vectors as points in space • Each element gives coordinate along an axis

Page 6: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Matrix

• 2-D array of numbers

• Each element identified by two indices

• Denoted by bold typeface A

• Elements indicated as Am,n

• E.g.,

• A[i:] is ith row of A, A[:j] is jth column of A

• If A has shape of height m and width n with real-values then

Page 7: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Tensor

• Sometimes need an array with more than two axes

• An array arranged on a regular grid with variable number of axes is referred to as a tensor

• Denote a tensor with bold typeface: A

• Element (i,j,k) of tensor denoted by Ai,j,k

Page 8: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Transpose of a Matrix

• Mirror image across principal diagonal

• Vectors are matrices with a single column • Often written in-line using transpose

• Since a scalar is a matrix with one element

Page 9: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Linear Transformation

• where and

n equations in n unknowns

Page 10: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Linear Transformation

• where and • More explicitly

• Sometimes we wish to solve for the unknowns x ={x1,..,xn} when A and b provide constraints

Can view A as a linear transformation of vector x to vector b

Page 11: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Identity and Inverse Matrices

• Matrix inversion is a powerful tool to analytically solve Ax=b

• Needs concept of Identity matrix

• Identity matrix does not change value of vector

• when we multiply the vector by identity matrix • Denote identity matrix that preserves n-dimensional vectors as In

• Formally In ∈ Rn×n and ∀x ∈Rn , In x = x

• Example of I3

Page 12: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Matrix Inverse

• Inverse of square matrix A defined as A−1A=In

• We can now solve Ax=b as follows:

Ax=bA−1Ax = A−1b

In x = A−1b

x = A−1b

• This depends on being able to find A-1

• If A-1 exists there are several methods for finding it

Page 13: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Solving Simultaneous equations

• Ax = b

• Two closed-form solutions • Matrix inversion x=A-1b

• Gaussian elimination

Page 14: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Norms

• Used for measuring the size of a vector

• Norms map vectors to non-negative values

• Norm of vector x is distance from origin to x • It is any function f that satisfies:

Page 15: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

LP Norm

• Definition

Page 16: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

LP Norm

• Definition

• L2 Norm• Called Euclidean norm, written simply as

• Squared Euclidean norm is same as

Page 17: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

LP Norm

• Definition

• L1 Norm• also called Manhattan distance

Page 18: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

LP Norm

• Definition

• Norm• also called max norm

Page 19: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Norms of two-dimensional Point

0

0.5

1

1.5

2

2.5

3

3.5

4

4.5

5

0 0.5 1 1.5 2 2.5 3 3.5

Y-Values

||x||1 =

||x||2 =

||x||⚯ =

3+4 = 7

X = (3,4)

Page 20: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Size of a Matrix

• Frobenius norm

• It is analogous to L2 norm of a vector

Page 21: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Image distance

- =

- =

L1 distance between X and Y:

L2 distance between X and Y:

distance between X and Y:

Page 22: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Introduction to Scientific Python

Page 23: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Numpy

• Fundamental package for scientific computing with Python

• N-dimensional array object

• Linear algebra, Fourier transform, random number capabilities

• Building block for other packages (e.g. Scipy)

• Open source

Page 24: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

import numpy as np

• Basics:

• Slicing as usual

Page 25: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

More basics

numpy.linspace:evenly spaced numbers over a specified interval.

numpy.arange:evenly spaced values within a given interval.

Page 26: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

More basics

numpy.random.normal:Draw random samples from a normal (Gaussian) distribution

loc: meanscale: standard deviation

Page 27: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Arrays are mutable

Page 28: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Array attributes

numpy.reshape:Gives a new shape to an array without changing its data.

Page 29: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Basic operations

• Arithmetic operators: elementwise application

• Also, we can use += and *=.

Page 30: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Array broadcasting

• When operating on two arrays, numpy compares shapes. Two dimensions are compatible when• They are of equal size

• One of them is 1

Page 31: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Array broadcasting

Page 32: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Array broadcasting with scalars

• This also allows us to add a constant to a matrix or multiply a matrix by a constant

Page 33: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Vector operations

• inner product

• outer product

• dot product (matrix multiplication)

Page 34: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Matrix operations

• First, define some matrices:

Page 35: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Matrix operations

np.dot(a,b)

If f both a and b are 1-D arrays, it is inner product of vectors

If both a and b are 2-D arrays, it is matrix multiplication

Page 36: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Operations along axes

Page 37: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Slicing arrays

• More advanced slicing

Page 38: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Iterating over arrays

• Iterating over multidimensional arrays is done with respect to the first axis: for row in A

• Looping over all elements: for element in A.flat

Page 39: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Reshaping

• Reshape • using reshape. Total size must remain the same.

• Resize • using resize, always works: chopping or appending zeros

• First dimension has ‘priority’, so beware of unexpected results

Page 40: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Matrix operations

• import numpy.linalg

Page 41: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Linear algebra

Page 42: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Fourier transform

Page 43: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Random sampling

Page 44: Linear Algebra and Scientific Pythonxiaowei/ai_materials... · Linear Algebra For Machine Learning. Scalar •Single number •Represented in lower-case italic x •E.g., let be the

Distributions in random