brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 11. 2020

앤드류 응의 머신러닝 (4-5):피처와 다항 회귀

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


Linear Regression with Mutiple Variables

다변수 선형회귀


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


Features and Polynomial Regression  (피처와 다항 회귀)


   You now know about linear regression with multiple variables. In this video, I wanna tell 

you a bit about the choice of features that you have and how you can get different learning 

algorithm, sometimes very powerful ones by choosing appropriate features. And in particular I also want to tell you about polynomial regression allows you to use the machinery of linear regression to fit very complicated, even very non-linear functions. 


   지금까지 다변수 선형 회귀를 배웠습니다. 이번 강의에서 피처(Feature)를 선택하는 법과 학습 알고리즘을 결정하는 법을 설명합니다. 적절한 피처(Feature)를 사용하는 학습 알고리즘은 매우 강력합니다. 매우 복잡한 선형 함수나 비선형 함수에 적합한 선형 회귀 메커니즘을 사용할 수 있도록 다항 회귀도 설명합니다. 




   Let's take the example of predicting the price of the house. Suppose you have two features, 

the frontage of house and the depth of the house. So, here's the picture of the house we're trying to sell. 


   여기에 주택 가격을 예측하는 가설과 주택의 모양이 있습니다. 주택 정면(Frontage)의 크기를 나타내는 데이터와 주택의 세로 길이를 나타내는 데이터(Depth)가 있습니다.    



   So, the. frontage is defined as this distance is basically the width or the length of how wide your lot is if this that you own, and the depth of the house is how deep your property is, so there's a frontage, there's a depth. called frontage and depth. You might build a linear regression model like this where frontage is your first feature x1 and and depth is your second feature x2, but when you're applying linear regression, you don't necessarily have to use just the features x1 and x2 that you're given. What you can do is actually create new features by yourself.  So, if I want to predict the price of a house, what I might do instead is decide that what really determines the size of the house is the area or the land area that I own. So, I might create a new feature. I'm just gonna call this feature x which is frontage, times depth. This is a multiplication symbol. It's a frontage x depth because this is the land area that I own and I might then select my hypothesis as that using just one feature which is my land area, right? Because the area of a rectangle is you know, the product of the length of the size So, depending on what insight you might have into a particular problem, rather than just taking the features [xx] that we happen to have started off with, sometimes by defining new features you might actually get a better model. 


   주택의 가로 길이(Frontage)와 주택의 세로 길이(Depth)를 이용하여 선형 회귀 모델을 만들 수 있습니다. 피처 x1은 주택의 가로 길이(Frontage)이고, 피처 x2는 주택의 세로 길이(Depth)입니다. 선형 회귀 가설을 적용할 때 반드시 피처를 그대로 사용할 필요가 없습니다. 새로운 피처를 만들기도 합니다. 주택 가격을 예측할 때 주택 크기는 주택의 면적이나 땅의 크기입니다. 사각형의 넓이는 '가로 X 세로'이므로 새로운 피처는 'Frontage(폭) X  Depth(길이)'로 주택 크기입니다. 두 개의 피처를 가진 가설을 한 개의 피처를 가진 가설로 바꿉니다.  즉, 문제에 따라 기존 Feature를 쓰는 것보다 새로운 Feature를 만드는 것이 더 효과적일 수 있습니다.   



   Closely related to the idea of choosing your features is this idea called polynomial regression. Let's say you have a housing price data set that looks like this. 



   다항 회귀는 피처를 선택하는 것과 매우 밀접하게 관련이 있습니다. 여기 주택 가격의 데이터 셋이 있습니다. 



   Then there are a few different models you might fit to this. One thing you could do is fit a quadratic model like this. It doesn't look like a straight line fits this data very well. So maybe you want to fit a quadratic model like this where you think the size, where you think the price is a quadratic function and maybe that'll give you, you know, a fit to the data that looks like that. 


   데이터 셋에 적합한 몇 가지 선형 모델들을 생각할 수 있습니다. 그중에 하나는 2차 함수의 곡선을 그릴 수 있습니다. 직선은 이 데이터 셋에 충분히 적합하지 않을 것 같습니다. 2차 함수의 곡선을 그릴 수 있는 선형 회귀 가설은 다음과 같습니다.

   이 가설은 주택 크기에 따른 주택 가격의 변화를 예측합니다. 


 


   But then you may decide that your quadratic model doesn't make sense because of a quadratic function, eventually this function comes back down and well, we don't think housing prices should go down when the size goes up too high. So then maybe we might choose a different polynomial model and choose to use instead a cubic function, and where we have now a third-order term and we fit that, maybe we get this sort of model, and maybe the green line is a somewhat better fit to the data cause it doesn't eventually come back down. So how do we actually fit a model like this to our data? 


   이차 함수 모델의 그래프는 주택 크기가 일정 크기를 넘어가면 주택 가격이 하락합니다. 실제 세계에서 주택 크기가 클수록 주택 가격이 하락할 수 없습니다. 데이터 셋에 좀 더 적합한 가설을 찾아보겠습니다. 2차 함수가 아닌 3차 함수를 사용합니다. 3차 함수는 주택 크기가 클수록 지속적으로 주택 가격이 증가하는 그래프를 훨씬 더 잘 표현합니다. 그럼 어떻게 3차 함수의 다항식을 만들까요?  



   Using the machinery of multivariant linear regression, we can do this with a pretty simple modification to our algorithm. The form of the hypothesis we, we know how the fit looks like this, where we say h of x is theta zero plus theta one x one plus x two theta X3. And if we want to fit this cubic model that I have boxed in green, what we're saying is that to predict the price of a house, it's theta 0 plus theta 1 times the size of the house plus theta 2 times the square size of the house. So this term is equal to that term. And then plus theta 3 times the cube of the size of the house raises that third term. In order to map these  two definitions to each other, well, the natural way to do that is to set the first feature x one to be the size of the house, and set the second feature x two to be the square of the size of the house, and set the third feature x three to be the cube of the size of the house. And, just by choosing my three features this way and applying the machinery of linear regression, I can fit this model and end up with a cubic fit to my data. 


   학습 알고리즘 가설을 다변수 선형 회귀처럼 변형합니다. 여기에 가설이 있습니다. 


    가설 hθ(x)는 주택 크기 x1을 활용하여 삼차 다항식으로 표현합니다. 피처 x1은 주택의 크기이고, 피처 x2는 주택 크기의 제곱이고, 피처 x3는 주택 크기의 세제곱입니다. 단 하나의 피처를 활용하여 마치 3개의 피처가 있는 것처럼 다항식 선형 회귀로 표현할 수 있습니다.  


   I just want to point out one more thing, which is that if you choose your features like this, then feature scaling becomes increasingly important. So if the size of the house ranges from one to a thousand, so, you know, from one to a thousand square feet, say, then the size squared of the house will range from one to one million, the square of a thousand, and your third feature x cubed, excuse me you, your third feature x three, which is the size cubed of the house, will range from one two ten to the nine, and so these three features take on very different ranges of values, and it's important to apply feature scaling if you're using gradient descent to get them into comparable ranges of values.



   피처를 활용하여 그래프의 모양을 바꿀 때 피처 스케일링(Feature scaling)은 매우 중요합니다. 예를 들어, 주택 크기를 나타내는 피처 x1의 값의 범위가 1에서 1,000 평방피트 사이의 값을 가진다면, 주택 크기의 제곱을 나타내는 피처 x2는 1에서 백만 평방피트 사이의 값을 가지고, 주택 크기의 세제곱을 나타내는 피처 x3는 1에서 10^9 평방 피트 사이의 값을 가집니다. 세 피처는 매우 다른 범위의 값을 가지기 때문에 피처 스케일링은 매우 중요합니다. 왜냐하면 여러 피처가 비슷한 값의 범위를 가져야 비용 함수 J(θ)의 최소값을 구하는 경사 하강법이 제대로 동작하기 때문입니다. 

 



   Finally, here's one last example of how you really have broad choices in the features you use. Earlier we talked about how a quadratic model like this might not be ideal because, you know, maybe a quadratic model fits the data okay, but the quadratic function goes back down and we really don't want, right, housing prices that go down, to predict that, as the size of housing freezes. But rather than going to a cubic model there, you have, maybe, other choices of features and there are many possible choices. But just to give you another example of a reasonable choice, another reasonable choice might be to say that the price of a house is theta zero plus theta one times the size, and then plus theta two times the square root of the size, right? So the square root function is this sort of function, and maybe there will be some value of theta one, theta two, theta three, that will let you take this model and, for the curve that looks like that, and, you know, goes up, but sort of flattens out a bit and doesn't ever come back down. And, so, by having insight into, in this case, the shape of a square root function, and, into the shape of the data, by choosing different features, you can sometimes get better models.


      마지막으로 데이터 셋에 적합한 또 다른 다항식 가설이 있습니다. 2차 다항식 모델은 기존 데이터 셋에 적합하지만 주택 크기가 클수록 주택 가격이 하락하는 것이 문제입니다. 3차 다항식 모델은 기존 데이터 셋에 적합하지만 주택 크기가 클수록 주택 가격이 급격히 상승하는 것이 문제입니다. 여기 좀 더 괜찮은 다항식 모델이 있습니다.



   여기 제급근 함수는 분홍색 곡선입니다. 곡선은 완만한 곡선을 그리다가 주택 크기가 클수록 주택 가격은 완만히 상승하지만 절대로 낮아지지 않습니다. 피처를 활용하여 데이터 셋의 모양에 맞는 더 나은 모델을 만들 수 있습니다. 

 

    In this video, we talked about polynomial regression. That is, how to fit a polynomial, like a quadratic function, or a cubic function, to your data. Was also throw out this idea, that you have a choice in what features to use, such as that instead of using the frontish and the depth of the house, maybe, you can multiply them together to get a feature that captures the land area of a house. In case this seems a little bit bewildering, that with all these different feature choices, so how do I decide what features to use. Later in this class, we'll talk about some algorithms were automatically choosing what features are used, so you can have an algorithm look at the data and automatically choose for you whether you want to fit a quadratic function, or a cubic function, or something else. But, until we get to those algorithms now I just want you to be aware that you have a choice in what features to use, and by designing different features you can fit more complex functions your data then just fitting a straight line to the data and in particular you can put polynomial functions as well and sometimes by appropriate insight into the feature simply get a much better model for your data.


   이 번 강의에서 다항 회귀를 배웠습니다. 어떻게 2차 함수나 3차 함수와 같은 다항식을 데이터 셋에 적합한 모델로 만드는 지를 배웠습니다. 그리고 피처를 변형하는 방법도 배웠습니다. 예를 들면, 주택의 가로 길이(Frontage)와 세로 길이(Depth)를 각각 사용하는 것보다 둘을 곱하여 새로운 피처 주택 크기 또는 면적(Area)을 사용하는 것이 효과적이었습니다. 


   여러 개의 피처가 있을 때, 어떻게 합쳐야 할지 혼란스러울 때가 있습니다. 이 과정 후반에는 자동으로 어떤 피처를 사용할지를 골라주는 알고리즘에 대해 배울 것입니다. 그 알고리즘은 데이터를 보고 자동으로 2차 함수나 3차 함수, 혹은 다른 함수들 중 알맞은 것을 선택합니다. 하지만, 이 알고리즘을 배우기 전에 피처를 활용하는 다양한 방법을 알아야 합니다. 데이터에 적합한 새로운 피처를 고안하고 적합한 가설을 찾을 수 있습니다. 데이터에 적합한 가설을 단순한 직선이 아닌 복잡한 다항 함수를 활용할 수 있습니다.


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



