Navie Bayes 방법론

Assignment

  • 중간 중간 assignment라고 되어있고 ''' ? ''' 이 있을거에요 그 부분을 채워주시면 됩니다.

우수과제 선정이유

나이브베이즈 관련 함수들의 인자와 반환값 형태 및 성질들을 꼼꼼히 살펴보았으며 군더더기없이 과제를 수행하였습니다.

Assignment 1 : Gaussian Naive Bayes Classification 해보기

  • sklearn에 Gaussian Naive Bayes Classification 클래스 함수가 이미 있습니다

  • 그것을 활용하여 간단하게 예측만 하시면 됩니다

  • 필요 함수 링크를 주석으로 처리하여 첨부했으니 보시고 사용해주세요

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
  • sklearn에 내장되어있는 붓꽃 데이터를 사용할 겁니다

iris = load_iris()
  • 붓꽃데이터를 불러옵니다

print(iris.DESCR)
_iris_dataset:

Iris plants dataset
--------------------

**Data Set Characteristics:**

    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
                
    :Summary Statistics:

    ============== ==== ==== ======= ===== ====================
                    Min  Max   Mean    SD   Class Correlation
    ============== ==== ==== ======= ===== ====================
    sepal length:   4.3  7.9   5.84   0.83    0.7826
    sepal width:    2.0  4.4   3.05   0.43   -0.4194
    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)
    ============== ==== ==== ======= ===== ====================

    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

.. topic:: References

   - Fisher, R.A. "The use of multiple measurements in taxonomic problems"
     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
     Mathematical Statistics" (John Wiley, NY, 1950).
   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
     Structure and Classification Rule for Recognition in Partially Exposed
     Environments".  IEEE Transactions on Pattern Analysis and Machine
     Intelligence, Vol. PAMI-2, No. 1, 67-71.
   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
     on Information Theory, May 1972, 431-433.
   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
     conceptual clustering system finds 3 classes in the data.
   - Many, many more ...
  • 설명변수 x가 꽃받침 길이, 꽃받침 폭 , 꽃잎 길이 , 꽃잎 폭 임을 알 수 있습니다

  • sepal length , sepal width , petal length , petal width

  • 타겟변수 y는 붓꽃의 품종으로 총 3가지의 종류가 있는 걸 알 수 있습니다

  • Iris-Setosa , Iris-Versicolour , Iris-Virginica 가 0 1 2 로 분류되어 있습니다

  • 설명변수 x와 타겟변수 y를 판다스의 데이터프레임 형태로 만듭니다.

0

1

2

3

0

5.1

3.5

1.4

0.2

1

4.9

3.0

1.4

0.2

2

4.7

3.2

1.3

0.2

3

4.6

3.1

1.5

0.2

4

5.0

3.6

1.4

0.2

...

...

...

...

...

145

6.7

3.0

5.2

2.3

146

6.3

2.5

5.0

1.9

147

6.5

3.0

5.2

2.0

148

6.2

3.4

5.4

2.3

149

5.9

3.0

5.1

1.8

0

0

0

1

0

2

0

3

0

4

0

...

...

145

2

146

2

147

2

148

2

149

2

  • 가우시안 나이브 베이즈 함수를 불러옵니다

1-1) assignment

1-2) assignment

image.png

Assignment 2 : Naive Bayes Classification 해보기

  • 제가 임의로 만든 데이터 셋입니다

  • spam 메세지에 gamble money hi라는 단어의 유무를 기준으로 0과 1을 주었고 spam 메세지인지 아닌지를 spam에 0과1로 정해주었습니다

  • 설명변수는 gamble, money, spam 이고 종속변수는 spam입니다(data가 세개~ sam일 확률 구하자룽)

  • 해당 딕셔너리 데이터를 판다스 데이터 프레임으로 변경하여줍니다

gamble

money

hi

spam

0

1

1

0

1

1

0

1

1

0

2

1

1

0

1

3

0

0

1

0

4

1

1

0

1

...

...

...

...

...

95

1

1

0

1

96

1

0

0

1

97

0

0

1

0

98

1

1

0

1

99

1

0

0

1

2-1) assignment

2-2) assignment

  • P(spam=1), P(spam=0)

  • P(gamble=1|spam=1), P(money=1|spam=1), P(hi=1|spam=1)

  • P(gamble=1|spam=0), P(money=1|spam=0), P(hi=1|spam=0)

  • 위의 확률들을 구하여 주세요

  • 먼저 P(spam)의 확률 부터 구해주세요

  • P(spam=1)인 경우만 제가 완성을 해두었습니다 참고하시면 금방 금방 채우실거에요 :)

  • gamble , money , hi의 조건부 확률을 다 구해주세요

  • P(gamble=0|spam=1) = 1 - P(gamble=1|spam=1) 의 형태로 구하면 되므로 굳이 따로 구하지 않습니다

  • 각 값이 어떤 조건부 확률인지 이름만으로는 알기 어려울거같아 바로 옆에 주석을 달아놨습니다 참고하세요

  • 위랑 마찮가지로 제일 위에 껀 제가 해놨어요 참고해서 한번 구해보세요

  • 이제 P(|spam=1)값 리스트와 P(|spam=0)값 리스트를 생성해줍니다

  • 요건 테스트 셋이에요

  • 예를 들어 [0,1,0]인 경우 gamble=0,money=1,hi=0인 경우에 spam인지 아닌지 확률을 계산해 달라는 의미 입니다

  • 설명변수가 3개 밖에 안되기때문에 [0,0,0] ~ [1,1,1] 8가지 모든 경우에 대해 확률 P(*|spam=1)를 구할 거에요

2-3) assignment

  • 조건부 확률을 구하는 함수를 구해주세요

  • x는 해당 독립변수가 0인지 1인지를 받는 인자이구요

  • p는 해당독립변수가 1일때의 조건부 확률이 들어갑니다

  • P(X=x|Y=1) = xP(X=1|Y=1)+(1-x)P(X=0|Y=1)을 응용하세요

  • test경우에 대해 각 확률을 반환해주는 함수를 생성해주세요

  • 결과 입니다 다음과 같은 값들이 똑같이 나오면 과제 성공이에요

  • 왼쪽이 spam 메세지일 확률, 오른쪽이 spam 메세지가 아닐 확률입니다

  • gamble money hi라는 단어가 들어가면 들어갈수록 spam메세지인걸 알수가 있네요

  • spam 메세지일 확률이 0.5를 넘기는 아래에서 6,7,8행의 경우가 spam 메세지로 분류가 되겠네요

  • 즉 gamble이라는 단어와 money 혹은 hi라는 단어가 하나라도 같이 있으면 spam메세지가 되나봐요.

성공!

Last updated

Was this helpful?