1 of 131

Dimensionality reduction

Today: Linear

Tomorrow: non-linear (t-SNE - Itay)

1

2 of 131

Dimensionality

Suppose we have a dataset of N observations with d attributes.

  • Can think of data as N points or vectors in a d dimensional space.

Can also say the dimensionality of dataset is d.

2

Height (in)

Weight (lbs)

65.8

113.0

71.5

136.5

69.4

153.0

2 dimensions

3 of 131

Dimensionality

Suppose we have a dataset of N observations with d attributes.

  • Can think of data as N points or vectors in a d dimensional space.

Can also say the dimensionality of dataset is d.

3

Height (in)

Weight (lbs)

Age

65.8

113.0

17

71.5

136.5

21

69.4

153.0

18

3 dimensions

Height (in)

Weight (lbs)

65.8

113.0

71.5

136.5

69.4

153.0

2 dimensions

4 of 131

Dimensionality

Consider the data shown.

Would you call this dataset:

  • 1 dimensional?
  • 2 dimensional?

4

Weight (lbs)

Weight (kg)

113.0

51.3

136.5

61.9

153.0

69.4

? dimensions

5 of 131

Dimensionality

Consider the data shown.

Would you call this dataset:

  • 1 dimensional?
    • Personally I like this answer better.

5

Weight (lbs)

Weight (kg)

113.0

51.3

136.5

61.9

153.0

69.4

1 dimension

6 of 131

Dimensionality

Consider the data shown.

How many dimensions does this data have?

6

? dimensions

Height (in)

Weight (kg)

Weight (lbs)

Age

65.8

51.3

113.0

17

71.5

61.9

136.5

21

69.4

69.4

153.0

18

7 of 131

Dimensionality

Consider the data shown.

How many dimensions does this data have?

  • 3 because 2 weight columns are redundant.

In linear algebra terms, we’d observe that this matrix has rank 3.

More generally: Can think of a dataset’s dimensionality as the rank of the matrix representing the data.

7

3 dimensions

Height (in)

Weight (kg)

Weight (lbs)

Age

65.8

51.3

113.0

17

71.5

61.9

136.5

21

69.4

69.4

153.0

18

8 of 131

Note on Rank and Dimensionality

Note that in the dataset below, I’ve added one outlier point.

  • Just this one outlier is enough to change the rank of the matrix to 2.
  • But intuitively – we can approximate the data very well in 1 dimension

8

9 of 131

Matrix Multiplication Example

Consider the matrix multiplication example below.

  • Each row of the fruits matrix represents one bowl of fruit.
    • First bowl: 2 apples, 2 lemons, 2 melons.

2

2

2

5

8

0

Number of Fruits

Bowl 1

Bowl 2

# Apples

# Lemons

# Melons

10 of 131

Matrix Multiplication Example

Consider the matrix multiplication example below.

  • Each row of the fruits matrix represents one bowl of fruit.
    • First bowl: 2 apples, 2 lemons, 2 melons.
  • Each column of the dollars matrix represents the cost of fruit at a store.
    • First store: 2 dollars for an apple, 1 dollar for a lemon, 4 dollars for a melon.

2

2

2

5

8

0

2

1

1

1

4

1

Number of Fruits

Bowl 1

Bowl 2

Store 1

Store 2

Fruit Costs

# Apples

# Lemons

# Melons

$/Apple

$/Lemon

$/Melon

11 of 131

Matrix Multiplication Example

Consider the matrix multiplication example below.

  • Each row of the fruits matrix represents one bowl of fruit.
    • First bowl: 2 apples, 2 lemons, 2 melons.
  • Each column of the dollars matrix represents the cost of fruit at a store.
    • First store: 2 dollars for an apple, 1 dollar for a lemon, 4 dollars for a melon.
  • Output is the cost of each bowl at each store.

2

2

2

5

8

0

2

1

1

1

4

1

14

6

18

13

Number of Fruits

Bowl 1

Bowl 2

Store 1

Store 2

Fruit Costs

# Apples

# Lemons

# Melons

$/Apple

$/Lemon

$/Melon

$/Bowl 1

$/Bowl 2

12 of 131

Matrix Multiplication Example

Consider the matrix multiplication example below.

  • Each row of the fruits matrix represents one bowl of fruit.
    • First bowl: 2 apples, 2 lemons, 2 melons.
  • Each column of the dollars matrix represents the cost of fruit at a store.
    • First store: 2 dollars for an apple, 1 dollar for a lemon, 4 dollars for a melon.
  • Output is the cost of each bowl at each store.

2

2

2

5

8

0

2

1

1

1

4

1

14

6

18

13

Number of Fruits

Bowl 1

Bowl 2

Bowl 3

Store 1

Store 2

Fruit Costs

# Apples

# Lemons

# Melons

$/Apple

$/Lemon

$/Melon

$/Bowl 1

$/Bowl 2

$/Bowl 3

13 of 131

View 1: Matrices as Linear Operations

One view of matrix multiplication: Performs multiple linear operations on data.

  • Left matrix are the data.
  • Right matrix are the operations.

We used this view when building linear models.

13

Data

Operation

D a t a

O

peration

D a t a

D a t a

D a t a

Output

Output

2

2

2

5

8

0

2

1

1

1

4

1

Bowl 1

Bowl 2

O

peration

14

6

18

13

(Note: You can also think of left matrix as operations and right as data. Figure not shown!)

14 of 131

View 2: Matrices as Coordinate Transformations

Another view is that matrix multiplication is a coordinate transformation.

  • In the original vector space, we have the vector 2i + j + 4k.
  • The vector 2i + j + 4k ends up at [14, 18].
  • The matrix on the left tells us where i, j, and k “land” after the transformation.
    • i ends up at [2, 5]
    • j ends up at [2, 8]
    • k ends up at [2, 0]

