55
Python Introduction & Linear Algebra Review Kuan Fang CS 231A 01/15/2021 1

Python Introduction & Linear Algebra Review 01/15/2021 CS

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python Introduction & Linear Algebra Review 01/15/2021 CS

Python Introduction &Linear Algebra Review

Kuan FangCS 231A

01/15/20211

Page 2: Python Introduction & Linear Algebra Review 01/15/2021 CS

Outline● Python Introduction

● Linear Algebra and NumPy

2

Page 3: Python Introduction & Linear Algebra Review 01/15/2021 CS

Outline● Python Introduction

● Linear Algebra and NumPy

3

Page 4: Python Introduction & Linear Algebra Review 01/15/2021 CS

Python

High-level, interpreted programming language

Python will be used in all the homeworks and recommended for the project.

We’ll cover some basics today

4

Page 5: Python Introduction & Linear Algebra Review 01/15/2021 CS

Why Python?● Python is high-level.

5

Page 6: Python Introduction & Linear Algebra Review 01/15/2021 CS

Why Python?● Python is accessible.

Interpreter/Terminal IDE Jupyter Notebook6

Page 7: Python Introduction & Linear Algebra Review 01/15/2021 CS

Why Python?● Python has many many awesome packages.

7

Page 8: Python Introduction & Linear Algebra Review 01/15/2021 CS

How to Set up Python?1. Find a computer:

a. Your Linux/Mac/Windows/... machines.b. Use Stanford Corn machines:

https://web.stanford.edu/group/farmshare/cgi-bin/wiki/index.php/Main_Pagec. iMac computers in Stanford Libraries.

2. Follow this guide: https://wiki.python.org/moin/BeginnersGuide/Download3. Choose your favourite editor or IDE:

a. Sublimeb. Vimc. Spyderd. PyCharme. Eclipsef. Jupyter Notebook

g. ... 8

Page 9: Python Introduction & Linear Algebra Review 01/15/2021 CS

Variable

9

Page 10: Python Introduction & Linear Algebra Review 01/15/2021 CS

Comment

10

Page 11: Python Introduction & Linear Algebra Review 01/15/2021 CS

List

11

Page 12: Python Introduction & Linear Algebra Review 01/15/2021 CS

List

12

Page 13: Python Introduction & Linear Algebra Review 01/15/2021 CS

List

13

Page 14: Python Introduction & Linear Algebra Review 01/15/2021 CS

List Indexing

14

Page 15: Python Introduction & Linear Algebra Review 01/15/2021 CS

List Indexing

15

Page 16: Python Introduction & Linear Algebra Review 01/15/2021 CS

Dictionary (Similar to Map in Java/C++)

16

Page 17: Python Introduction & Linear Algebra Review 01/15/2021 CS

Dictionary

17

Page 18: Python Introduction & Linear Algebra Review 01/15/2021 CS

Dictionary Indexing

18

Page 19: Python Introduction & Linear Algebra Review 01/15/2021 CS

Control Flow

19

Page 20: Python Introduction & Linear Algebra Review 01/15/2021 CS

Control Flow

20

Page 21: Python Introduction & Linear Algebra Review 01/15/2021 CS

List Comprehension

21

Page 22: Python Introduction & Linear Algebra Review 01/15/2021 CS

Function

22

Page 23: Python Introduction & Linear Algebra Review 01/15/2021 CS

Outline● Python Introduction

● Linear Algebra and NumPy

23

Page 24: Python Introduction & Linear Algebra Review 01/15/2021 CS

Why use Linear Algebra in Computer Vision?As you’ve seen in lecture, it’s useful to represent many quantities, e.g. 3D points on a scene, 2D points on an image.

Transformations of 3D points with 2D points can be represented as matrices.

Images are literally matrices filled with numbers (as you will see in HW0).

24

Page 25: Python Introduction & Linear Algebra Review 01/15/2021 CS

Vector Review

25

Page 26: Python Introduction & Linear Algebra Review 01/15/2021 CS

