class Naive_Bayes:
def __init__(self, path = 'd:\\datadata\\flu.csv'):
import csv
a1 = []
with open(path,'r') as f:
file = csv.reader(f)
for i in file:
a1.append(i)
len_a1 = len(a1)
Ys = []
for i in a1:
Ys.append(i[-1])
Ys = set(Ys[1:])
# 딕셔너리 T - 속에 Y 카테고리에 따라 딕셔너리로 열 별 개수 넣을 예정
T = {}
for i in Ys :
T[i] = []
for _ in range(len(a1[0])):
for key in T.keys():
T[key].append({})
# 열 별 개수 넣기
for i in range(1,len(a1)): # 행
for key,value in T.items():
if a1[i][-1] == key:
for j in range(1,len(a1[0])): # 열
aij = a1[i][j]
if aij not in T[key][j]:
T[key][j][aij] = 1
else :
T[key][j][aij] += 1
# 개수 -> 확률
for key,value in T.items():
for n,i in enumerate(T[key]):
#if
if n < len(a1[0]) -1 :
tmp = 0
for v in i.values():
tmp += v
for k,v in i.items():
i[k] /= tmp
else:
i[key] /= (len(a1)-1)
self.T = T
self.a1 = a1
def get_result(self,test = [1, 'Y','N','Mild','N','_'] ):
# 평가 데이터 넣어서 계산
# p(x1,x2,....xn | Ck)
result = [ [], [], [] ]
for key, value in self.T.items():
result[0].append(key)
tmp = 1
for i in range(1, len(self.a1[0]) -1):
tmp *= self.T[key][i][test[i]]
result[1].append(tmp)
# p(Ck) * p(x1,x2,....xn | Ck)
for i,j in enumerate(result[0]):
result[1][i] *= self.T[j][len(a1[0])-1][j]
# 판정
tmp = 0
for i in range(len(result[1])):
if result[1][i] > tmp :
tmp = i
return result[0][i]
nb = Naive_Bayes()
print('판단 결과 : ', nb.get_result())