14

=

Where i lands

Where j lands

Where k lands

=

Transformation of i

Transformation of j

Transformation of k

For more on this perspective see 3blue1brown.

This transform: From 3D to 2D!

15 of 131

Matrices Have Many Representations

Takeaway: a matrix can be interpreted many different ways. We discussed two today:

  • Interpretation used in regression modeling:
    • One matrix holds data, the other holds operations.
    • Either left or right matrix can be data.
  • Another interpretation that we’ll use today!
    • One matrix holds coordinates, the other holds a transformation of coordinates.

See 3blue1brown’s Essence of Linear Algebra videos for more intuition.

  • These videos give a fantastic overview of linear algebra concepts – if you like to refresh (/learn) linear algenra

16 of 131

Interpreting a Matrix Multiplication Operation in Context

Suppose we compute the matrix product below. Interpret the resulting matrix.

  • How many rows are there? Columns? What does each row represent? Each column?
  • Give your answer in English.

16

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

=

17 of 131

Interpretation of the Results of the Operation

Suppose we compute the matrix product below. Interpret the resulting matrix.

  • Each row is an observation.
  • Each column is attribute. The 3 attributes of the new matrix are:
    • The two original attributes exactly as before.
    • One of the original attributes now in a different unit (feet).

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

=

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

18 of 131

Interpretation of the Matrix Multiplication Process (View 1)

Suppose we compute the matrix product below. Interpret the resulting matrix.

  • Provides original data along with new column giving height in different units.

Each column of the right matrix represents an operation that computes one column of the output.

18

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

=

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

19 of 131

Interpretation of the Matrix Multiplication Process (View 2)

Suppose we compute the matrix product below. Interpret the resulting matrix.

  • Provides original data along with new column giving height in different units.

The right matrix transforms the data from 2D into 3D.

  • Each column of right matrix represents a basis vector in the 3D space.

19

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

=

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

20 of 131

Reminder: Matrix Multiplication in Python

To multiply two matrices in Python, use the @ operator.

20

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

=

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

21 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 3.3 * 3.3 = 1 * 9.9 = …

21

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

22 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 2.2 * 4.5 = 3.3 * 3.3 = …
  • The size of the two matrices isn’t even unique.
    • 3x2 times 2x3 → 3x3

22

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

Age (days)

Height (in)

182

28

399

30

725

33

1

0

0

0

1

1/12

x

23 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 2.2 * 4.5 = 3.3 * 3.3 = …
  • The size of the two matrices isn’t even unique.
    • 3x2 times 2x3 → 3x3
    • 3x3 times 3x3 → 3x3

23

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

182

28

2.33

399

30

2.5

725

33

2.75

1

0

0

0

1

0

0

0

1

x

24 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 2.2 * 4.5 = 3.3 * 3.3 = …
  • The size of the two matrices isn’t even unique.
    • 3x2 times 2x3 → 3x3
    • 3x3 times 3x3 → 3x3
    • 3x1 times 1x3 → 3x3

Why is this impossible?

24

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

?

?

?

?

?

?

x

25 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 2.2 * 4.5 = 3.3 * 3.3 = …
  • The size of the two matrices isn’t even unique.
    • 3x2 times 2x3 → 3x3
    • 3x3 times 3x3 → 3x3
    • 3x1 times 1x3 → 3x3

Why is this impossible? Rank is greater than 1!

25

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

a

b

c

x

y

z

x

Age (days)

Height (in)

Height (ft)

ax

ay

az

bx

by

bz

cx

cy

cz

ax/bx = a/b = 182/399

ay/by = a/b = 28/30

Contradiction!

26 of 131

Matrix Decomposition

Matrix decomposition (a.k.a. Matrix Factorization) is the opposite of matrix multiplication, i.e. taking a matrix and decomposing it into two separate matrices.

  • Just like with real numbers, there are infinitely many such decompositions.
    • 9.9 = 1.1 * 9 = 2.2 * 4.5 = 3.3 * 3.3 = …
  • The size of the two matrices isn’t even unique.
    • 3x2 times 2x3 → 3x3
    • 3x3 times 3x3 → 3x3
    • 3x1 times 1x3 → 3x3
    • 3x4 times 4x3 → 3x3

26

Age (days)

Height (in)

Height (ft)

182

28

2.33

399

30

2.5

725

33

2.75

182

28

2.33

0

399

30

2.5

0

725

33

2.75

0

1

0

0

0

1

0

0

0

1

99

31

17

x

Can go higher than the size of the original matrix. No reason to do this though!

27 of 131

Rank Exercise

You measure the width, length, area, and perimeter of 100 rectangular backyards.

What is the rank of the given matrix?

27

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

28 of 131

Rank Exercise

The rank is 3. While area is redundant with width and length, it cannot be computed using linear operations.

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

29 of 131

Matrix Decomposition Exercise

You measure the width, length, area, and perimeter of 100 rectangular backyards.

Suppose we want to decompose this matrix into two matrices L and R of minimum size.

  • What are the dimensions of L and R?

L: 100 x ??? R: ??? x 4

L times R to be 100 x 4

29

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

30 of 131

Matrix Decomposition Exercise

You measure the width, length, area, and perimeter of 100 rectangular backyards.

Suppose we want to decompose this matrix into two matrices L and R of minimum size.

  • What are the dimensions of L and R?

L is 100 x 3, and R is 3 x 4.

30

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

31 of 131

Matrix Decomposition Exercise

You measure the width, length, area, and perimeter of 100 rectangular backyards.

