brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 17. 2020

앤드류 응의 머신러닝 (6-1) : 로지스틱 회귀 분류

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


Logistic Regression

로지스틱 회귀


Classification and Representation (분류와 표현) 


Classification (분류)


   In this and the next few videos, I want to start to talk about classification problems, where the variable y that you want to predict is valued. We'll develop an algorithm called logistic regression, which is one of the most popular and most widely used learning algorithms today.


   이번 강의부터 예측하려는 변수 y가 이산 값을 가지는 분류(classification) 문제를 다룹니다. 로지스틱 회귀는 오늘날 가장 널리 쓰이는 학습 알고리즘 중 하나입니다. 시작합니다.  


  


   Here are some examples of classification problems. Earlier we talked about email spam classification as an example of a classification problem. Another example would be classifying online transactions. So if you have a website that sells stuff and if you want to know if a particular transaction is fraudulent or not, whether someone is using a stolen credit card or has stolen the user's password. There's another classification problem. And earlier we also talked about the example of classifying tumors as cancerous, malignant or as benign tumors.  In all of these problems the variable that we're trying to predict is a variable y that we can think of as taking on two values either zero or one, either spam or not spam, fraudulent or not fraudulent, related malignant or benign.


   분류 문제에 대한 사례를 봅시다. 전에 분류 문제의 사례로 스팸 이메일 분류를 이야기했었습니다. 또 다른 사례는 온라인 거래를 분류입니다. 물건을 파는 쇼핑 웹사이트에서 특정 거래가 사기인지 아닌지, 즉 누군가가 도용된 신용카드나 다른 유저의 비밀번호를 사용하고 있는지 아닌 지를 분류합니다. 종양을 악성 또는 양성 종양으로 분류했던 사례도 있습니다. 지금 언급한 모든 사례들은 0 또는 1 둘 중의 하나의 값을 가지는 변수 y를 예측합니다. 스팸인지 아닌지, 사기인지 아닌지, 악성 종양인지 아닌지처럼.



   Another name for the class that we denote with zero is the negative class, and another name for the class that we denote with one is the positive class. So zero we denote as the benign tumor, and one, positive class we denote a malignant tumor. The assignment of the two classes, spam not spam and so on. The assignment of the two classes to positive and negative to zero and one is somewhat arbitrary and it doesn't really matter but often there is this intuition that a negative class is conveying the absence of something like the absence of a malignant tumor. Whereas one the positive class is conveying the presence of something that we may be looking for, but the definition of which is negative and which is positive is somewhat arbitrary and it doesn't matter that much.


   '0'으로 나타내는 클래스(Class)를 네거티브 클래스라 하고 '1'로 표현하는 클래스를 파지티브 클래스(Class)라고 합니다. 따라서, 양성 종양은 0으로 악성 종양은 1로 표기합니다. 스팸인지 아닌지 등은 두 가지 분류 중에 하나를 선택합니다. 네거티브 또는 파지티브, 0 또는 1로 할당하는 것은 다소 임의적입니다. 무엇을 할당하는지는 중요하지 않습니다. 악성 종양이 없는 것처럼 무엇이 없을 때는 네거티브 클래스로 분류합니다. 반면에 악성 종양이 있는 것처럼 우리가 찾는 무엇인가가 있을 때는 파지티브 클래스로 분류합니다. 무엇이 양성인지 음성인지에 관한 정의는 중요하지 않습니다.



   For now we're going to start with classification problems with just two classes zero and one. Later one we'll talk about multi class problems as well where therefore y may take on four values zero, one, two, and three. This is called a multiclass classification problem. But for the next few videos, let's start with the two class or the binary classification problem and we'll worry about the multiclass setting later. 


   우선은 0과 1 두 가지 클래스만을 가지는 분류 문제를 먼저 다루고, 나중에 0, 1, 2, 3 등과 같이 여러 가지 y 값을 찾는 멀티 클래스 문제(multiclass classification) 도 다룰 것입니다. 당분간 이진 분류를 하는 바이너리 클래스를 다루고 나중에 멀티 클래스는 다루겠습니다. 



