Docsity

Principal Component Analysis (PCA): A Comprehensive Guide with Examples, Dispense di Statistica Descrittiva

Anteprima parziale del testo

Scarica Principal Component Analysis (PCA): A Comprehensive Guide with Examples e più Dispense in PDF di Statistica Descrittiva solo su Docsity!

A tutorial on Principal Components Analysis

Lindsay I Smith

February 26, 2002

Chapter 1

Introduction

This tutorial is designed to give the reader an understanding of Principal Components Analysis (PCA). PCA is a useful statistical technique that has found application in fields such as face recognition and image compression, and is a common technique for finding patterns in data of high dimension. Before getting to a description of PCA, this tutorial first introduces mathematical concepts that will be used in PCA. It covers standard deviation, covariance, eigenvec- tors and eigenvalues. This background knowledge is meant to make the PCA section very straightforward, but can be skipped if the concepts are already familiar. There are examples all the way through this tutorial that are meant to illustrate the concepts being discussed. If further information is required, the mathematics textbook “Elementary Linear Algebra 5e” by Howard Anton, Publisher John Wiley & Sons Inc, ISBN 0-471-85223-6 is a good source of information regarding the mathematical back- ground.

of some bigger population. There is a reference later in this section pointing to more information about samples and populations. Here’s an example set:          

I could simply use the symbol

to refer to this entire set of numbers. If I want to refer to an individual number in this data set, I will use subscripts on the symbol

to indicate a specific number. Eg.

refers to the 3rd number in

, namely the number

  1. Note that

is the first number in the sequence, not

like you may see in some textbooks. Also, the symbol  will be used to refer to the number of elements in the set

There are a number of things that we can calculate about a data set. For example, we can calculate the mean of the sample. I assume that the reader understands what the mean of a sample is, and will only give the formula:

^   !"$# 

Notice the symbol

(said “X bar”) to indicate the mean of the set

. All this formula says is “Add up all the numbers and then divide by how many there are”. Unfortunately, the mean doesn’t tell us a lot about the data except for a sort of middle point. For example, these two data sets have exactly the same mean (10), but are obviously quite different:

&%'& % (  *)

So what is different about these two sets? It is the spread of the data that is different. The Standard Deviation (SD) of a data set is a measure of how spread out the data is. How do we calculate it? The English definition of the SD is: “The average distance from the mean of the data set to a point”. The way to calculate it is to compute the squares of the distance from each data point to the mean of the set, add them all up, divide by .-

, and take the positive square root. As a formula:

/ ^ !"$#^

^10 " - ^3254 

Where / is the usual symbol for standard deviation of a sample. I hear you asking “Why are you using 0 6-

and not  ?”. Well, the answer is a bit complicated, but in general, if your data set is a sample data set, ie. you have taken a subset of the real-world (like surveying 500 people about the election) then you must use 0 7-

because it turns out that this gives you an answer that is closer to the standard deviation that would result if you had used the entire population, than if you’d used . If, however, you are not calculating the standard deviation for a sample, but for an entire population, then you should divide by  instead of

. For further reading on this topic, the web page http://mathcentral.uregina.ca/RR/database/RR.09.95/weston2.html describes standard deviation in a similar way, and also provides an example experiment that shows the

Set 1:

0

0 -10 100 8 -2 4 12 2 4 20 10 100 Total 208 Divided by (n-1) 69. Square Root 8.

Set 2:

" 0 "

8 -2 4 9 -1 1 11 1 1 12 2 4 Total 10 Divided by (n-1) 3. Square Root 1.

Table 2.1: Calculation of standard deviation

difference between each of the denominators. It also discusses the difference between samples and populations. So, for our two data sets above, the calculations of standard deviation are in Ta- ble 2.1. And so, as expected, the first set has a much larger standard deviation due to the fact that the data is much more spread out from the mean. Just as another example, the data set: (^)  % %&%&% 

also has a mean of 10, but its standard deviation is 0, because all the numbers are the same. None of them deviate from the mean.

2.1.2 Variance

Variance is another measure of the spread of data in a data set. In fact it is almost identical to the standard deviation. The formula is this:

/^4 ^ !"9#^

includegraphicscovPlot.ps

Figure 2.1: A plot of the covariance data showing positive relationship between the number of hours studied against the mark received

It is exactly the same except that in the second set of brackets, the

F^ ’s are replaced by ’s. This says, in English, “For each data item, multiply the difference between the < value and the mean of < , by the the difference between the = value and the mean of =. Add all these up, and divide by 0 .-