Decompose the given matrix into two matrices of minimum size.

  • Left matrix should be 100 x 3.
  • Right matrix should be 3 x 4.

31

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

32 of 131

Most Natural Answer

Since the rank is three, we should decompose into 100 x 3 and 3 x 4 matrices.

There are many possible matrices we could multiply to get back full results.

  • The most natural decomposition is given below.

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

width

length

area

20

20

400

16

12

192

24

12

288

...

25

24

600

1

0

0

2

0

1

0

2

0

0

1

0

x

=

100 x 4

100 x 3

3 x 4

33 of 131

Singular Value Decomposition

Singular value decomposition is an important concept in linear algebra.

  • Will not explain in its entirety – just the gist for intuition

  • Will provide an experimental look at how SVD works.
  • Will give a better context for what SVD means in the context of data science.
  • Will omit a lot of interesting deep theory and rationale.

34 of 131

Redundancy and Decomposition

Earlier, we saw that our rectangle data could be decomposed into two matrices:

  • The data without the perimeter.
  • A matrix that transforms the data from 3D into 4D, computing the perimeter in the process.

Required manual work on our part to identify the magical transformation matrix.

Interesting observation: Total amount of information stored is less!

34

100 x 4 = 400

100 x 3 + 3 x 4 = 312

m x 3

=

x

rectangle

rectangle.iloc[:, 0:3]

1

0

0

2

0

1

0

2

0

0

1

0

Transformation matrix

35 of 131

Singular Value Decomposition

The “Singular Value Decomposition” technique will automatically do a similar transformation for us.

35

m x 4

=

x

4 x n

VT

Same dimensions as the original matrix

36 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” technique will automatically do a similar transformation for us.

What is different about this decomposition from the one you did manually?

36

100 x 4 = 400

100 x 4 + 4 x 4 = 416

m x 4

=

x

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

-56

4.1

-0.77

0

-19

-6.7

3.07

0

37 of 131

Singular Value Decomposition Output (Your Answers)

What is different about this decomposition from the one we did manually?

37

100 x 4 = 400

100 x 4 + 4 x 4 = 416

m x 4

=

x

X

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

-56

4.1

-0.77

0

-19

-6.7

3.07

0

38 of 131

SVD Attendance Question

For this data, it is guaranteed that the last column of UΣ is all zeros. Suppose we simply delete it from the matrix. What else can we throw away?

A. First row of VT. C. Last row of VT.

B. First column of VT. D. Last column of VT.

38

100 x 4 = 400

100 x 4 + 4 x 4 = 416

m x 4

=

x

X

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

-56

4.1

-0.77

0

-19

-6.7

3.07

0

39 of 131

SVD Attendance Question

For this data, it is guaranteed that the last column of UΣ is all zeros. Suppose we simply delete it from the matrix. What else can we throw away?

A. First row of VT. C. Last row of VT.

39

100 x 4 = 400

100 x 3 + 3 x 4 = 312

m x 3

=

x

X

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-56

4.1

-0.77

-19

-6.7

3.07

40 of 131

Matrix Decomposition Summary

Decomposing a MxN matrix X into two matrix factors of size MxQ and QxN does not yield a unique answer.

  • The size of the smallest matrices are when Q = the rank R, i.e. MxR and RxN.

We manually performed a decomposition where:

  • Left matrix was X with redundant columns removed.
    • Example: Length, Width, and Perimeter. We removed Perimeter.
  • Right matrix represented a transformation from lower dimensional space of left matrix to original dimensionality of X.
    • Example: Computes Perimeter as 2 x Length + 2 x Width.

The “Singular Value Decomposition” decomposes X into matrices of size MxN and NxN.

  • Left matrix is no longer the original matrix with some columns removed, totally separate.
  • Right matrix transforms data from the
  • If R < N, the N - R columns will be all zeroes.

40

41 of 131

  • Left: “Data” matrix: Three dimensions (instead of four).
  • Right: “Transform” matrix: Elevated 3D data matrix up to original 4D matrix.

Able to decompose into 100 x 3 and 3 x 4 matrices because rank of 4D matrix was 3.

41

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

??

42 of 131

Last Lecture’s Final Goal: Matrix Decomposition

At the end of last lecture, we did an exercise where I had you decompose the data matrix below into two matrices.

  • Left: 100x3 “Data” matrix: Three dimensions (instead of four).
  • Right: 3x4 “Transform” matrix: Elevated 3D data matrix up to original 4D matrix.

Can factorize (a.k.a. decompose) into infinite number of different matrices. Most natural is:

42

width

length

area

20

20

400

16

12

192

24

12

288

...

25

24

600

1

0

0

2

0

1

0

2

0

0

1

0

x

100 x 3

3 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

43 of 131

Last Lecture’s Final Goal: Matrix Decomposition

Other factorizations are possible, e.g.

  • Leftmost column is W + L
  • Middle column is 2W - L
  • Right column is area

Left matrix can be any linear combination of original columns that allows reconstruction.

43

W+L

2W-L

area

40

20

400

28

20

192

36

36

288

...

49

26

600

1/3

2/3

0

2

1/3

-1/3

0

0

0

0

1

0

x

100 x 3

3 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

44 of 131

Desiderata

One possible goal: Find a procedure which can automatically decompose a rank R matrix into an R dimensional representation times some transformation matrix.

  • Lower dimensional representation avoids redundant features.
  • Imagine a 1000 dimensional dataset: If the rank is only 5, it’s much easier to do EDA after this mystery procedure.

44

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

100 x R

=

x

R x 4

45 of 131

Approximate Factorization