So how do we develop a classification algorithm? Here's an example of a training set for a classification task for classifying a tumor as malignant or benign. And notice that malignancy takes on only two values, zero or no, one or yes.


   그럼. 분류 알고리즘을 어떻게 개발할까요? 여기 종양을 악성 또는 양성으로 분류하는 학습 데이터 셋이 있습니다. 악성 여부는 두 가지 값만을 가집니다. 0 또는 No, 1 또는 Yes. 




   So one thing we could do given this training set is to apply the algorithm that we already know. Linear regression to this data set and just try to fit the straight line to the data. So if you take this training set and fill a straight line to it, maybe you get a hypothesis that looks like that, right. So that's my hypothesis. H(x) equals theta transpose x. If you want to make predictions one thing you could try doing is then threshold the classifier outputs at 0.5 that is at a vertical axis value 0.5 and if the hypothesis outputs a value that is greater than equal to 0.5 you can take y = 1. If it's less than 0.5 you can take y=0. Let's see what happens if we do that. So 0.5 and so that's where the threshold is and that's using linear regression this way. Everything to the right of this point we will end up predicting as the positive cross. Because the output values is greater than 0.5 on the vertical axis and everything to the left of that point we will end up predicting as a negative value. In this particular example, it looks like linear regression is actually doing something reasonable. Even though this is a classification toss we're interested in.


   학습  데이터 셋에 이미 배운 선형 회귀 알고리즘을 적용합니다. 데이터에 적합한 직선을 그립니다. 학습 데이터 셋에 적합한 직선을 그리면 다음과 같은 가설을 얻을 수 있습니다. 


                 hθ(x) = θ^T * X = θ'X


   다음으로 가설 hθ(x)의 결과값이 0.5가 되는 곳에 수직 임계값 경계선을 긋는 것입니다. hθ(x) >= 0.5 면 y = 1이고,  hθ(x) < 0.5 면 y = 0입니다.  가설을 만들고 실제 적용해 봅니다. 0.5에 대해 임계값을 설정하는 것이 바로 선형 회귀를 사용하는 방법입니다. 이 점을 기준으로 오른쪽에 있는 모든 데이터는 파지티브 클래스로 예측합니다.  왜냐하면 출력 값이 수직축의 0.5보다 크기 때문입니다. 이 점을 기준으로 왼쪽에 있는 모든 데이터는 네거티브 클래스로 예측할 것입니다. 선형 회귀가 잘 작동하는 것처럼 보입니다. 



   But now let's try changing the problem a bit. Let me extend out the horizontal access a little bit and let's say we got one more training example way out there on the right. Notice that that additional training example, this one out here, it doesn't actually change anything, right. Looking at the training set it's pretty clear what a good hypothesis is. Is that well everything to the right of somewhere around here, to the right of this we should predict this positive. Everything to the left we should probably predict as negative because from this training set, it looks like all the tumors larger than a certain value around here are malignant, and all the tumors smaller than that are not malignant, at least for this training set. 


   이제 문제를 약간 바꿔봅시다. 수평축을 조금 더 확장한 후 오른쪽 끝에 학습 데이터를 하나 더 추가합니다. 학습 데이터를 추가한 것 외에는 아무런 변화가 없습니다. 새로운 학습 예제를 보면 좋은 가설이 어떤 것이 분명히 알 수 있습니다. 처음 다루었던 학습 데이터 셋에서 사용한 0.5를 가리키는 점을 기준으로 오른쪽의 데이터는 파지티브이라고 예측하고 왼쪽의 데이터는 네거티브라고 예측합니다. 즉, 특정 값보다 큰 모든 종양들은 악성으로 작은 모든 종양들은 양성으로 보입니다. 적어도 여기 학습 데이터 셋에서는 그렇습니다. 



   But once we've added that extra example over here, if you now run linear regression, you instead get a straight line fit to the data. That might maybe look like this. And if you know threshold hypothesis at 0.5, you end up with a threshold that's around here, so that everything to the right of this point you predict as positive and everything to the left of that point you predict as negative. And this seems a pretty bad thing for linear regression to have done, right, because you know these are our positive examples, these are our negative examples. It's pretty clear we really should be separating the two somewhere around there, but somehow by adding one example way out here to the right, this example really isn't giving us any new information. I mean, there should be no surprise to the learning algorithm. 