정리하며 


   문제에 따라 기존의 피처를 사용하는 것보다 새로운 피처를 만드는 것도 좋습니다. 예를 들면, 주택의 가로 길이를 나타내는 피처 x1과 세로 길이를 나타내는 피처 x2를 각각 사용하는 것보다 피처 x1과 x2를 곱한 주택 크기나 주택 면적을 나타내는 새로운 피처를 만드는 것이 효과적입니다.


   학습 데이터 셋의 모양이 직선일 때는 1차 함수 그래프를, 선일 때 2 차 함수 그래프를 또는 복잡한 모양일 때는 3차 함수 그래프 나 제곱근 함수 그래프를 사용합니다.  


다음은 3차 함수 그래프입니다. 



   1차 함수 다항 회귀 : h(x) = θ0 + θ1x1 + θ2x2 + θ3x3입니다. 

   2차 함수 다항 회귀 : h(x) = θ0 + θ1*(집의 크기) + θ2*(집의 크기)^2 

   3차 함수 다항 회귀 : h(x) = θ0 + θ1*(집의 크기) + θ2*(집의 크기)^2 + θ3*(집의 크기)^3

   제곱근 함수 다항 회귀 : h(x) = θ0 + θ1 * (집의 크기) + θ2*(집의 크기의 제곱근) 





   피처를 다양하게 변형할 때는 피처 스케일링이 매우 중요합니다. 여러 피처가 비슷한 값의 범위를 가져야 비용 함수 J(θ)의 최소값을 구하는 경사 하강법이 제대로 동작하기 때문입니다. 


문제 풀이


   가설 함수를 제곱근 함수를 이용한다고 가정합니다. 이때 Feature 집의 크기의 범위는 1부터 1,000까지입니다. 정규화 없이 피처 스케일링을 한다고 가정할 때, x1과 x2의 값은 무엇일까요? 



정답은 3번입니다. 정규화를 하지 않으므로 단순히 '데이터 / 최대값'입니다. 

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