Naive Bayes Classifier

김민재·2024년 4월 20일
0

ML

목록 보기
4/17

new instance가 들어올때의 상황을 생각해보자. 우리가 vMAPv_{MAP} 을 구하려 할때, instance <a1,a2,,an><a_1, a_2, \cdots,a_n>을 생각하면

vMAP=argmaxvjV  P(vja1,a2,,an)v_{MAP} = \underset{v_j\in V}{\text{argmax}}\;P(v_j|a_1,a_2,\cdots,a_n)

이고, 베이즈 정리에 의해 다음처럼 나타낼 수 있다.

vMAP=argmaxvjVP(a1,a2,,anvj)P(vj)P(a1,a2,,an)=argmaxvjVP(a1,a2,,anvj)P(vj)\begin{aligned} v_{MAP} =\underset{v_j\in V}{\text{argmax}}\frac{P(a_1,a_2,\cdots,a_n|v_j)P(v_j)}{P(a_1,a_2,\cdots,a_n)}\\ = \underset{v_j\in V}{\text{argmax}}P(a_1,a_2,\cdots,a_n|v_j)P(v_j) \end{aligned}

여기서 문제가 발생간다. P(vj)P(v_j)는 모르면 uniform하게 둘수있지만, 앞의 joint prob은 알수없다.
따라서 naive하게 생각을 하는것이다.

Naive Bayes Classifier

Naive하게 아까의 joint prob 계산을 모두 conditionally independent 하다고 놓고 계산을 하는것이다.

P(a1,a2,,anvj)=iP(aivi)\Rightarrow P(a_1,a_2,\cdots,a_n|v_j) = \prod_i P(a_i|v_i)

Definition. Naive Bayes Classifier

Under the naive Bayes classifier, the target value output vNBv_{NB} is determined by
vNB=argmax vjVP(vj)iP(aivj)\qquad \qquad v_{NB} = \underset{v_j\in V}{\text{argmax }}P(v_j)\prod_i P(a_i|v_j)

이렇게 independent일 경우 모든 확률의 곱으로 나타낼 수 있으므로 계산이 가능해진다.

그렇다면 예제를 보자


Example. Play Tennis?


\\
Consider the following novel instance
<outlook=sunny, temperature=cool, humidity=high, wind=strong> 이때, 과연 테니스를 플레이할것인가?

Target value vNBv_{NB}는 다음처럼 구할 수 있다.

vNB=argmax vj{yes,no}P(vj)iP(aivj)=argmax vj{yes,no}P(vj)P(Outlook=sunnyvj)P(Temperature=coolvj)P(Humidity=highvj)P(Wind=strongvj)\begin{aligned} v_{NB} = &\underset{v_j\in\left \{ yes, no \right \}}{\text{argmax }}P(v_j)\prod_i P(a_i|v_j)\\ = &\underset{v_j\in\left \{ yes, no \right \}}{\text{argmax }}P(v_j) \cdot P(\text{Outlook=sunny}|v_j)\cdot P(\text{Temperature=cool}|v_j) \\ \cdot &P(\text{Humidity=high}|v_j) \cdot P(\text{Wind=strong}|v_j) \end{aligned}

그리고 P(vj)P(v_j) 는 이미 데이터를 통해 알고있다.

P(PlayTennis=yes)=914,  P(PlayTennis=no)=514P(\text{PlayTennis=yes}) = \frac{9}{14}, \; P(\text{PlayTennis=no}) = \frac{5}{14}

그렇다면 이제 새로운 instance에 대한 확률을 계산하면 된다.

P(Outlook=sunny  PlayTennis=yes)=29,  P(Outlook=sunny  PlayTennis=no)=35P(\text{Outlook=sunny }| \text{ PlayTennis=yes}) = \frac{2}{9}, \; P(\text{Outlook=sunny }| \text{ PlayTennis=no}) = \frac{3}{5}\\

(나머지 계산은 생략)

이런식으로 모두 확률을 찾은뒤에, 단순히 곱해주기만 하면된다.

P(yes)P(sunnyyes)P(coolyes)P(highyes)P(strongyes)=914292939390.005291P(\text{yes})P(\text{sunny}|\text{yes})P(\text{cool}|\text{yes})P(\text{high}|\text{yes})P(\text{strong}|\text{yes}) = \frac{9}{14}\cdot\frac{2}{9}\cdot\frac{2}{9}\cdot\frac{3}{9}\cdot\frac{3}{9}\\ \qquad\qquad\qquad\qquad\qquad \approx 0.005291
P(no)P(sunnyno)P(coolno)P(highno)P(strongno)=514351545350.020571P(\text{no})P(\text{sunny}|\text{no})P(\text{cool}|\text{no})P(\text{high}|\text{no})P(\text{strong}|\text{no}) = \frac{5}{14}\cdot\frac{3}{5}\cdot\frac{1}{5}\cdot\frac{4}{5}\cdot\frac{3}{5}\\ \qquad\qquad\qquad\qquad\qquad \approx 0.020571

그렇다면 우리는 naive Bayes Classification의 결과가 "PlayTennis=no" 라고 분류할 수 있다.


Laplace Smoothing (Laplacian Correction)

나이브 베이시안 분류기를 사용할때 라플라스 스무딩이라는걸 해줘야한다.

왜냐하면 위에서 vNBv_{NB}의 식이 다음과 같았다.

vNB=argmax vjVP(vj)iP(aivj)v_{NB} = \underset{v_j\in V}{\text{argmax }}P(v_j)\prod_i P(a_i|v_j)

그런데 만약 P(aivj)=0P(a_i|v_j) = 0 이라면? (or 0에 가까운 값) \to 어떤 확률이 존재하던지 0이 나오게 된다.

따라서 이를 방지하고자 다음처럼 smoothing을 진행한다.

P(aivj)=nc+mpn+mP(a_i|v_j) = \frac{n_c + m\cdot p}{n + m}

n:total num of training examplen : \text{total num of training example}
nc:num of aivje.g.    P(Wind=strongPlayTennis=no):n=5,nc=3n_c : \text{num of }a_i | v_j\\ e.g. \;\; P(\text{Wind=strong}|\text{PlayTennis=no}) : n=5, n_c = 3
m:equivalent sample size (m=0 means no laplace smoothing)m : \text{equivalent sample size }(m = 0 \text{ means no laplace smoothing})
p:probabilistic weight to m;normally set to 1k  where k is the num of attribute values, e.g. Wind = 2p : \text{probabilistic weight to }m; \text{normally set to }\frac{1}{k}\\ \quad\;\text{where }k \text{ is the num of attribute values, e.g. Wind = 2}

0개의 댓글