If we go to a two dimensional left matrix, can no longer exactly reconstruct the 4D matrix.

  • Rank of the 4D matrix is 3.

Still, some 2D matrices yield better approximations than others.

  • Find a “reasonable” approximation.

45

??

??

...

x

100 x 2

2 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

46 of 131

Approximate Factorization (My Answer)

There are infinite possible factorization. One intuitive choice (to me) is below.

  • Gets area and perimeter perfectly correct.
  • Width and length are wrong, but more accurate for squarish rectangles.
  • Effectively, my model assumes all rectangles are square.

46

(W+L)/2

Area

20

400

14

192

18

288

...

24.5

600

1

1

0

4

0

0

1

0

x

100 x 2

2 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

47 of 131

Approximate Factorization (My Answer)

There are infinite possible factorization. One intuitive choice (to me) is below.

  • Gets area and perimeter perfectly correct.
  • Width and length are wrong, but more accurate for squarish rectangles.
  • Effectively, my model assumes all rectangles are square.

47

(W+L)/2

Area

20

400

14

192

18

288

...

24.5

600

1

1

0

4

0

0

1

0

x

100 x 2

2 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

actual

48 of 131

Approximate Factorization Exercise That We Won’t Do!

If we go to a one dimensional left matrix, can no longer exactly reconstruct the 4D matrix.

  • Rank of the 4D matrix is 3.

Still, some 1D matrices yield better approximations than others.

  • Find a “reasonable” approximation. Let’s not actually try this by hand in lecture, but feel free to do so if you’re watching webcast.

48

??

...

x

100 x 1

1 x 4

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

49 of 131

Desiderata

Revised goal: Find a procedure which automatically decomposes a matrix into a P dimensional representation times some transformation matrix.

  • Here P is a free choice.
  • Representation should be the “best” one.
    • If P is at least the rank of the matrix, reconstruction should be perfect.

Our tool for this purpose will be “Singular Value Decomposition”.

49

width

length

area

perimeter

20

20

400

80

16

12

192

56

24

12

288

72

...

25

24

600

98

100 x 4

100 x P

=

x

P x 4

= or ≈

= when P ≥ R

≈ when P < R

50 of 131

Singular Value Decomposition

51 of 131

Singular Value Decomposition

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices:

  • Left matrix UΣ will always be same dimensionality as the original data.
  • Right matrix VT will transform UΣ back into X.

51

m x 4

=

x

X

4 x n

VT

100 x 4 = 400

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

52 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

What do you notice about the left matrix?

52

=

x

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

???

???

???

???

-56.3

4.08

-0.77

0

-13.9

-5.62

1.59

0

-7.39

-5.11

1.51

0

-19.6

-6.7

3.07

0

...

100 x 4

4 x 4

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

53 of 131

Singular Value Decomposition Output (Your Answer)

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices. What do you notice about the left matrix?

  • Obviously last column is all zeroes.
  • Other columns are mysterious.

53

=

x

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

-19.6

-6.7

3.07

0

...

100 x 4

4 x 4

X

2

6

12

16

...

???

???

???

???

-56.3

4.08

-0.77

0

-13.9

-5.62

1.59

0

-7.39

-5.11

1.51

0

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

54 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

Suppose we wanted a 3 dimensional decomposition, what would we do to and VT?

54

=

x

VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-0.67

-0.67

0

0.33

-19.6

-6.7

3.07

0

...

100 x 4

4 x 4

X

2

6

12

16

...

???

???

???

???

-56.3

4.08

-0.77

0

-13.9

-5.62

1.59

0

-7.39

-5.11

1.51

0

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

55 of 131

Singular Value Decomposition Output (My Answer)

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

Suppose we wanted a 3 dimensional decomposition, what would we do to and VT?

  • Eliminate last column of UΣ and last row of VT.

55

=

x

First 3 rows of VT

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-19.6

-6.7

3.07

...

100 x 3

3 x 4

First 3 Columns of UΣ

???

???

???

-56.3

4.08

-0.77

-13.9

-5.62

1.59

-7.39

-5.11

1.51

X

2

6

12

16

...

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

56 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

Suppose we wanted a 2 dimensional decomposition, what would we do to and VT?

  • Anybody want to guess? You don’t have enough info to make a confident answer, but make a suggestion.

56

=

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

-56.3

4.08

-0.77

-13.9

-5.62

1.59

-7.39

-5.11

1.51

-19.6

-6.7

3.07

...

First 3 rows of VT

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

First 3 Columns of UΣ

???

???

???

-56.3

4.08

-0.77

-13.9

-5.62

1.59

-7.39

-5.11

1.51

57 of 131

Singular Value Decomposition Output (Your Answer)

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

Suppose we wanted a 2 dimensional decomposition, what would we do to and VT?

  • Drop another column.

57

=

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

???

???

???

-56.3

4.08

-0.77

-13.9

-5.62

1.59

-7.39

-5.11

1.51

-19.6

-6.7

3.07

...

First 3 columns of UΣ

First 3 rows of VT

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

58 of 131

Singular Value Decomposition Output (My Answer)

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

Suppose we wanted a 2 dimensional decomposition, what would we do to and VT?

  • Use only the first 2 columns of UΣ and first 2 rows of V.

58

=

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-19.6

-6.7

...

First 2 columns of UΣ

First 2 rows of VT

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

???

???

-56.3

4.08

-13.9

-5.62

-7.39

-5.11

59 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

If we multiply the first 2 columns of UΣ by the first 2 rows of VT will the reconstruction be perfect?

  • In other words should the = be an ≈?

59

=

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-19.6

-6.7

...

First 2 rows of VT

X

2

6

12

16

...

First 2 columns of UΣ

???

???

-56.3

