brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Nov 29. 2020

앤드류 응의 머신러닝(14-6):PCA 파라미터 k결정

   온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Dimensionality Reduction 

(차원 축소)


Applying PCA (PCA 적용)  


Choosing the Number of Principal Components 

(주성분의 숫자 k 결정하기)    


   In the PCA algorithm we take N dimensional features and reduce them to some K dimensional feature representation. This number K is a parameter of the PCA algorithm. This number K is also called the number of principle components or the number of principle components that we've retained. And in this video I'd like to give you some guidelines, tell you about how people tend to think about how to choose this parameter K for PCA.


   PCA 알고리즘은 n 차원의 피처를 k 차원의 피처로 축소합니다. k는 PCA 알고리즘의 파라미터이자 주성분(Principal Components)의 수입니다. 이번 강의는 PCA의 파라미터 k를 선택하는 방법을 설명하고 몇 가지 가이드라인을 제시합니다. 


    


   In order to choose k, that is to choose the number of principal components, here are a couple of useful concepts. What PCA tries to do is it tries to minimize the average squared projection error. So it tries to minimize this quantity, which I'm writing down, which is the difference between the original data X and the projected version, X-approx-i, which was defined last video, so it tries to minimize the squared distance between x and it's projection onto that lower dimensional surface. So that's the average square projection error.

   Also let me define the total variation in the data to be the average length squared of these examples Xi so the total variation in the data is the average of my training sets of the length of each of my training examples. And this one says, "On average, how far are my training examples from the vector, from just being all zeros?" How far is, how far on average are my training examples from the origin? 


   여기 주성분의 수 k를 선택하는 몇 가지 유용한 개념이 있습니다.  PCA는 평균 투영 오차의 제곱을 최소화합니다.  x^(i)approx는 원래 데이터 x^(i)를 저차원 표면으로 투영한 것입니다. 이것이 평균 투영 오차 제곱입니다. 

     또, 데이터의 총변화량을 정의합니다.

   각 학습 예제가 원점부터 데이터 셋까지의 길이의 제곱에 대한 평균입니다. 이 식의 의미는 다음과 같습니다. "평균적으로 학습에서 원점으로부터 얼마나 멀리 떨어져 있을까요? 또는 평균적으로 학습 예제가 원래 값에서 얼마나 떨어져 있을까요?"


   

    When we're trying to choose k, a pretty common rule of thumb for choosing k is to choose the smaller values so that the ratio between these is less than 0.01. So in other words, a pretty common way to think about how we choose k is we want the average squared projection error that is the average distance between x and it's projections divided by the total variation of the data that is how much the data varies. We want this ratio to be less than, let's say, 0.01. Or to be less than 1%, which is another way of thinking about it. And the way most people think about choosing K is rather than choosing K directly the way most people talk about it is as what this number is, whether it is 0.01 or some other number. And if it is 0.01, another way to say this to use the language of PCA is that 99% of the variance is retained. I don't really want to, don't worry about what this phrase really means technically but this phrase "99% of variance is retained" just means that this quantity on the left is less than 0.01. 


   PCA 파라미터 k를 선택하는 일반적인 규칙은 평균 투영 오차의 제곱을 데이터의 총변화량의 평균으로 나눈 값이 0.01 미만이 되도록 하는 것입니다. 



   평균 투영 오차의 제곱은 데이터 x와 투영한 데이터 사이의 평균 거리입니다. 데이터의 총변화량은 데이터가 얼마나 변하는 지를 나타냅니다. 이 값이 0.01 이거나 1% 보다 작아야 합니다. 대부분은 값이 0.01 또는 다른 숫자인지는 중요하지 않습니다. PCA에서 '0.01'의 의미는" 99%의 분산을 유지한다"라는 의미입니다. 이 문구가 기술적으로 무엇을 의미하는지는 중요하지 않습니다. "99%의 분산을 유지한다"는 문구는 왼쪽의 수가 0.01 미만이라는 의미입니다. 여기서 분산은 데이터가 평균으로부터 얼마나 떨어져 있는 지를 나타는 값들의 제곱의 평균입니다. 


   And so, if you are using PCA and if you want to tell someone, you know, how many principle components you've retained it would be more common to say well, I chose k so that 99% of the variance was retained. And that's kind of a useful thing to know, it means that you know, the average squared projection error divided by the total variation that was at most 1%. That's kind of an insightful thing to think about, whereas if you tell someone that, "Well I had to 100 principle components" or "k was equal to 100 in a thousand dimensional data" it's a little hard for people to interpret that. So this number 0.01 is what people often use. 


   그래서, 누군가에게 PCA를 사용한다고 말할 때 얼마나 많은 주성분을 보유하는지를 설명하는 지를 함께 설명합니다. "99%의 분산을 유지하도록 k를 선택합니다." 알아두면 매우 유용한 정보입니다. 평균 투영 오차의 제곱을 최대 1%의 변화량으로 나눈 값입니다. 이런 통찰력을 누군가에게 "나는 100개의 주성분을 유지합니다." 또는 "1,000차원 데이터에서 k는 100입니다"라고 말한다면 사람들이 해석하기 조금 어렵습니다. 그래서, 이 숫자 0.01을 자주 사용합니다. 



   Other common values is 0.05, and so this would be 5%, and if you do that then you go and say well 95% of the variance is retained and, you know other numbers maybe 90% of the variance is retained, maybe as low as 85%. So 90% would correspond to say 0.10, kinda 10%. And so range of values from, you know, 90, 95, 99, maybe as low as 85% of the variables contained would be a fairly typical range in values. Maybe 95 to 99 is really the most common range of values that people use. For many data sets you'd be surprised, in order to retain 99% of the variance, you can often reduce the dimension of the data significantly and still retain most of the variance. Because for most real life data says many features are just highly correlated, and so it turns out to be possible to compress the data a lot and still retain you know 99% of the variance or 95% of the variance.


   다른 값은 0.05이고 5%이면,  95%의 분산을 유지합니다. 또, 90%의 분산을 유지하거나 85%만큼 낮을 수도 있습니다. 90%의 분산 0.1과 10%의 값에 대응합니다. 85%, 90%, 95%, 99% 범위를 가진 변수도 일반적이지만, 95%~ 99%가 가장 일반적인 범위입니다. 많은 데이터 셋에서 99%의 분산을 유지하기 위해 데이터의 차원을 크게 줄이면서도 분산을 유지합니다. 대부분의 실제 데이터에서 많은 피처가 상관관계가 높기 때문에 데이터를 많이 압축하면서도 99%의 분산 또는 95%의 분산을 유지하는 것이 가능합니다. 그렇다면 어떻게 구현할 수 있을까요?



    So how do you implement this? Well, here's one algorithm that you might use. You may start off, if you want to choose the value of k, we might start off with k equals 1. And then we run through PCA. You know, so we compute, you reduce, compute z1, z2, up to zm. Compute all of those xapprox^(1) and so on up to xapprox^(m) and then we check if 99% of the variance is retained. Then we're good and we use k equals 1.


   그렇다면 어떻게 구현할 수 있을까요? 여기 사용할 수 있는 알고리즘이 하나 있습니다. 먼저 k의 값을 선택합니다. k = 1로 시작할 수 있습니다. PCA를 실행합니다. Ureduce를 만들고 z1, z2,..., zm까지 계산합니다. 모든 xapprox^(1), xapprox^(2),..., xapprox^(m)까지 모두 계산한 다음 99%의 분산이 유지되는 지를 확인합니다. 결과가 좋다면 k = 1을 사용합니다.



   But if it isn't then what we'll do we'll next try K equals 2. And then we'll again run through this entire procedure and check, you know is this expression satisfied. Is this less than 0.01. And if not then we do this again. Let's try k equals 3, then try k equals 4, and so on until maybe we get up to k equals 17 and we find 99% of the data have is retained and then we use k equals 17, right?  That is one way to choose the smallest value of k, so that and 99% of the variance is retained. 


   결과가 좋지 않다면, K = 2를 시도합니다. 여기 전체 절차를 다시 한번 반복하고 0.01 미만의 값인지를 확인합니다. 결과가 좋지 않다면 K = 3, K = 4, 그리고 k = 17이 될 때까지 시도합니다. k = 17일 때 99%의 분산이 유지되는 것을 확인합니다. 이것이 99%의 분산이 유지되고  k의 가장 작은 값을 선택하는 한 가지 방법입니다. 


 

   But as you can imagine, this procedure seems horribly inefficient we're trying k equals one, k equals two, we're doing all these calculations. Fortunately when you implement PCA it actually, in this step, it actually gives us a quantity that makes it much easier to compute these things as well. Specifically when you're calling SVD to get these matrices u, s, and d, when you're calling usvd on the covariance muratrix sigma, it also gives us back this matrix S and what S is, is going to be a square matrix an N by N matrix in fact,  that is diagonal. So is diagonal entries s one one, s two two, s three three down to s n n are going to be the only non-zero elements of this matrix, and everything off the diagonals is going to be zero. Okay? So those big O's that I'm drawing, by that what I mean is that everything off the diagonal of this matrix all of those entries there are going to be zeros. And so, what is possible to show, and I won't prove this here, and it turns out that for a given value of k, this quantity over here can be computed much more simply. And that quantity can be computed as one minus sum from i equals 1 through K of Sii divided by sum from I equals 1 through N of Sii.


   그러나, 이 프로세스는 k = 1, k = 2에 대한 모든 계산을 해야 합니다. 다행히도 PCA를 구현할 때 계산을 훨씬 더 쉽게 만드는 함수가 있습니다.


       [U, S, D] = svd(Sigma);


   여기서, 공분산 행렬 Sigma에 대해 svd() 함수를 실행하면, 행렬 S를 반환합니다. 행렬 S는 n X n 정방 행렬입니다. 정방 행렬은 대각선 성분 s11, s22, s33,..., snn을 제외한 모든 성분이 0인 행렬입니다. 그래서, 큰 O는 대각선의 행렬을 제외한 모든 성분의 값이 0이라는 의미입니다. 무엇을 보여줄 수 있을지는 증명하지 않을 것입니다. 단지 주어진 k의 값을 훨씬 더 간단하게 계산할 수 있다는 것을 의미합니다. 

   여기서 분자의 Σ는 i = 1부터 k까지의 합산이고, 분모의 Σ는 i = 1부터 n까지 입니다. 


   

   So just to say that it words, or just to take another view of how to explain that, if K equals 3 let's say. What we're going to do to compute the numerator is sum from one-- I equals 1 through 3 of of Sii, so just compute the sum of these first three elements. So that's the numerator. And then for the denominator, well that's the sum of all of these diagonal entries. And one minus the ratio of that, that gives me this quantity over here, that I've circled in blue. And so, what we can do is just test if this is less than or equal to 0.01. Or equivalently, we can test if the sum from i equals 1 through k, s-i-i divided by sum from i equals 1 through n, s-i-i if this is greater than or equal to 0.99, if you want to be sure that 99% of the variance is retained. And so what you can do is just slowly increase k, set k equals one, set k equals two, set k equals three and so on, and just test this quantity to see what is the smallest value of k that ensures that 99% of the variance is retained.


   또 다른 관점에서 설명하기 위해 k = 3이라고 가정합니다. 분자를 계산하기 위해 i = 1에서 3까지의 합을 계산합니다. 즉, 처음 3개 성분의 합계  s11 + s22 + s33입니다. 분모는 모든 대각선 성분의 합 s11 + s22 + s33 +... + snn입니다. 분자는 빨간색 타원이고, 분모는 분홍색 타원입니다. 그리고, 여기에서 1을 때면  0.01보다 작거나 같은 지를 테스트합니다. 같은 의미로 아래와 같은 두식을 만들 수 있습니다. 

 

   이제 k의 값을 하나씩 증가시킵니다. k = 1, k =2, k =3으로 증가시키면서  99%의 분산이 유지되도록 가장 작은 값이 무엇인지를 확인합니다. 


   And if you do this, then you need to call the SVD function only once. Because that gives you the S matrix and once you have the S matrix, you can then just keep on doing this calculation by increasing the value of K in the numerator and so you don't need keep to calling SVD over and over again to test out the different values of K.    So this procedure is much more efficient, and this can allow you to select the value of K without needing to run PCA from scratch over and over. You just run SVD once, this gives you all of these diagonal numbers, all of these numbers S11, S22 down to SNN, and then you can just you know, vary K in this expression to find the smallest value of K, so that 99% of the variance is retained. 


   이렇게 하면 svd() 함수를 한 번만 호출합니다. 반환받은 행렬 S에서 분자에서 k 값을 늘려가면서 계산을 계속하면서 테스트를 합니다. 즉, k의 다른 값을 테스트하기 위해 SVD 함수를 계속 호출할 필요가 없습니다. 이 절차가 훨씬 더 효율적이며 PCA를 처음부터 계속 실행할 필요 없이 k 값을 선택할 수 있습니다. svd()를 한 번만 실행하면 모든 대각선 숫자 s11, s22,..., snn 까지가 제공됩니다. k 값을 변화시켜 k의 가장 작은 값을 찾으면 99%의 분산이 유지됩니다. 



   So to summarize, the way that I often use, the way that I often choose K when I am using PCA for compression is I would call SVD once in the covariance matrix, and then I would use this formula and pick the smallest value of K for which this expression is satisfied.


   요약하면, 압축을 위해 PCA를 선택할 때 k를 선택하는 방식은 간단합니다. 공분산 행렬에서 svd() 함수를 한 번 호출합니다. 다음 공식을 사용하여 k의 가장 작은 값을 선택합니다. 


   And by the way, even if you were to pick some different value of K, even if you were to pick the value of K manually, you know maybe you have a thousand dimensional data and I just want to choose K equals one hundred. Then, if you want to explain to others what you just did, a good way to explain the performance of your implementation of PCA to them, is actually to take this quantity and compute what this is, and that will tell you what was the percentage of variance retained. And if you report that number, then, you know, people that are familiar with PCA, and people can use this to get a good understanding of how well your hundred dimensional representation is approximating your original data set, because there's 99% of variance retained. 


   그런데, k 값을 직접 선택하더라도 여러분은 1,000차원의 데이터를 k = 100차원으로 줄이고 싶습니다. 그리고 다른 사람에게 방금 한 작업을 설명하고 싶다면  실제로 k=100이 무엇인지를 계산합니다. 그리고, 몇 % 의 분산이 유지되는 지를 설명합니다. PCA에 익숙한 사람들은 100차원 표현이 원래 데이터에 얼마나 근접한 지를 이해합니다. 왜냐하면 99%의 분산이 유지되기 때문입니다.


   That's really a measure of your square of construction error, that ratio being 0.01, just gives people a good intuitive sense of whether your implementation of PCA is finding a good approximation of your original data set. So hopefully, that gives you an efficient procedure for choosing the number K. For choosing what dimension to reduce your data to, and if you apply PCA to very high dimensional data sets, you know, to like a thousand dimensional data, very often, just because data sets tend to have highly correlated features, this is just a property of most of the data sets you see, you often find that PCA will be able to retain ninety nine per cent of the variance or say, ninety five ninety nine, some high fraction of the variance, even while compressing the data by a very large factor.


   이것이 구성 오차의 제곱에 대한 척도이며 그 비율은 0.01입니다. PCA 구현이 원래 데이터 셋의 좋은 근사치를 찾는지에 대한 감각을 제공합니다. 숫자 k를 선택하는 효율적인 절차입니다. 데이터를 축소할 차원을 선택하고 PCA를 매우 높은 차원의 데이터 셋에 적용하는 경우 수천 차원의 데이터를  선호하는 경우가 많습니다. 데이터 셋이 상관관계가 높은 피처를 갖는 경향이 있기 때문입니다. 대부분의 데이터의 속성입니다. PCA가 99%의 분산을 유지할 수 있다는 것을 종종 발견합니다. 


 

앤드류 응의 머신러닝 동영상 강의



정리하며


   PCA는 차원 축소 문제를 해결하는 가장 인기 있는 알고리즘 중 하나입니다. 수학적으로 데이터가 직교 투영할 때 점과 표면 사이의 거리의 제곱의 합이 최소가 되는 더 낮은 차원의 표면 또는 직선을 찾는 것입니다. PCA는 데이터를 투영할 더 낮은 차원을 찾아서 직교 투영하기 위해  투영 오차의 제곱을 최소화합니다.


   정리하면, PCA는 n차원 데이터를 k차원으로 축소합니다. 여기서, k는 주 성분의 수입니다. 여기서 k의 값을 어떻게 선택해야 할까요?


   "99%의 분산을 유지한다"는 것이 중요합니다. 분산은 데이터가 평균으로부터 얼마나 떨어져 있는 지를 나타는 값들의 제곱의 평균입니다. n차원의 데이터와 k차원의 데이터가 거의 비슷한 분산을 유지할 수 있는 k의 값을 선택합니다. 대부분의 실제 데이터에서 많은 피처가 상관관계가 높기 때문에 데이터를 많이 압축하면서도 99%의 분산 또는 95%의 분산을 유지하는 것이 가능합니다. 


  보통,  k = 1, k = 2, k =3 일 때 분산을 99%를 유지하는 지를 모두 계산해야 하지만 가장 단순하게  계산하는 방법이 있습니다.


       [U, S, D] = svd(Sigma);


  여기서, 공분산 행렬 Sigma에 대해 svd() 함수를 실행하면, 행렬 S를 반환합니다. 행렬 S는 n X n 정방 행렬입니다. 정방 행렬은 대각선 성분 s11, s22, s33,..., snn을 제외한 모든 성분이 0인 행렬입니다. 단지 주어진 k의 값에 대해 훨씬 더 간단하게 계산할 수 있다는 것을 의미합니다. 


   여기서 분자의 Σ는 i = 1부터 k까지의 합산이고, 분모의 Σ는 i = 1부터 n까지 입니다. 요약하면 [U, S, D]를 한 번만 실행하여 행렬 S의 값을 반환받아 k의 가장 작은 값이 되는 k값을 선택합니다. 



문제 풀이


   PCA를 최소화하기 위한 또 다른 방법은?


정답은 3번입니다. 




매거진의 이전글 앤드류 응의 머신러닝(14-5): PCA 데이터 재구성
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari