데이터 분석/컴퓨터응용확률

Iris flower identification using Bayesian Classifier - 2

세희홍 2022. 6. 23. 09:50

1.    For four different features, show four separate histograms where each shows three classes in different colors. Find your optimal decision boundaries to minimize the costs. Report your analysis about the result.

 

(1)   기본값 설정

데이터를 그래프화 하기 위해 각 Iris-setosa, Iris-vesicolor, Iris-virginica 세 종류의 클래스를 각각 sepal length, sepal width, petal length, petal width의 빈도 순으로 정리를 하여 분류하였다. 결과적으로 총 12개의 텍스트 파일을 만들었다.

경로를 지정해주고 투명도와 경계를 설정해주었다. 데이터의 단위가 0.1이므로 그래프의 x축의 단위를 0.1로 지정해주었다.

 

 

(2)  Sepal length

# sepal length
import numpy as np
import math

setosa = np.array(
    [4.3, 4.8, 4.9, 4.9, 4.9, 5.2, 4.6, 5, 5.8, 4.4, 4.4, 4.7, 5.5, 4.4, 4.9, 4.6, 5, 5.2, 5.1, 5, 5.5, 4.6, 5, 5.1,
     5.2, 5.3, 5.4, 5, 4.8, 4.7, 4.8, 5.1, 5.4, 4.8, 4.5, 5, 4.8, 4.6, 5.1, 5.1, 5.7, 5.4, 5.4, 5.1, 5.7, 5, 5.4, 5.1,
     5.1, 5])
versicolor = np.array(
    [5, 4.9, 5, 5.7, 5.5, 6, 5.8, 5.1, 5.5, 5.6, 5.8, 5.8, 5.7, 5.5, 6.1, 5.6, 5.5, 5.5, 6.1, 5.7, 5.6, 5.6, 5.7, 6.2,
     6.4, 6.3, 5.7, 6.6, 5.2, 6.6, 6.7, 6.1, 6.1, 7, 6.8, 5.9, 6.2, 6, 5.4, 5.6, 6.4, 6.5, 6.7, 6.3, 6.9, 6, 6.3, 6,
     6.7, 5.9])
virginica = np.array(
    [4.9, 5.6, 5.7, 5.8, 5.8, 5.8, 5.9, 6, 6, 6.1, 6.1, 6.2, 6.2, 6.3, 6.3, 6.3, 6.3, 6.3, 6.3, 6.4, 6.4, 6.4, 6.4, 6.4,
     6.5, 6.5, 6.5, 6.5, 6.7, 6.7, 6.7, 6.7, 6.7, 6.8, 6.8, 6.9, 6.9, 6.9, 7.1, 7.2, 7.2, 7.2, 7.3, 7.4, 7.6, 7.7, 7.7,
     7.7, 7.7, 7.9])

sepal width의 바운더리를 정하기 위해 먼저 데이터를 정리하였다. 파이썬의 numpy라이브러리를 이용하여 setosa, versicolor, virginica 배열을 만들었다.

 

def myrange(start, end, step):
    r = start
    while (r < end):
        yield r
        r += step

미세한 숫자로 바운더리를 결정하기 위해 myrange 함수를 만들었다.

 

smallest = 100
for i in myrange(4.9, 5.8, 0.001):
    sum1 = 0
    sum2 = 0
    error_sum = 0
    for j in range(0, n):
        if i < setosa[j]:
            gap1 = setosa[j] - i
            sum1 += gap1
        if i > versicolor[j]:
            gap2 = i - versicolor[j]
            sum2 += gap2
    error_sum = sum1 + sum2
    if error_sum < smallest:
        smallest = error_sum
        min_i = i
print(smallest, min_i)

Setosa versicolor의 그래프 개형으로 보아 둘의 바운더리는 versicolor의 최솟값(4.9)setosa의 최댓값(5.8) 사이에 있다고 판단할 수 있다. For문을 사용해서 에러의 합을 구했는데 i보다 큰 setosa의 값과, i보다 작은

versicolor의 값을 모두 합해서 그 합이 가장 작은 값을 바운더리로 설정하였다. 이 코드의 결과는 에러를 모두 합한 값이 35.4가 나왔고, 이는 에러가 최소가 되는 바운더리라고 할 수 있다.

 

같은 방법으로 setosa-virginica의 바운더리는 5.7, versicolor-virginica의 바운더리는 6.3이다.

→  Setosa-versicolor: 5.4, setosa-virginica: 5.7, versicolor-virginica: 6.3

 

 

(3) Sepal width

위와 동일한 방법으로 실행하였다

→  Setosa-versicolor: 3, setosa-virginica: 3.1, versicolor-virginica: 2.9

 

 

(4) Petal length

petal length에서 setosa versicolor은 겹치는 구간이 존재하지 않는다. 그러므로 일반화했을 때 에러를 최소화할 수 있는 바운더리의 값은 setosa versicolor 각 평균의 중간값일 것이다. Setosa petal length 평균값은 1.464, versicolor의 평균값은 4.26이므로 (1.464+4.26) / 2 = 2.862이다.

Setosa-virginica의 바운더리도 겹치는 구간이 존재하지 않으므로 위와 동일하게 setosa의 평균 1.464virginica의 평균 5.552의 평균을 구하면 (1.464+5.552) / 2 = 3.508

versicolor-virginica 바운더리는 겹치는 구간이 존재하므로 (2)Sepal length와 동일한 방법으로 구하면 4.9가 바운더리로 나온다.

Setosa-versicolor: 2.862, setosa-virginica: 3.508, versicolor-virginica: 4.9

 

(5)  Petal width

petal width에서 setosa versicolor은 겹치는 구간이 존재하지 않으므로 Setosa petal width 평균값 0.244, versicolor의 평균값 1.326의 평균을 구하면 (0.244 + 1.326) / 2 = 0.785이다.

setosavirginica은 겹치는 구간이 존재하지 않으므로 Setosa petal width 평균값 0.244, versicolor의 평균값 2.026의 평균을 구하면 (0.244 + 2.026) / 2 = 1.135이다.

 versicolor-virginica 바운더리는 겹치는 구간이 존재하므로 (2)Sepal length와 동일한 방법으로 구하면 1.6가 바운더리로 나온다.

 Setosa-versicolor: 0.785, setosa-virginica: 1.135, versicolor-virginica: 1.6