4.08

-13.9

-5.62

-7.39

-5.11

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

60 of 131

Singular Value Decomposition Output

The “Singular Value Decomposition” automatically decomposes a matrix into two matrices.

If we multiply the first 2 columns of UΣ by the first 2 rows of VT will the reconstruction be perfect?

  • No! No matter what magic singular value decomposition uses, it still follows the laws of linear algebra. Since 2 < rank(X), the reconstruction cannot be perfect.

60

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-19.6

-6.7

...

First 2 rows of VT

X

2

6

12

16

...

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

First 2 columns of UΣ

???

???

-56.3

4.08

-13.9

-5.62

-7.39

-5.11

61 of 131

Multiplying Out the Result

We can multiply out UΣVT and see how close the result is to our original matrix X.

  • Rank 2 approximation: generally pretty good!

61

x

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-19.6

-6.7

...

First 2 rows of VT

W

H

Area

Per.

7.46

6.54

48.0

28.0

3.12

2.87

7.99

11.99

2.07

1.92

2.99

7.99

4.17

3.82

11.98

15.97

...

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

Note: If you try to carry out this matrix multiplication by hand, you’ll get different results because I had to round off the entries in UΣ and VT so they’d fit on the slide.

First 2 columns of UΣ

???

???

-56.3

4.08

-13.9

-5.62

-7.39

-5.11

62 of 131

Going Down to One Dimension

If we go all the way down to one dimension for , the results are better than you might expect.

  • Rank 1 approximation: not totally bad!

62

x

-0.15

-0.13

-0.81

-0.55

???

-56.3

-13.9

-7.39

-19.6

...

First column of UΣ

First row of VT

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

Note: If you try to carry out this matrix multiplication by hand, you’ll get different results because I had to round off the entries in UΣ and VT so they’d fit on the slide.

W

H

Area

Per.

7.46

6.54

48.0

28.0

3.12

2.87

7.99

11.99

2.07

1.92

2.99

7.99

4.17

3.82

11.98

15.97

...

63 of 131

Going Down to One Dimension

If we go all the way down to one dimension for , the results are better than you might expect.

  • Notice any patterns in the 1D approximation?

63

x

-0.15

-0.13

-0.81

-0.55

-56.3

-13.9

-7.39

-19.6

...

First column of UΣ

First row of VT

8.24

7.32

45.6

31.13

2.04

1.81

11.3

7.70

1.08

0.96

5.98

4.08

2.88

2.55

15.91

10.85

...

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

64 of 131

Going Down to One Dimension (Your Answer)

If we go all the way down to one dimension for , the results are better than you might expect.

  • Notice any patterns in the 1D approximation?

64

x

-0.15

-0.13

-0.81

-0.55

-56.3

-13.9

-7.39

-19.6

...

First column of UΣ

First row of VT

8.24

7.32

45.6

31.13

2.04

1.81

11.3

7.70

1.08

0.96

5.98

4.08

2.88

2.55

15.91

10.85

...

X

W

H

Area

Per.

8

6

48

28

2

4

8

12

1

3

3

8

2

6

12

16

...

65 of 131

Matrix Decomposition Summary

Decomposing a MxN matrix X into two matrix factors of size MxQ and QxN does not yield a unique answer.

  • The size of the smallest matrices are when Q = the rank R, i.e. MxR and RxN.

We manually performed a decomposition where:

  • Left matrix was X with redundant columns removed.
    • Example: Length, Width, and Perimeter. We removed Perimeter.
  • Right matrix represented a transformation from lower dimensional space of left matrix to original dimensionality of X.
    • Example: Computes Perimeter as 2 x Length + 2 x Width.

The “Singular Value Decomposition” decomposes X into matrices of size MxN and NxN.

  • Left matrix is no longer the original matrix with some columns removed, totally separate.
  • If R < N, the N - R columns will be all zeroes.
  • How to interpret the matrices returned by singular value decomposition?

65

66 of 131

Manual Decomposition

Before, we took our data and manually created a 3D to 4D transformation matrix.

Decomposed data into:

  • Truncated data.
  • Transformation matrix.

66

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

width

length

area

8

6

48

2

4

8

1

3

3

...

2

6

12

1

0

0

2

0

1

0

2

0

0

1

0

x

=

Transformation matrix

67 of 131

Singular Value Decomposition

Then we used SVD which automatically created a 3D to 4D transformation matrix.

Decomposed data into:

  • Mysteriously rescaled data UΣ.
  • Different transformation matrix VT.

67

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

??

??

??

-56

4.1

-0.77

-1.4

-5.6

1.6

-7.4

-5.1

1.5

...

-19

-6.7

3.07

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

x

=

VT

68 of 131

Singular Value Decomposition

The truth about UΣ and VT:

  • Σ is a diagonal matrix. Contains the so-called “singular values” of X.
  • The columns of U and V are each an orthonormal set.

Let’s define these bolded terms.

68

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

??

??

??

-56

4.1

-0.77

-1.4

-5.6

1.6

-7.4

-5.1

1.5

...

-19

-6.7

3.07

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

x

=

VT

69 of 131

Diagonal Matrices and Σ

A diagonal matrix is a matrix with zeros everywhere except possibly the diagonal.

  • Multiplying by a diagonal matrix is equivalent to scaling the columns.

In Σ, the singular values appear in decreasing order.

  • Singular values are always non-negative.
  • Singular values beyond rank R will be zero.
  • Example of singular values for our rectangle data:
    • Note the 4th singular value is 0.

69

70 of 131

Singular Value Decomposition

The truth about UΣ and VT:

  • Σ is a diagonal matrix. Contains the so-called “singular values” of X.
  • The columns of U and V are each an orthonormal set.

