brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 08. 2020

앤드류 응의 머신러닝 (4-1) : 여러 개의 피처들

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


Linear Regression with Mutiple Variables

다변수 선형회귀


Multivarate Linear Regression (다변수 선형 회귀) 


Multiple Fearues (다수의 피처들)


   In this video we will start to talk about a new version of linear regression that's more powerful. One that works with multiple variables or with multiple features.


   이번 강의에서 여러 개의 변수나 피처(Features)를 가진 더 강력한 선형 회귀를 설명합니다.  



   Here's what I mean. In the original version of linear regression that we developed, we have a single feature 'x', the size of the house, and we wanted to use that to predict 'y' the price of 

the house and this was our form of our hypothesis. 


   여기 지금까지 공부한 선형 회귀입니다. x는 주택 크기를 나타내는 단일 피처(Feature)이고, y는 실제 주택 가격입니다. 가설 함수 h(x)는 주택 크기에 따른 주택 가격을 예측합니다.  


   But now imagine, what if we had not only the size of the house as a feature or as a variable. of which to try to predict the price, but that we also knew the number of bedrooms, the. number of house and the age of the home (years). It seems like this would give us a lot. more information with which to predict the price.


   그러나 생각해보면 주택 가격을 예측하는 피처는 여러 가지입니다. 주요 피처는 주택 크기, 방의 개수, 층 수, 주택 나이 등입니다. 집값을 더 정확히 예측하기 위한 더 많은 정보가 있습니다.  



   To introduce a little bit of notation, we sort of started to talk about this earlier, I'm going to use the variables X subscript 1 X subscript 2 and so on to denote my, in this case, four features and I'm going to continue to use Y to denote the variable, the output variable price that we're trying to predict. 


   먼저 표기법을 설명합니다. 피처를 나타내는 변수 x에 숫자를 아래 첨자로 사용합니다. x1은 주택 크기, x2는 방의 수, x3는 층 수, x4는 주택 나이입니다. 주택 가격은 y로 표기하고 가설 함수이 예측하는 변수입니다. 



   Let's introduce a little bit more notation. Now that we have four features I'm going to use lowercase "n" to denote the number of features. So in this example we have 'n = 4' because we have, you know, one, two, three, four features. And "n" is different from our earlier notation where we were using "n" to denote the number of examples. So if you have 47 rows "m" is the number of rows on this table or the number of training examples. So I'm also going to use x superscript "i" to denote the input features of the "i" training example. As a concrete example let say x2 is going to be a vector of the features for my second training example. And so x2 here is going to be a vector 1416, 3, 2, 40 since those are my four features that I have to try to predict the price of the second house. So, in this notation, the superscript 2 here. That's an index into my training set. This is not x to the power of 2. Instead, this is, you know, an index that says look at the second row of this table. This refers to my second training example. With this notation X^(2) is a four dimensional vector. In fact, more generally, this is an n-dimensional feature back there. With this notation, X2 is now a vector. And so, I'm going to use also x^(i) subscript j to denote the value of the J, of feature number J and the training example.  So concretely x^(2) subscript 3, will refer to feature number three in the x factor which is equal to 2, right? That was a 3 over there, just fix my handwriting. So x^(2) subscript 3 is going to be equal to 2.


   여기에 테이블에 4 개의 피처(Features)가 있습니다. 피처의 개수를 'n'으로 표기합니다. 예제에서 피처의 개수가 4개이므로 'n= 4'입니다. m은 학습 데이터 셋의 개수입니다. 예제에서 학습 데이터 셋이 47개이므로 'm=47'입니다. 'm'은 학습 데이터 셋의 개수이자 테이블의 행의 개수입니다.  


 

    'x^(i)'는 학습 데이터의 i번째 데이터 셋의 모든 피처의 값을 의미합니다. 예를 들면, x^(2)는  두 번째 훈련용 데이터의 모든 피처의 벡터입니다. 그러면 x^(2) = [1416, 3, 2, 40]입니다. x^(2) 피처 벡터는 두 번째 학습 데이터의 집의 가격을 예측하기 위한 결정 요소입니다. 그래서 윗 첨자 2는 학습 데이터 셋의 순서입니다. 이것은 x의 2승이 아닙니다. 이것은 두 번째 학습 데이터를 참조하는 것입니다.  x^(2)는  4차원 벡터입니다. 일반적으로 학습 데이터는 n차원 피처 벡터입니다.  

 

   'x^(i)j는 학습 데이터의 j 번째 피처의 값을 나타냅니다. 예를 들면, x^(2)3 은 2 번째 학습 데이터의 3번째 피처의 값인 2입니다. x^(2)1은 2번째 학습 데이터의 1번째 피처의 값인 1416입니다. 


      

   Now that we have multiple features, let's talk about what the form of our hypothesis should be. Previously this was the form of our hypothesis, where x was our single feature, but now that we have multiple features, we aren't going to use the simple representation any more. Instead, a form of the hypothesis in linear regression is going to be this, can be theta 0 plus theta 1 x1 plus theta 2 x2 plus theta 3 x3 plus theta 4 X4. And if we have N features then rather than summing up over our four features, we would have a sum over our N features. Concretely for a particular setting of our parameters we may have g of X 80 + 0.1 X1 + 0.01x2 + 3x3 - 2x4. This would be one example of a hypothesis and you remember a hypothesis is trying to predict the price of the house in thousands of dollars, just saying that, you know, the base price of a house is maybe 80,000 plus another open 1, so that's an extra, what, hundred dollars per square feet, yeah, plus the price goes up a little bit for each additional floor that the house has. x2 is the number of floors, and it goes up further for each additional bedroom the house has, because x3 was the number of bedrooms, and the price goes down a little bit with each additional age of the house. With each additional year of the age of the house. Here's the form of a hypothesis rewritten on the slide. And what I'm gonna do is introduce a little bit of notation to simplify this equation.


   가설 함수가 다수의 피처를 어떻게 활용하는 지를 살펴봅니다. 단 하나의 피처를 가진 선형 회귀의 가설 함수 'hθ(x) = θ0 + θ1x'입니다. 다수의 피처를 가진 선형 회귀의 가설 함수를 다음과 같이 생각할 수 있습니다. 


  피처의 개수가 늘어날수록 다항식은 늘어납니다. 가설 함수 hθ(x)에 파라미터 θ의 값을 설정하면 가설 함수는 다음과 같습니다. 

   위의 가설 hθ(x)는 다음과 같이 이해할 수 있습니다. 주택 가격의 단위는 천 달러입니다. 주택 가격의 기본은 $80,000 + @입니다. 아마도 x1 주택 크기는 평방 피트 당 몇 백 달러의 주택 가격을 올릴 것입니다. x2 방의 개수는 약간의 가격을 추가할 것입니다. x3 방의 개수이고 주택 가격을 약간 올릴 것입니다. 반대로 x4 주택 나이는 주택 가격을 떨어트릴 것입니다.  



   For convenience of notation, let me define x subscript 0 to be equals one. Concretely, this means that for every example i I have a feature vector x superscript i and x superscript 