That the example way out here turns out to be malignant. But somehow having that example out there caused linear regression to change its straight-line fit to the data from this magenta line out here to this blue line over here, and caused it to give us a worse hypothesis.


   하지만, 새로운 학습 예제 하나를 추가하고 선형 회귀를 실행합니다. 새로운 데이터를 포함하여 데이터 셋에 적합한 새로운 직선을 그립니다. 가설은 파란색 직선을 따를 것이고, 가설의 임계값 0.5는 X축을 기준으로 오른쪽으로 많이 이동합니다. 새로운 임계값 0.5 보다 오른쪽에 있는 모든 데이터를 파지티브라고 예측하고,  왼쪽에 있는 모든 데이터는 네거티브라고 예측합니다. 선형 회귀를 분류 문제에 적용하여 좋지 않은 결과를 얻었습니다. 위쪽에 있는 데이터는 모두 파지티브이고, 아래쪽에 있는 데이터는 모두 네거티브입니다. 따라서, 기존의 분홍색 점에서 나누어야 하지만, 선형 회귀는 오른쪽 끝의 새로운 데이터 하나로 인해 임계값이 변경이 발생합니다. 학습 알고리즘은 새로운 학습 데이터에 적합하게 가설을 변경하는 것은 전혀 놀라운 것이 아닙니다. 하지만, 변경으로 인해 선형 회귀는 더 안 좋은 가설을 만들었습니다. 

       

   So, applying linear regression to a classification problem often isn't a great idea. In the first example, before I added this extra training example, previously linear regression was just getting lucky and it got us a hypothesis that worked well for that particular example, but usually applying linear regression to a data set, you might get lucky but often it isn't a good idea. So I wouldn't use linear regression for classification problems. 


   그러므로, 분류 문제에 선형 회귀는 좋은 알고리즘이 아닙니다. 첫 번째 예는 운 좋게 가설 함수가 잘 작동하였을 뿐입니다. 대부분의 분류 문제에서 잘못된 결과가 발생합니다. 분류 문제에서 선형 회귀를 사용하는 것을 추천하지 않습니다.



   Here's one other funny thing about what would happen if we were to use linear regression for a classification problem. For classification we know that y is either zero or one. But if you are using linear regression where the hypothesis can output values that are much larger than one or less than zero, even if all of your training examples have labels y equals zero or one. And it seems kind of strange that even though we know that the labels should be zero, one it seems kind of strange if the algorithm can output values much larger than one or much smaller than zero.


   선형 회귀가 분류 문제에서 또 다른 문제를 일으킬 수 있습니다. 분류 문제에서 y = 0 또는 y = 1입니다. 그러나 선형 회귀 문제에서 가설 함수 hθ(x) > 1 또는 hθ(x) < 1 수 있습니다. 학습 데이터 셋의 모든 예제는 y의 값이 0 또는 1을 나타내지만, 알고리즘은 이산적이 아닌 연속적인 값입니다. 즉, 알고리즘의 계산 결과는  hθ(x) > 1 또는 hθ(x) < 1의 범위에 있습니다. 이것은 매우 이상합니다. 



    So what we'll do in the next few videos is develop an algorithm called logistic regression, which has the property that the output, the predictions of logistic regression are always between zero and one, and doesn't become bigger than one or become less than zero. And by the way, logistic regression is, and we will use it as a classification algorithm, is some, 

maybe sometimes confusing that the term regression appears in this name even though logistic regression is actually a classification algorithm. But that's just a name it was given for historical reasons. So don't be confused by that logistic regression is actually a classification algorithm that we apply to settings where the label y is discrete value, when it's either zero or one. 


   로지스틱 회귀는 예측값이 항상 0과 1 사이의 값입니다. 1보다 크거나 0보다 작은 값이 될 수 없습니다. 로지스틱 회귀는 분류 문제에 적합합니다. 로지스틱 회귀는 분류 알고리즘이지만 '회귀'라는 단어 때문에 혼동될 수도 있습니다. 이건 역사적인 이유가 있습니다. 로지스틱 회귀는 0이나 1인 이산적 값을 가지는 분류 알고리즘을 뜻합니다.  


   So hopefully you now know why, if you have a classification problem, using linear regression isn't a good idea. In the next video, we'll start working out the details of the logistic regression algorithm.


   이번 강의에서 분류 문제를 해결해야 할 때 선형 회귀를 쓰는 것은 좋지 않다는 것을 배웠습니다. 다음 강의에서 본격적으로 로지스틱 회귀를 공부합니다. 


앤드류 응의 동영상 강의




정리하며


   로지스틱 회귀는 데이터가 어떤 범주에 속하는 지를 분류하는 알고리즘입니다. 참과 거짓과 같이 이분법으로 분류하는 알고리즘은 '0'으로 나타내는 클래스(Class)를 네거티브 클래스라 하고 '1'로 표현하는 클래스를 파지티브 클래스(Class)라고 합니다. 악성 종양이 없는 것처럼 무엇이 없을 때는 네거티브 클래스로 분류합니다. 반면에  악성 종양이 있는 것처럼 우리가 찾는 무엇인가가 있을 때는 파지티브 클래스로 분류합니다. 


   로지스틱 회귀는 지금까지 배운 선형 회귀를 분류 문제를 해결하기에 적합하지 않습니다. 이유는 로지스틱 회귀는 분류 확률을 0과 1 사이의 값으로 예측하고 분류 기준점을 0.5로 잡습니다. 선형 회귀는 데이터가 늘수록 기준점이 계속 바뀌어 의미가 모호합니다. 그래서 새로운 분류 알고리즘이 필요합니다. 


매거진의 이전글 머신러닝 옥타브 실습 (1-1):단변수 선형회귀(상)
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari