#coding=utf8
class Bayes:
def __init__(self,data,inpu):
self.data=data
self.inpu=inpu
self.lable=[self.data[i][] for i in range(len(self.data))]
self.score,self.result,self.resultM=self.__bayes()
def __bayes(self):
lableL=list(set(self.lable))
#print lableL
numL=[ for i in range(len(lableL))]
for i in range(len(self.lable)):
t=self.__getIndex(lableL,self.lable[i])
numL[t]+=
#print numL
resultM=[]
#print lableL,numL
for i in range(len(lableL)):
#pp 表示類出現的機率
pp=*numL[i]/sum(numL)
pL=[ for j in range(len(self.inpu))]
#print pL
temp=[[] for j in range(len(self.inpu))]
#屬于A類的機率
for k in range(len(self.data)):
if self.data[k][]==lableL[i]:
for q in range(len(self.inpu)):
temp[q].append(self.data[k][][q])
#print temp
for q in range(len(self.inpu)):
num=self.__findCF(temp[q],self.inpu[q])
#print temp[-1],self.inpu[q],num
#使用拉普拉斯平滑
pL[q]=*(+num)/(len(temp[-])+)
#這裡使用lambda表達式
#print pL
ppp=lambda pp,pL: pp*self.__jiec(pL,len(pL))
pf=ppp(pp,pL)
print "aaa"
resultM.append([pf,lableL[i]])
sum1=
for i in range(len(resultM)):
sum1+=resultM[i][]
for i in range(len(resultM)):
resultM[i][]*=/sum1
#pass計算每一類的機率 和 先驗機率
resultM.sort(reverse=True)
return resultM[][],resultM[][],resultM
def __getIndex(self,L,li):
index=
for j in range(len(L)):
if li==L[j]:
index=j
break
return index
def __findCF(self,L,li):
index=
for j in range(len(L)):
if li==L[j]:
index+=
return index
def __jiec(self,lis,n):
#print "aaaa",n
if n==:
return
return lis[n-]*self.__jiec(lis,n-)
data=[[[,],'A'],[[,],'B'],[[,],'C'],[[,],'B'],[[,],'A']]
a=Bayes(data,[,])
print a.result,a.score,a.resultM