i subscript 0 is going to be equal to 1. You can think of this as defining an additional zero feature. So whereas previously I had n features because x1, x2 through xn, I'm now defining an additional sort of zero feature vector that always takes on the value of one. So now my feature vector x becomes this N+1 dimensional vector that is zero index. So this is now a n+1 dimensional feature vector, but I'm gonna index it from 0 and I'm also going to think of my parameters as a vector. So, our parameters here, right that would be our theta zero, theta one, theta two, and so on all the way up to theta n, we're going to gather them up into a parameter vector written theta 0, theta 1, theta 2, and so on, down to theta n. This is another zero index vector. It's of index signed from zero. That is another n plus 1 dimensional vector. So, my hypothesis cannot be written θ0x0 + θ1x1+ up to θnxn.


   가설 함수를 벡터화 구현을 하기 위해 피처 x0를 추가합니다. 따라서, x^(i)0 = 1입니다. 'x1, x2,..., xn' 까지 있고 x0를 추가적으로 정의하여 피처 벡터를 구현합니다. 결국, 피처 벡터 x는 n+1차원입니다. 

   피처 벡터 x와 파라미터 벡터 θ 는 0-인덱스 벡터입니다. 파라미터 벡터 θ는  θ0, θ1, θ2… θn까지입니다. 이건 다른 형식의 n + 1차원 벡터입니다. 그래서, 이 가설 함수 hθ(x) = θ0x0 + θ1x1 +... + θnxn로 써도 무방합니다.  



   And this equation is the same as this on top because, you know, x0 is equal to one.   

