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

Post on 11-Nov-2021

4 views 0 download

transcript

Python Introduction &Linear Algebra Review

Kuan FangCS 231A

01/15/20211

Outline● Python Introduction

● Linear Algebra and NumPy

2

Outline● Python Introduction

● Linear Algebra and NumPy

3

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

Why Python?● Python is high-level.

5

Why Python?● Python is accessible.

Interpreter/Terminal IDE Jupyter Notebook6

Why Python?● Python has many many awesome packages.

7

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

Variable

9

Comment

10

List

11

List

12

List

13

List Indexing

14

List Indexing

15

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

16

Dictionary

17

Dictionary Indexing

18

Control Flow

19

Control Flow

20

List Comprehension

21

Function

22

Outline● Python Introduction

● Linear Algebra and NumPy

23

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

Vector Review

25

Vector Review

26

Matrix Review

27

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

np.ndarray: Matrices and Vectors in Python

29

np.ndarray: Matrices and Vectors in Python

30

np.ndarray: Matrices and Vectors in Python

31

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

32

Matrix Indexing

33

Dot Product

34

Cross Product

35

Cross Product

36

Matrix Multiplication

37

Basic Operations - Dot Multiplication

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

38

Basic Operations - Element-wise Multiplication

39

Orthonormal Basis= Orthogonal and Normalized Basis

40

Transpose

41

Basic Operations - Transpose

42

Matrix Determinant

43

Matrix Inverse

44

Basic Operations - Determinant and Inverse

45

Matrix Eigenvalues and Eigenvectors

46

Matrix Eigenvalues and Eigenvectors

47

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

Singular Value Decomposition

49

Singular Value Decomposition

Image source: Wikipedia50

Singular Value Decomposition

Image source: Wikipedia51

Singular Value Decomposition

52

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

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

Thanks!Questions

55