Dimensionality reduction
Today: Linear
Tomorrow: non-linear (t-SNE - Itay)
1
Dimensionality
Suppose we have a dataset of N observations with d attributes.
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
Dimensionality
Suppose we have a dataset of N observations with d attributes.
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
Dimensionality
Consider the data shown.
Would you call this dataset:
4
Weight (lbs) | Weight (kg) |
113.0 | 51.3 |
136.5 | 61.9 |
153.0 | 69.4 |
? dimensions
Dimensionality
Consider the data shown.
Would you call this dataset:
5
Weight (lbs) | Weight (kg) |
113.0 | 51.3 |
136.5 | 61.9 |
153.0 | 69.4 |
1 dimension
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 |
Dimensionality
Consider the data shown.
How many dimensions does this data have?
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 |
Note on Rank and Dimensionality
Note that in the dataset below, I’ve added one outlier point.
8
Matrix Multiplication Example
Consider the matrix multiplication example below.
2 | 2 | 2 |
5 | 8 | 0 |
Number of Fruits
Bowl 1
Bowl 2
# Apples
# Lemons
# Melons
Matrix Multiplication Example
Consider the matrix multiplication example below.
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
Matrix Multiplication Example
Consider the matrix multiplication example below.
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
Matrix Multiplication Example
Consider the matrix multiplication example below.
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
View 1: Matrices as Linear Operations
One view of matrix multiplication: Performs multiple linear operations on data.
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!)
View 2: Matrices as Coordinate Transformations
Another view is that matrix multiplication is a coordinate transformation.
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!
Matrices Have Many Representations
Takeaway: a matrix can be interpreted many different ways. We discussed two today:
See 3blue1brown’s Essence of Linear Algebra videos for more intuition.
Interpreting a Matrix Multiplication Operation in Context
Suppose we compute the matrix product below. Interpret the resulting matrix.
16
Age (days) | Height (in) |
182 | 28 |
399 | 30 |
725 | 33 |
1 | 0 | 0 |
0 | 1 | 1/12 |
x
=
Interpretation of the Results of the Operation
Suppose we compute the matrix product below. Interpret the resulting matrix.
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 |
Interpretation of the Matrix Multiplication Process (View 1)
Suppose we compute the matrix product below. Interpret the resulting matrix.
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 |
Interpretation of the Matrix Multiplication Process (View 2)
Suppose we compute the matrix product below. Interpret the resulting matrix.
The right matrix transforms the data from 2D into 3D.
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 |
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 |
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.
21
Age (days) | Height (in) | Height (ft) |
182 | 28 | 2.33 |
399 | 30 | 2.5 |
725 | 33 | 2.75 |
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.
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
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.
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
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.
Why is this impossible?
24
Age (days) | Height (in) | Height (ft) |
182 | 28 | 2.33 |
399 | 30 | 2.5 |
725 | 33 | 2.75 |
? |
? |
? |
? | ? | ? |
x
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.
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!
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.
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!
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
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
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.
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
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.
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
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.
31
width | length | area | perimeter |
20 | 20 | 400 | 80 |
16 | 12 | 192 | 56 |
24 | 12 | 288 | 72 |
...
25 | 24 | 600 | 98 |
100 x 4
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.
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
Singular Value Decomposition
Singular value decomposition is an important concept in linear algebra.
Redundancy and Decomposition
Earlier, we saw that our rectangle data could be decomposed into two matrices:
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
Singular Value Decomposition
The “Singular Value Decomposition” technique will automatically do a similar transformation for us.
35
m x 4
=
x
UΣ
4 x n
VT
Same dimensions as the original matrix
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
UΣ
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 |
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
UΣ
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 |
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
UΣ
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 |
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
UΣ
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 |
Matrix Decomposition Summary
Decomposing a MxN matrix X into two matrix factors of size MxQ and QxN does not yield a unique answer.
We manually performed a decomposition where:
The “Singular Value Decomposition” decomposes X into matrices of size MxN and NxN.
40
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
??
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.
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
Last Lecture’s Final Goal: Matrix Decomposition
Other factorizations are possible, e.g.
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
Desiderata
One possible goal: Find a procedure which can automatically decompose a rank R matrix into an R dimensional representation times some transformation matrix.
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
Approximate Factorization
If we go to a two dimensional left matrix, can no longer exactly reconstruct the 4D matrix.
Still, some 2D matrices yield better approximations than others.
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
≈
Approximate Factorization (My Answer)
There are infinite possible factorization. One intuitive choice (to me) is below.
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
≈
Approximate Factorization (My Answer)
There are infinite possible factorization. One intuitive choice (to me) is below.
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
≈
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.
Still, some 1D matrices yield better approximations than others.
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
≈
Desiderata
Revised goal: Find a procedure which automatically decomposes a matrix into a P dimensional representation times some transformation matrix.
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
Singular Value Decomposition
Singular Value Decomposition
The “Singular Value Decomposition” automatically decomposes a matrix into two matrices:
51
m x 4
=
x
X
UΣ
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 |
...
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
UΣ
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 |
...
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?
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 |
...
UΣ
??? | ??? | ??? | ??? |
-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 |
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 UΣ 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 |
...
UΣ
??? | ??? | ??? | ??? |
-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 |
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 UΣ and 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 |
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 UΣ and VT?
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 |
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 UΣ and VT?
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 |
...
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 UΣ and VT?
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 |
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?
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 |
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?
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 |
Multiplying Out the Result
We can multiply out UΣVT and see how close the result is to our original matrix X.
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 |
Going Down to One Dimension
If we go all the way down to one dimension for UΣ, the results are better than you might expect.
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 |
...
Going Down to One Dimension
If we go all the way down to one dimension for UΣ, the results are better than you might expect.
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 |
...
Going Down to One Dimension (Your Answer)
If we go all the way down to one dimension for UΣ, the results are better than you might expect.
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 |
...
Matrix Decomposition Summary
Decomposing a MxN matrix X into two matrix factors of size MxQ and QxN does not yield a unique answer.
We manually performed a decomposition where:
The “Singular Value Decomposition” decomposes X into matrices of size MxN and NxN.
65
Manual Decomposition
Before, we took our data and manually created a 3D to 4D transformation matrix.
Decomposed data into:
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
Singular Value Decomposition
Then we used SVD which automatically created a 3D to 4D transformation matrix.
Decomposed data into:
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
UΣ
Singular Value Decomposition
The truth about UΣ and VT:
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
UΣ
Diagonal Matrices and Σ
A diagonal matrix is a matrix with zeros everywhere except possibly the diagonal.
In Σ, the singular values appear in decreasing order.
69
Singular Value Decomposition
The truth about UΣ and VT:
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
UΣ
Orthogonality, Dot Products, Vector Length
Two orthogonal vectors:
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
Orthonormality
A set of vectors is said to be an orthonormal set if:
72
Orthonormality
A set of vectors is said to be an orthonormal set if:
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
Orthonormality
A set of vectors is said to be an orthonormal set if:
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.
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
Orthonormality
A set of vectors is said to be an orthonormal set if:
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.
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
Principal Components
76
Principal Component
When performing SVD, we’ve left the columns in the “data” matrix UΣ unnamed.
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
UΣ
*: There’s an important technical detail we’ll need to fix first (coming in a few slides).
Interpreting Principal Components
Let’s look at a geometric interpretation of:
This is easiest if we work with a simple dataset with 2 attributes.
78
Interpreting Principal Components
Let’s look at a geometric interpretation of:
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
Data, Rank 2, and Rank 1 Approximation
Below, we see the original data, along with the rank 2 and rank 1 approximations.
80
Original Data
Rank 2 Approximation
Rank 1 Approximation
The Rank 1 Approximation Visually
We see that the rank 1 approximation projects the data on to a 1D subspace.
81
The Rank 1 Approximation Visually
There’s a subtle but significant issue with our rank 1 approximation. What is it?
82
The Rank 1 Approximation Visually
Our approximation goes through the origin, but original data has non-zero y-intercept.
83
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.
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
Data Uncentering
After performing the approximation, you can add back the means to get back to the original x and y scale.
86
Principal Component
When performing SVD, we’ve left the columns in the “data” matrix UΣ unnamed.
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
UΣ
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.
In essence, the 1st principal component gives each country a single score along this intuitive axis!
88
High PC1 countries
Low PC1 countries
Computing PC1
Given U, Σ, and VT below, write an expression to compute PC1 for Afghanistan.
89
U
VT
Σ
...
Computing PC1
Since the principal components are just the columns of UΣ, we just compute the top left entry of UΣ:
90
U
VT
Σ
...
PC1 Computation
Below, we see a table showing PC1 for every country.
91
High PC1 countries
Low PC1 countries
Plotting Countries vs. PC1
Naturally, we can plot countries vs. PC1.
92
High PC1 countries
Low PC1 countries
Somalia
Chad
Singapore
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
PC1 Back to 2D
To get our rank 1 estimates for Afghanistan:
94
Somalia
Chad
Singapore
VT
PC1 Back to 2D
To get our rank 1 estimates for Afghanistan:
95
Somalia
Chad
Singapore
VT
PC1 Back to 2D
The first principal component captures much of the variance, but not all.
By using 2nd principal component, we can get back original data exactly.
96
Somalia
Chad
Singapore
VT
PC2
PC2 is harder to interpret.
The gray area shows countries with similar values of PC1.
97
Low PC2 countries
High PC2 countries
Intuitive Picture of Low Rank Approximation
Approach 2:
98
U
VT
Σ
...
Principal Components and Variance
99
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 |
Principal Components
Our centered data is easy to interpret:
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 |
Singular Value Interpretation (Informally)
SVD decomposes our data into U, Σ and VT.
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
Σ =
Singular Value Interpretation (Informally)
Informally, the ith singular value tells us how valuable the ith principal component will be in reconstructing our original data.
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
Σ =
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
Σ =
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?
105
Σ =
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 |
Singular Value Interpretation (More Formally)
Formally, the ith singular value tells us how much of the variance is captured by the ith principal component.
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
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
Principal Component Analysis
Principal Component Analysis (PCA) is the name for what we’ve done today.
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 |
Principal Component Analysis Example
110
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
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:
112
SVD for PCA
Singular value decomposition (SVD) describes a matrix decomposition:
Principal component analysis (PCA) is a specific application of SVD:
113
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.
Plotting the values of the first principal component direction can provide insight into how attributes are being combined.
Interpreting other principal components is challenging; the constraint that they are orthogonal to prior components strongly influences their directions.
114
PCA and clustering?
Let’s discuss on the board…
115
Extra Slides: PCA vs. Regression
116
Regression: The Big Idea
Suppose we know the child mortality rate of a given country.
117
Regression: The Big Idea
Suppose we know the child mortality rate of a given country.
The regression line tells us the “best” prediction of fertility given all possible mortality values.
118
Regression: The Big Idea
Suppose we know the child mortality rate of a given country.
The regression line tells us the “best” prediction of fertility given all possible mortality values.
119
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
Rank 1 Approximation of Fertility / Mortality Data
Below we see our data and the rank 1 approximation.
121
Rank 1 Approximation of Fertility / Mortality Data
If we make a line plot instead, this starts to look a lot like a regression line.
122
Rank 1 Approximation vs. Predicted Mortality Regression Line
The rank 1 approximation is not the same as the mortality regression line.
123
Rank 1 Approximation vs. Predicted Fertility Regression Line
The fertility regression line is pretty close, but is also different.
124
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
SVD: Minimizing the Perpendicular Error
Reminder: The similarity of the rank 1 approximation and the fertility was just a coincidence.
126
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
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.
128
Extra Slides: PCA and Optimization
129
FAQ
Why are those two goals equivalent?
Maximizing variance = spreading out red dots
Minimizing error = making red lines short
130
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