Vector Review

26

Page 27: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Review

27

Page 28: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrices and Vectors in Python (NumPy)

import numpy as npAn optimized, well-maintained scientific computing package for Python.

As time goes on, you’ll learn to appreciate NumPy more and more.

Years later I’m still learning new things about it!

28

Page 29: Python Introduction & Linear Algebra Review 01/15/2021 CS

np.ndarray: Matrices and Vectors in Python

29

Page 30: Python Introduction & Linear Algebra Review 01/15/2021 CS

np.ndarray: Matrices and Vectors in Python

30

Page 31: Python Introduction & Linear Algebra Review 01/15/2021 CS

np.ndarray: Matrices and Vectors in Python

31

Page 32: Python Introduction & Linear Algebra Review 01/15/2021 CS

Other Ways to Create Matrices and VectorsNumPy provides many convenience functions for creating matrices/vectors.

32

Page 33: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Indexing

33

Page 34: Python Introduction & Linear Algebra Review 01/15/2021 CS

Dot Product

34

Page 35: Python Introduction & Linear Algebra Review 01/15/2021 CS

Cross Product

35

Page 36: Python Introduction & Linear Algebra Review 01/15/2021 CS

Cross Product

36

Page 37: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Multiplication

37

Page 38: Python Introduction & Linear Algebra Review 01/15/2021 CS

Basic Operations - Dot Multiplication

Matrix multiplication in NumPy can be defined as the dot product between a matrix and a matrix/vector.

38

Page 39: Python Introduction & Linear Algebra Review 01/15/2021 CS

Basic Operations - Element-wise Multiplication

39

Page 40: Python Introduction & Linear Algebra Review 01/15/2021 CS

Orthonormal Basis= Orthogonal and Normalized Basis

40

Page 41: Python Introduction & Linear Algebra Review 01/15/2021 CS

Transpose

41

Page 42: Python Introduction & Linear Algebra Review 01/15/2021 CS

Basic Operations - Transpose

42

Page 43: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Determinant

43

Page 44: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Inverse

44

Page 45: Python Introduction & Linear Algebra Review 01/15/2021 CS

Basic Operations - Determinant and Inverse

45

Page 46: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Eigenvalues and Eigenvectors

46

Page 47: Python Introduction & Linear Algebra Review 01/15/2021 CS

Matrix Eigenvalues and Eigenvectors

47

Page 48: Python Introduction & Linear Algebra Review 01/15/2021 CS

Basic Operations - Eigenvalues, Eigenvectors

NOTE: Please read the NumPy docs on this function before using it, lots more information about multiplicity of eigenvalues and etc there.

48

Page 49: Python Introduction & Linear Algebra Review 01/15/2021 CS

Singular Value Decomposition

49

Page 50: Python Introduction & Linear Algebra Review 01/15/2021 CS

Singular Value Decomposition

Image source: Wikipedia50

Page 51: Python Introduction & Linear Algebra Review 01/15/2021 CS

Singular Value Decomposition

Image source: Wikipedia51

Page 52: Python Introduction & Linear Algebra Review 01/15/2021 CS

Singular Value Decomposition

52

Page 53: Python Introduction & Linear Algebra Review 01/15/2021 CS

Singular Value Decomposition

Recall SVD is the factorization of a matrix into the product of 3 matrices, and is formulated like so:

M = UΣVT

Caution: The notation of SVD in NumPy is slightly different. Here V is actually VT in the common notation.53

Page 54: Python Introduction & Linear Algebra Review 01/15/2021 CS

More InformationPython Documentation: https://docs.python.org/2/index.html

NumPy Documentation: https://docs.scipy.org/doc/numpy-1.13.0/user/index.html

The Matrix Cookbook: https://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf

CS231N Python Tutorial: http://cs231n.github.io/python-numpy-tutorial/

Office hours!The rest of the internet!

54

Page 55: Python Introduction & Linear Algebra Review 01/15/2021 CS

Thanks!Questions

55