Let’s define these bolded terms.

70

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

??

??

??

-56

4.1

-0.77

-1.4

-5.6

1.6

-7.4

-5.1

1.5

...

-19

-6.7

3.07

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

x

=

VT

71 of 131

Orthogonality, Dot Products, Vector Length

Two orthogonal vectors:

  • Meet at a right angle.
  • Have a dot-product of zero.

A unit vector has length 1.

Side fact: The length of a vector v is the square root of v • v.

71

[-1.5 3]

[2 1]

(-1.5) • (2) + (3) • (1) = 0

72 of 131

Orthonormality

A set of vectors is said to be an orthonormal set if:

  • All of the vectors are unit vectors, i.e. have length 1.
  • All of the vectors are orthogonal.

72

73 of 131

Orthonormality

A set of vectors is said to be an orthonormal set if:

  • All of the vectors are unit vectors, i.e. have length 1.
  • All of the vectors are orthogonal.

Challenge: Given VT, propose a procedure to verify that the columns of V are an orthonormal set (columns of V are the rows of VT).

Give your answer in terms of dot product operations.

73

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

VT

74 of 131

Orthonormality

A set of vectors is said to be an orthonormal set if:

  • All of the vectors are unit vectors, i.e. have length 1.
  • All of the vectors are orthogonal.

Challenge: Given VT, propose a procedure to verify that the columns of V are an orthonormal set (columns of V are the rows of VT).

Give your answer in terms of dot product operations.

  • Magnitude of each row must be 1. Ri dot Ri = 1 for all rows.
  • Ri dot Rj = 0 for all row pairs where i not equal to j.

74

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

VT

75 of 131

Orthonormality

A set of vectors is said to be an orthonormal set if:

  • All of the vectors are unit vectors, i.e. have length 1.
  • All of the vectors are orthogonal.

Challenge: Given VT, propose a procedure to verify that the columns of V are an orthonormal set.

Give your answer in terms of dot product operations.

  • Check dot products of rows with each other.
    • If we dot product a row with itself, what should we get? 1
    • If we dot product a row with another, what should we get? 0

75

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

VT

76 of 131

Principal Components

76

77 of 131

Principal Component

When performing SVD, we’ve left the columns in the “data” matrix UΣ unnamed.

  • Their common name: “Principal Components”*.
  • Example: The second column of UΣ is the “2nd principal component” of our original matrix.

77

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

PC1

PC2

PC3

-56

4.1

-0.77

-1.4

-5.6

1.6

-7.4

-5.1

1.5

...

-19

-6.7

3.07

-0.15

-0.13

-0.81

-0.55

-0.19

-0.18

0.59

-0.76

-0.71

0.71

0.0008

0.0008

x

=

VT

*: There’s an important technical detail we’ll need to fix first (coming in a few slides).

78 of 131

Interpreting Principal Components

Let’s look at a geometric interpretation of:

  • principal components.
  • The rank-k approximation as a whole.

This is easiest if we work with a simple dataset with 2 attributes.

78

79 of 131

Interpreting Principal Components

Let’s look at a geometric interpretation of:

  • principal components.
  • The rank-k approximation as a whole.

This is easiest if we work with a simple dataset with 2 attributes.

We’ll use the dataset to the right showing the age 5 mortality rates and fertility rates for most of the countries around the world.

79

80 of 131

Data, Rank 2, and Rank 1 Approximation

Below, we see the original data, along with the rank 2 and rank 1 approximations.

  • As before, we see that approximation at rank 1 is still fairly decent.
  • The degree to which it is decent depends on how strong the relationship is between mortality and fertility.
  • In Python: np.linalg.svd
  • We can get a lot more insight by looking at things visually.

80

Original Data

Rank 2 Approximation

Rank 1 Approximation

81 of 131

The Rank 1 Approximation Visually

We see that the rank 1 approximation projects the data on to a 1D subspace.

81

82 of 131

The Rank 1 Approximation Visually

There’s a subtle but significant issue with our rank 1 approximation. What is it?

82

83 of 131

The Rank 1 Approximation Visually

Our approximation goes through the origin, but original data has non-zero y-intercept.

83

84 of 131

More Flawed Example

For other datasets, the impact of this y-intercept mismatch can be severe.

84

Data looks different on right because of different x-axis.

85 of 131

Data Centering

Typically, to deal with this issue we recenter the data by subtracting the mean of each column for all values in that column. Resulting projection is much better!

85

86 of 131

Data Uncentering

After performing the approximation, you can add back the means to get back to the original x and y scale.

86

87 of 131

Principal Component

When performing SVD, we’ve left the columns in the “data” matrix UΣ unnamed.

  • Their common name: “Principal Components”.
  • To get the correct principal components, it’s important to center data first!
  • Example: The second column of UΣ is the “2nd principal component” of our centered matrix.

87

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

PC1

PC2

PC3

-26

0.16

0.81

17

-2.18

3.48

2.32

-3.54

2.00

...

11.8

-1.61

-2.51

-0.099

-0.073

-0.93

-0.34

0.67

-0.37

-0.26

0.589

0.314

-0.640

0.257

-0.652

x

=

VT

88 of 131

Intuitive Picture of Low Rank Approximation

Consider the maternal fertility vs. child mortality data we discussed earlier.

Technically, every country has its own separate fertility and mortality.

  • Looking at the plot, we see that we can approximate by saying that each country lies on a continuum between “bottom left” and “top right”.

In essence, the 1st principal component gives each country a single score along this intuitive axis!

88

High PC1 countries

Low PC1 countries

89 of 131

Computing PC1

