나이브 베이지안 분류

One of the most useful applications of the Bayes rule is the so-called naive Bayes classifier.

베이지안 규칙의 가장 효과적인 적용 사례는 나이브 베이지안 분류입니다.

The Bayes classifier is a machine learning technique that can be used to classify objects such as text documents into two or more classes. The classifier is trained by analyzing a set of training data, for which the correct classes are given.

베이즈 분류기는 텍스트 문서와 같은 정보를 두 개 이상의 클래스로 분류하는 데 사용할 수 있는 기계학습 기법입니다. 분류기는 정답인 경우가 담겨 있는 교육 데이터 세트를 분석하여 학습하게 됩니다.

The naive Bayes classifier can be used to determine the probabilities of the classes given a number of different observations. The assumption in the model is that the feature variables are conditionally independent given the class (we will not discuss the meaning of conditional independence in this course. For our purposes, it is enough to be able to exploit conditional independence in building the classifier).

나이브 베이즈 분류기는 서로 상이한 관찰 결과가 주어진 클래스 간의 확률을 지정할 수 있습니다. 이 분류기 모델의 가정은 각각 기능 별로 주어진 변수(feature variables)가 주어진 클래스에 따라 서로 조건적이고 또 독립적일 수 있다는 것입니다. (이 과정에서는 조건부 독립성의 의미를 논하지 않을 것입니다. 분류기 구축에 조건부 독립성을 이용할 수 있는 것으로 우리의 목적에 충분합니다)

Real world application: spam filters

실제 사례: 스팸 필터에서 사용해보기

We will use a spam email filter as a running example for illustrating the idea of the naive Bayes classifier. Thus, the class variable indicates whether a message is spam (or “junk email”) or whether it is a legitimate message (also called “ham”). The words in the message correspond to the feature variables, so that the number of feature variables in the model is determined by the length of the message.

우리는 나이브 베이즈 분류기의 작동방식을 이해하기 위해서 스팸메일 필터를 예시로 들어볼 것입니다. 클래스 변수는 이 메시지가 스팸인지 아니면 정상적인 메시지인지 판단할 것입니다. 메시지의 단어는 피쳐 변수(feature variables)에 대응되므로, 모델의 피쳐 변수의 수가 메시지의 길이에 따라 결정됩니다.

<aside> 💡 Note

Why we call it “naive”

Using spam filters as an example, the idea is to think of the words as being produced by choosing one word after the other so that the choice of the word depends only on whether the message is spam or ham. This is a crude simplification of the process because it means that there is no dependency between adjacent words, and the order of the words has no significance. This is in fact why the method is called naive.

</aside>

<aside> 💡 우리가 나이브라고 부르는 이유 스팸 필터를 예시로 들고 있습니다. 메시지가 스팸인지 아닌지 판단하기 위해서 특정 단어의 선택은 다른 단어의 선택에 의해 만들어지는 지 파악해볼 수 있습니다. 단어의 선택만 메시지의 스팸 여부를 판단할 것입니다. 이게 우리가 순진하다고, 즉 나이브하다고 부르는 이유인데요. 인접한 단어 간의 연결성이 없고 단어의 순서가 별로 중요하지 않기 때문에 과정이 조잡하게 단순화된 것입니다. 여기서 말하는 논리는 다음 그림을 통해 묘사되는데, 메시지가 스팸인지 아닌지는 오로지 단어에 의해서만 판단됩니다.

</aside>

Because the model is based on the idea that the words can be processed independently, we can identify specific words that are indicative of either spam ("FREE", "LAST") or ham ("meeting", "algorithm").

Despite its naivete, the naive Bayes method tends to work very well in practice. This is a good example of the common saying in statistics, “all models are wrong, but some are useful” means (the aphorism is generally attributed to statistician George E.P. Box).

이 방법이 비록 순진하다고 하지만, 실제로 매우 잘 작용하는 경향이 있습니다. 이것은 통계학에서 흔히 말하는 "모든 모델은 틀리지만, 일부는 유용하다" 는 의미의 좋은 예시입니다. 통계학자 George E.P. Box 가 주장하기도 하죠.

Estimating parameters

모수의 추정

To get started, we need to specify the prior odds for spam (against ham). For simplicity assume this to be 1:1 which means that on the average half of the incoming messages are spam (in reality, the amount of spam is probably much higher).

먼저 스팸 메시지에 대한 사전 확률을 명시해야 합니다. 단순하게 가정하기 위해서 우리가 받는 메시지의 절반이 스팸이라고 가정해봅시다. (실제로는 스팸 메시지의 수는 더욱 많을 것입니다.)

To get our likelihood ratios, we need two different probabilities for any word occurring: one in spam messages and another one in ham messages.

확률을 얻기 위해서 우리는 두 가지 가능성을 생각해보아야 합니다. 하나는 스팸 메시지일 때이고 다른 하나는 정상적인 메시지일 때이죠.

The word distributions for the two classes are best estimated from actual training data that contains some spam messages as well as legitimate messages. The simplest way is to count how many times each word, abacus, acacia, ..., zurg, appears in the data and divide the number by the total word count.

두 클래스 간의 단어 분포는 실제 트레이닝 데이터일 때 가장 잘 학습됩니다. 이 데이터는 보통 스팸 메시지와 정상적인 메시지를 모두 담고 있죠. 가장 간단한 방법은 얼마나 많은 단어가 데이터에 나타나고 전체 단어 수에서 어느 정도의 비중을 차지하는 지 나누어보는 것입니다.

To illustrate the idea, let’s assume that we have at our disposal some spam and some ham. You can easily obtain such data by saving a batch of your emails in two files.

Assume that we have calculated the number of occurrences of the following words (along with all other words) in the two classes of messages:

예시를 간단하기 생각해보기 위해서 스팸 메시지와 정상적인 메시지를 우리가 다음과 같이 갖고 있다고 생각해봅시다. 하기의 데이터는 이메일 한 묶음에서 쉽게 발견할 수 있습니다. 아래와 같이 단어의 발생빈도를 계산했다고 가정해보죠.