Underneath and I now take this form of the hypothesis and write this as either transpose x, depending on how familiar you are with inner products of vectors if you write what theta transfers, x is what theta transfer and this is theta zero, theta one, up to theta N. So this thing here is theta transpose and this is actually a N plus one by one matrix. [It should be a 1 by (n+1) matrix] It's also called a row vector and you take that and multiply it with the vector X which is x0, x1, and so on, down to xn.   And so, the inner product that is theta transpose x is just equal to this. This gives us a convenient way to write the form of the hypothesis as just the inner product between our parameter vector theta and our theta vector x. And it is this little bit of notation, this little except of the notation convention that let us write this in this compact form. 


   그리고 가설 함수 hθ(x) = θ0x0 + θ1x1 +... + θnxn (x0= 1)입니다. 가설 함수를 벡터화 구현을 하기 위해 피처 벡터 x와 파라미터 벡터 θ를 곱하기 위해서는 하나를 전치 행렬로 만들어야 합니다. 

   파라미터 벡터 θ= [θ0; θ1; θ2;...; θn]의 전치 행렬  Θ^T = [θ0; θ1; θ2;...; θn]입니다. 가설을 두 벡터의 내적으로 표현할 수 있습니다. 가설 hθ(x) = Θ^Tx입니다.  가설은 파라미터 벡터 θ와 피처 벡터 x 간의 내적으로 표현할 수 있습니다. 내적으로 표기한 표기법은 편리하게 사용할 수 있습니다. 


   So that's the form of a hypthesis when we have multiple features. And, just to give this another name, this is also called multivariate linear regression. And the term multivariable that's just maybe a fancy term for saying we have multiple features, or multivariables with which to try to predict the value Y.


   이것이 다수의 피처를 가진 가설 함수의 형식입니다. 이것을 다변수 선형 회귀라고 부르기도 합니다. y 값을 예측하기 위한 여러 개의 피처를 사용할 때 다변수라고 말합니다. 





정리하며 


   지금까지 우리는 단변수 선형 회귀로 가설 함수를 표현했습니다. 예를 들면, 주택 크기만으로 주택 가격을 예측했습니다. 하지만, 세상은 훨씬 더 복잡합니다. 주택 가격을 예측하는 요소는 주택 크기, 방의 개수, 층 수, 주택 나이 등이 있습니다. 여러 개의 Feature가 있는 훈련용 데이터는 다음과 같이 표기합니다.


  


       'x^(i) = i번째 학습 데이터의 모든 피처들의 값, 피처 벡터 

      'x^(i)j = i번째 학습 데이터의 모든 피처들의 값 중 j번째 피처의 값 


선형 회귀의 가설 함수는 다음과 같습니다.  

   

    일변수 함수 기반의 선형 회귀 가설 함수  : hθ(x) = θ0 + θ1x1

    다변수 함수 기반의 선형 회귀 가설 함수 : hθ(x) = θ0x0 + θ1x1 + θ2x2 +...+ θnxn (x0=1)


   다변수 선형 회귀 가설 함수를 행렬로 표현하면 훨씬 이해하기 쉽습니다. 행렬 X와 행렬 θ를 곱하기로 연산하기 위해서는 θ를 전치해야 합니다. 이를 θ^T 또는 θ Transpose로 표시하고 X와 곱합니다. 이를 수학적으로 표현합니다. 

             


문제 풀이    


    아래의 문제를 풀어봅시다. x^(4)1의 값은 무엇일까요?



   정답은 3번입니다. 

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