”. How does this work? Lets use some example data. Imagine we have gone into the world and collected some 2-dimensional data, say, we have asked a bunch of students how many hours in total that they spent studying COSC241, and the mark that they received. So we have two dimensions, the first is the H dimension, the hours studied, and the second is the I dimension, the mark received. Figure 2.2 holds my imaginary data, and the calculation of B&CD?^0 H

E

I

, the covariance between the Hours of study done and the Mark received. So what does it tell us? The exact value is not as important as it’s sign (ie. positive or negative). If the value is positive, as it is here, then that indicates that both di- mensions increase together , meaning that, in general, as the number of hours of study increased, so did the final mark. If the value is negative, then as one dimension increases, the other decreases. If we had ended up with a negative covariance here, then that would have said the opposite, that as the number of hours of study increased the the final mark decreased. In the last case, if the covariance is zero, it indicates that the two dimensions are independent of each other. The result that mark given increases as the number of hours studied increases can be easily seen by drawing a graph of the data, as in Figure 2.1.3. However, the luxury of being able to visualize data is only available at 2 and 3 dimensions. Since the co- variance value can be calculated between any 2 dimensions in a data set, this technique is often used to find relationships between dimensions in high-dimensional data sets where visualisation is difficult. You might ask “is B1CD?^0

8E1F,^

equal to B&CD?^0

FJEKA^

”? Well, a quick look at the for- mula for covariance tells us that yes, they are exactly the same since the only dif- ference between B1CD?^0

8E1F,^

and B&CD?^0

FLEMN^

is that 0

A2 0 F

F 72 

is replaced by 0 F" - F2^0 " - A2^. And since multiplication is commutative, which means that it

doesn’t matter which way around I multiply two numbers, I always get the same num- ber, these two equations give the same answer.

2.1.4 The covariance Matrix

Recall that covariance is always measured between 2 dimensions. If we have a data set with more than 2 dimensions, there is more than one covariance measurement that can be calculated. For example, from a 3 dimensional data set (dimensions < , = , >) you could calculate B&CD?^0 <

E

, 0 B&CD?^0 <

E

, and B&CD?^0 =

E

. In fact, for an  -dimensional data set, you can calculate Q !PO !SR

DT

O

U

4 different covariance values.

Hours(H) Mark(M) Data 9 39 15 56 25 93 14 61 10 50 18 75 0 32 16 85 5 42 19 70 16 66 20 80 Totals 167 749 Averages 13.92 62.

Covariance:

H I

H " -

H

I " -

I

H " -

H

I " -

I

9 39 -4.92 -23.42 115. 15 56 1.08 -6.42 -6. 25 93 11.08 30.58 338. 14 61 0.08 -1.42 -0. 10 50 -3.92 -12.42 48. 18 75 4.08 12.58 51. 0 32 -13.92 -30.42 423. 16 85 2.08 22.58 46. 5 42 -8.92 -20.42 182. 19 70 5.08 7.58 38. 16 66 2.08 3.58 7. 20 80 6.08 17.58 106. Total 1149. Average 104.

Table 2.2: 2-dimensional data set and covariance calculation

ed   f

d

ed   f

d 

g f

d 

Figure 2.2: Example of one non-eigenvector and one eigenvector

f

d 

ed   f

g f

Figure 2.3: Example of how a scaled eigenvector is still and eigenvector

2.2.1 Eigenvectors

As you know, you can multiply two matrices together, provided they are compatible sizes. Eigenvectors are a special case of this. Consider the two multiplications between a matrix and a vector in Figure 2.2. In the first example, the resulting vector is not an integer multiple of the original vector, whereas in the second example, the example is exactly 4 times the vector we began with. Why is this? Well, the vector is a vector in 2 dimensional space. The vector

d  (from the second example multiplication) represents an arrow pointing from the origin,

SEh% , to the point

d SEh

. The other matrix, the square one, can be thought of as a transformation matrix. If you multiply this matrix on the left of a vector, the answer is another vector that is transformed from it’s original position. It is the nature of the transformation that the eigenvectors arise from. Imagine a transformation matrix that, when multiplied on the left, reflected vectors in the line =

<. Then you can see that if there were a vector that lay on the line =

< , it’s reflection it itself. This vector (and all multiples of it, because it wouldn’t matter how long the vector was), would be an eigenvector of that transformation matrix. What properties do these eigenvectors have? You should first know that eigenvec- tors can only be found for square matrices. And, not every square matrix has eigen- vectors. And, given an  f  matrix that does have eigenvectors, there are  of them. Given a d f d matrix, there are 3 eigenvectors. Another property of eigenvectors is that even if I scale the vector by some amount before I multiply it, I still get the same multiple of it as a result, as in Figure 2.3. This is because if you scale a vector by some amount, all you are doing is making it longer,

not changing it’s direction. Lastly, all the eigenvectors of a matrix are perpendicular , ie. at right angles to each other, no matter how many dimensions you have. By the way, another word for perpendicular, in maths talk, is orthogonal. This is important because it means that you can express the data in terms of these perpendicular eigenvectors, instead of expressing them in terms of the < and = axes. We will be doing this later in the section on PCA. Another important thing to know is that when mathematicians find eigenvectors, they like to find the eigenvectors whose length is exactly one. This is because, as you know, the length of a vector doesn’t affect whether it’s an eigenvector or not, whereas the direction does. So, in order to keep eigenvectors standard, whenever we find an eigenvector we usually scale it to make it have a length of 1, so that all eigenvectors have the same length. Here’s a demonstration from our example above.

d 

is an eigenvector, and the length of that vector is

0 d^4 i^ ^42 kj^ ld

so we divide the original vector by this much to make it have a length of 1.

d  m j dn d So (^) j &d So (^) j &d

How does one go about finding these mystical eigenvectors? Unfortunately, it’s only easy(ish) if you have a rather small matrix, like no bigger than about

d f

d

. After that, the usual way to find the eigenvectors is by some complicated iterative method which is beyond the scope of this tutorial (and this author). If you ever need to find the eigenvectors of a matrix in a program, just find a maths library that does it all for you. A useful maths package, called newmat, is available at http://webnz.com/robert/. Further information about eigenvectors in general, how to find them, and orthogo- nality, can be found in the textbook “Elementary Linear Algebra 5e” by Howard Anton, Publisher John Wiley & Sons Inc, ISBN 0-471-85223-6.

2.2.2 Eigenvalues

Eigenvalues are closely related to eigenvectors, in fact, we saw an eigenvalue in Fig- ure 2.2. Notice how, in both those examples, the amount by which the original vector was scaled after multiplication by the square matrix was the same? In that example, the value was 4. 4 is the eigenvalue associated with that eigenvector. No matter what multiple of the eigenvector we took before we multiplied it by the square matrix, we would always get 4 times the scaled vector as our result (as in Figure 2.3). So you can see that eigenvectors and eigenvalues always come in pairs. When you get a fancy programming library to calculate your eigenvectors for you, you usually get the eigenvalues as well.

Chapter 3

Principal Components Analysis

Finally we come to Principal Components Analysis (PCA). What is it? It is a way of identifying patterns in data, and expressing the data in such a way as to highlight their similarities and differences. Since patterns in data can be hard to find in data of high dimension, where the luxury of graphical representation is not available, PCA is a powerful tool for analysing data. The other main advantage of PCA is that once you have found these patterns in the data, and you compress the data, ie. by reducing the number of dimensions, without much loss of information. This technique used in image compression, as we will see in a later section. This chapter will take you through the steps you needed to perform a Principal Components Analysis on a set of data. I am not going to describe exactly why the technique works, but I will try to provide an explanation of what is happening at each point so that you can make informed decisions when you try to use this technique yourself.

3.1 Method

Step 1: Get some data

In my simple example, I am going to use my own made-up data set. It’s only got 2 dimensions, and the reason why I have chosen this is so that I can provide plots of the data to show what the PCA analysis is doing at each step. The data I have used is found in Figure 3.1, along with a plot of that data.

Step 2: Subtract the mean

For PCA to work properly, you have to subtract the mean from each of the data dimen- sions. The mean subtracted is the average across each dimension. So, all the < values have

< (the mean of the < values of all the data points) subtracted, and all the = values have

= subtracted from them. This produces a data set whose mean is zero.

Data =

x y 2.5 2. 0.5 0. 2.2 2. 1.9 2. 3.1 3. 2.3 2. 2 1. 1 1. 1.5 1. 1.1 0.

DataAdjust =

x y .69. -1.31 -1. .39. .09. 1.29 1. .49. .19 -. -.81 -. -.31 -. -.71 -1.

0

1

2

3

4

-1 0 1 2 3 4

Original PCA data "./PCAdata.dat"

Figure 3.1: PCA example data, original data on the left, data with the means subtracted on the right, and a plot of the data

-1.

-0.

0

1

2

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

Mean adjusted data with eigenvectors overlayed "PCAdataadjust.dat" (-.740682469/.671855252)x (-.671855252/-.740682469)x

Figure 3.2: A plot of the normalised data (mean subtracted) with the eigenvectors of the covariance matrix overlayed on top.

will notice that the eigenvalues are quite different values. In fact, it turns out that the eigenvector with the highest eigenvalue is the principle component of the data set. In our example, the eigenvector with the larges eigenvalue was the one that pointed down the middle of the data. It is the most significant relationship between the data dimensions. In general, once eigenvectors are found from the covariance matrix, the next step is to order them by eigenvalue, highest to lowest. This gives you the components in order of significance. Now, if you like, you can decide to ignore the components of lesser significance. You do lose some information, but if the eigenvalues are small, you don’t lose much. If you leave out some components, the final data set will have less dimensions than the original. To be precise, if you originally have  dimensions in your data, and so you calculate  eigenvectors and eigenvalues, and then you choose only the first { eigenvectors, then the final data set has only { dimensions. What needs to be done now is you need to form a feature vector , which is just a fancy name for a matrix of vectors. This is constructed by taking the eigenvectors that you want to keep from the list of eigenvectors, and forming a matrix with these eigenvectors in the columns.

| s(zv@ (^) sy}~syBZz1C @n (^0) s\Zt  s\Zt (^4) s\Zt  (^) qq€q€q s\Zt !

Given our example set of data, and the fact that we have 2 eigenvectors, we have two choices. We can either form a feature vector with both of the eigenvectors:

  • q

r& dd

  • q &d  
  • q

&dr   q  &dd

or, we can choose to leave out the smaller, less significant component and only have a single column:

  • q

(^)  &dd

  • q

d& 

We shall see the result of each of these in the next section.

Step 5: Deriving the new data set

This the final step in PCA, and is also the easiest. Once we have chosen the components (eigenvectors) that we wish to keep in our data and formed a feature vector, we simply take the transpose of the vector and multiply it on the left of the original data set, transposed.

|


u (^) [ (z(6k (^) CD‚ | s(zv@ (^) sx}7syBZz1C @ (^) f  CD‚ [ (z(ƒ^ )

„

v /hz^

E

where

CD‚^

| s(zv†@^ sy}6syBZz1C @ is the matrix with the eigenvectors in the columns trans- posed so that the eigenvectors are now in the rows, with the most significant eigenvec- tor at the top, and

CD‚^

[ (z(ƒ^ )

„

v /hz^ is the mean-adjusted data transposed , ie. the data items are in each column, with each row holding a separate dimension. I’m sorry if this sudden transpose of all our data confuses you, but the equations from here on are

Transformed Data=

-.827970186 -. 1.77758033. -.992197494. -.274210416. -1.67580142 -. -.912949103. .0991094375 -. 1.14457216. .438046137. 1.22382056 -.

-1.

-0.

0

1

2

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

Data transformed with 2 eigenvectors "./doublevecfinal.dat"

Figure 3.3: The table of data by applying the PCA analysis using both eigenvectors, and a plot of the new data points.

Transformed Data (Single eigenvector) < -.

-. -. -1. -. .

.

Figure 3.4: The data after transforming using only the most significant eigenvector

So, how do we get the original data back? Before we do that, remember that only if we took all the eigenvectors in our transformation will we get exactly the original data back. If we have reduced the number of eigenvectors in the final transformation, then the retrieved data has lost some information. Recall that the final transform is this: |


(u (^) [ (z(6k (^) CD‚ | s(zv@ (^) sx}7syBZz1C @ (^) f  CD‚ [ (z(ƒ^ )

„

v /hz^

E

which can be turned around so that, to get the original data back,

 CD‚ [ (z(ƒ ) ‡„

v /hz^ ˆ^ CD‚ | s(zv@^ sx}7syBZz1C @ (^) R

(^) f | \ (u^ [ (z(

where

CD‚^

| s(zv†@^ sy}6syBZz1C @ R

(^) is the inverse of  CD‚ | s(zv@^ sx}7syBZz1C @. However, when

we take all the eigenvectors in our feature vector, it turns out that the inverse of our feature vector is actually equal to the transpose of our feature vector. This is only true because the elements of the matrix are all the unit eigenvectors of our data set. This makes the return trip to our data easier, because the equation becomes

 CD‚ [ (z(ƒ )

„

v /hz^ ˆ^ CD‚ | s(zv@^ sy}7sxBMz1C @‰^ f

| \

(u (^) [ (z(

But, to get the actual original data back, we need to add on the mean of that original data (remember we subtracted it right at the start). So, for completeness,

 CD‚7Š @\Zt‹
(^)  (u^ [ (z(7^0  CD‚ | s(zv@^ sy}6syBZz1C @‰^ f

| \

(‹u (^) [ (z(P2 i Š @\Zt
(^)  (u^ I (^) s( 

This formula also applies to when you do not have all the eigenvectors in the feature vector. So even when you leave out some eigenvectors, the above equation still makes the correct transform. I will not perform the data re-creation using the complete feature vector, because the result is exactly the data we started with. However, I will do it with the reduced feature vector to show you how information has been lost. Figure 3.5 show this plot. Compare