Given U, Σ, and VT below, write an expression to compute PC1 for Afghanistan.

  • Hint the first column of UΣ is the 1st PC (for all countries).

89

U

VT

Σ

...

90 of 131

Computing PC1

Since the principal components are just the columns of UΣ, we just compute the top left entry of UΣ:

  • -0.0954 * 43.48 + 0.023 * 0 = -4.15

90

U

VT

Σ

...

91 of 131

PC1 Computation

Below, we see a table showing PC1 for every country.

91

High PC1 countries

Low PC1 countries

92 of 131

Plotting Countries vs. PC1

Naturally, we can plot countries vs. PC1.

  • Only one dimension so we end up with a rug plot.

92

High PC1 countries

Low PC1 countries

Somalia

Chad

Singapore

93 of 131

PC1 Back to 2D

Our rank 1 approximation is essentially just going from PC1 back into 2 dimensions.

Give expressions that compute the rank 1 approximation for the fertility and mortality rates for Afghanistan.

93

Somalia

Chad

Singapore

VT

94 of 131

PC1 Back to 2D

To get our rank 1 estimates for Afghanistan:

  • Recall our original data X = UΣVT.
  • So just multiply the first value of the column (principal component) of UΣ by the first row of VT.
  • Mortality: -4.14 * -0.929 = 3.86
  • Fertility: -4.14 *-0.368 = 1.53

94

Somalia

Chad

Singapore

VT

95 of 131

PC1 Back to 2D

To get our rank 1 estimates for Afghanistan:

  • Recall our original data X = UΣVT.
  • So just multiply the first value of the column (principal component) of UΣ by the first row of VT.
  • Mortality: -4.14 * -0.929 = 3.86
  • Fertility: -4.14 *-0.368 = 1.53

95

Somalia

Chad

Singapore

VT

96 of 131

PC1 Back to 2D

The first principal component captures much of the variance, but not all.

  • Differently worded: Countries live near the line, but not exactly on it.

By using 2nd principal component, we can get back original data exactly.

96

Somalia

Chad

Singapore

VT

97 of 131

PC2

PC2 is harder to interpret.

  • It tells you how much you lie “above” or “below” the PC1 line.

The gray area shows countries with similar values of PC1.

  • Countries at the top left of the oval have “large” PC2.
  • Countries at the bottom left have “small” PC2.

97

Low PC2 countries

High PC2 countries

98 of 131

Intuitive Picture of Low Rank Approximation

Approach 2:

  • We know that X = UΣVT.
  • We also know VT = V-1.
  • Thus, XV = UΣ.

98

U

VT

Σ

...

99 of 131

Principal Components and Variance

99

100 of 131

Principal Components

Back to our original rectangle data.

Recall: Before we can identify the principal components of this data, we must first center the data by subtracting the mean of each column from that column.

100

width

length

area

perimeter

8

6

48

28

2

4

8

12

1

3

3

8

...

2

6

12

16

101 of 131

Principal Components

Our centered data is easy to interpret:

  • The first observation had a width that was 2.97 greater than the mean, length 1.35 greater than the mean, etc.
  • Each entry is given in its original pre-centering units (e.g. meters, square meters, etc.).

101

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

102 of 131

Singular Value Interpretation (Informally)

SVD decomposes our data into U, Σ and VT.

  • Let’s talk more about the singular values Σ.

102

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

U

VT

Σ =

103 of 131

Singular Value Interpretation (Informally)

Informally, the ith singular value tells us how valuable the ith principal component will be in reconstructing our original data.

  • First principal component does most of the work.
  • Next two principal components contribute about equally.
  • Fourth principal component does nothing (4th singular value is zero).

103

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

U

VT

Σ =

104 of 131

Singular Value Interpretation (Informally)

Suppose we do SVD of a 6 dimensional dataset and get back the singular values shown, what might be the most appropriate rank to use for our approximation?

104

Σ =

105 of 131

Singular Value Interpretation (Informally)

Suppose we do SVD of a 6 dimensional dataset and get back the singular values shown, what might be the most appropriate rank to use for our approximation?

  • 2? We don’t get nearly as much additional accuracy by also using the next 4 principal components.

105

Σ =

106 of 131

Singular Value Interpretation (More Formally)

Formally, the ith singular value tells us how much of the variance is captured by the ith principal component.

To understand this, first let’s define the total variance of your data as the sum of the individual variances of the attributes.

Width variance: 7.7

Height variance: 5.3

Area variance: 338.7

Perimeter: 50.8

Total variance: 402.56

106

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

107 of 131

Singular Value Interpretation (More Formally)

Formally, the ith singular value tells us how much of the variance is captured by the ith principal component.

  • The amount of variance captured by the ith principal component is equal to (ith singular value)2 / N.

Variance captured by 1st PC: 197.392/100 = 389.63

Variance captured by 2nd PC: 27.432/100 = 7.52

Variance captured by 3rd PC: 23.262 / 100 = 5.41

107

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

Total variance: 402.56

108 of 131

Variance Fraction Arrays and Scree Plots

To get a sense of the relative importance of each principal component, we can compute the fraction of the variance captured in each coordinate:

Or we can plot them on an axis in what is known as a “scree plot”.

108

109 of 131

Principal Component Analysis

Principal Component Analysis (PCA) is the name for what we’ve done today.

  • PCA is the process of (linearly) transforming data into a new coordinate system such that the greatest variance occurs along the first dimension, the second most along the second dimension, and so on.

Variance captured by 1st PC: 197.392/100 = 389.63

Variance captured by 2nd PC: 27.432/100 = 7.52

Variance captured by 3rd PC: 23.262 / 100 = 5.41

109

width

length

area

perimeter

2.97

1.35

24.78

8.64

-3.03

-0.65

-15.22

-7.36

-4.03

-1.65

-20.22

-11.36

...

-3.03

1.35

-11.22

-3.36

110 of 131

Principal Component Analysis Example

110

111 of 131

Introducing PCA: Principal Component Analysis

“The purpose of computing is insight, not numbers.” - Richard Hamming

So far we’ve done a lot of numbers. Why is this useful?

111

112 of 131

Principal Component Analysis for Exploratory Data Analysis

Goal: Plot high dimensional data as a 2 dimensional approximation that results from a linear combinations of attributes.

Related Goal: Determine whether this two-dimensional plot is really showing the variability in the data. (If not, be wary of conclusions drawn using PCA.)

PCA is appropriate for EDA when:

  • Visually identifying clusters of similar observations in high dimensions.
  • You are still exploring the data.
  • You have reason to believe that the data are inherently low rank: there are many attributes, but only a few (perhaps unobserved) attributes mostly determine the rest through a linear association.

112

113 of 131

SVD for PCA

Singular value decomposition (SVD) describes a matrix decomposition:

  • X = UΣVT (or XV = UΣ) where columns of U and V are orthonormal sets and Σ is diagonal.
  • If X has rank R, then there will be R non-zero values on the diagonal of Σ.
  • The values in Σ, called singular values, are ordered from greatest to least.
  • The columns of U are the left singular vectors.
  • The columns of V are the right singular vectors.

Principal component analysis (PCA) is a specific application of SVD:

  • X is a data matrix centered at the mean of each column.
  • The largest n singular values are kept, for some choice of n;�All other singular values are set to zero: the dimensionality reduction.
  • The first n rows of VT are directions for the n principal components.
  • The first n columns of UΣ (or XV) contain the n principal components of X.
  • Primarily utilizes UΣ (by plotting first two columns and scree plot of Σ).

113

114 of 131

Inspecting Principal Component Directions (Axes)

Instead of looking at UΣ, we can instead look at VT.

A principal component direction is a linear combination of attributes.

  • Given as rows of VT.

Plotting the values of the first principal component direction can provide insight into how attributes are being combined.

  • High variance attributes are typically included (but not always).
  • Many attributes are often included, even if only a few are really important.

Interpreting other principal components is challenging; the constraint that they are orthogonal to prior components strongly influences their directions.

114

115 of 131

PCA and clustering?

Let’s discuss on the board…

115

116 of 131

Extra Slides: PCA vs. Regression

116

117 of 131

Regression: The Big Idea

Suppose we know the child mortality rate of a given country.

  • Linear regression tries to predict the fertility rate from the mortality rate.
  • For example, if the mortality is 6, we might guess the fertility is near 4.

117

118 of 131

Regression: The Big Idea

Suppose we know the child mortality rate of a given country.

  • Linear regression tries to predict the fertility rate from the mortality rate.
  • For example, if the mortality is 6, we might guess the fertility is near 4.

The regression line tells us the “best” prediction of fertility given all possible mortality values.

118

119 of 131

Regression: The Big Idea

Suppose we know the child mortality rate of a given country.

  • Linear regression tries to predict the fertility rate from the mortality rate.
  • For example, if the mortality is 6, we might guess the fertility is near 4.

The regression line tells us the “best” prediction of fertility given all possible mortality values.

  • Minimizes the root mean squared error [see vertical red lines, only some shown].

119

120 of 131

Regression: The Big Idea

We can also perform a regression in the reverse direction.

That is, given the fertility, we try to predict the mortality.

In this case, we get a different regression line which minimizes the root mean squared length of the horizontal lines.

120

121 of 131

Rank 1 Approximation of Fertility / Mortality Data

Below we see our data and the rank 1 approximation.

121

122 of 131

Rank 1 Approximation of Fertility / Mortality Data

If we make a line plot instead, this starts to look a lot like a regression line.

  • Note: The approximation is just the data projected on to this line.

122

123 of 131

Rank 1 Approximation vs. Predicted Mortality Regression Line

The rank 1 approximation is not the same as the mortality regression line.

  • They’re vaguely close, but not the same.

123

124 of 131

Rank 1 Approximation vs. Predicted Fertility Regression Line

The fertility regression line is pretty close, but is also different.

  • The closeness is just a coincidence.

124

125 of 131

SVD: Minimizing the Perpendicular Error

Instead of minimizing horizontal or vertical error, our rank 1 approximation minimizes the error perpendicular to the subspace onto which we’re projecting.

That is, SVD finds the line such that if we project our data onto that line, the error between the projection and our original data is minimized.

125

126 of 131

SVD: Minimizing the Perpendicular Error

Reminder: The similarity of the rank 1 approximation and the fertility was just a coincidence.

126

127 of 131

SVD: Minimizing the Perpendicular Error

Looking at adiposity and bicep size from our body measurements dataset, we see the 1D subspace onto which we are projecting is between the two regression lines.

127

128 of 131

Beyond 1D/2D

In higher dimensions the idea behind principal components is just the same!

Example: Suppose we have 30 dimensional data and decide to use the first 5 principal components.

  • Our procedure minimizes the error between:
    • The original 30 dimensional data.
    • The projection of that 30 dimensional data on to the “best” 5 dimensional subspace.

128

129 of 131

Extra Slides: PCA and Optimization

129

130 of 131

FAQ

Why are those two goals equivalent?

Maximizing variance = spreading out red dots

Minimizing error = making red lines short

130

131 of 131

FAQ

Imagine that the black line is a stick, and the red lines are springs attached to the stick from the points.

The first PC is where the stick comes to rest.

SVD finds this for us.

131