码迷,mamicode.com
首页 > 其他好文 > 详细

朴素贝叶斯(naive bayes)

时间:2017-04-25 23:31:06      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:bsp   weak   ==   main   attr   htm   win   items   www   

#coding=utf-8

#Naive Bayes

#Calculate the Prob. of class:cls
def P(data,cls_val,cls_name="class"):
cnt = 0.0
for e in data:
if e[cls_name] == cls_val:
cnt += 1

return cnt/len(data)

#Calculate the Prob(attr|cls)
def PT(data,cls_val,attr_name,attr_val,cls_name="class"):
cnt1 = 0.0
cnt2 = 0.0
for e in data:
if e[cls_name] == cls_val:
cnt1 += 1
if e[attr_name] == attr_val:
cnt2 += 1

return cnt2/cnt1

#Calculate the NB
def NB(data,test,cls_y,cls_n):
PY = P(data,cls_y)
PN = P(data,cls_n)
for key,val in test.items():
#print (key,val)
PY *= PT(data,cls_y,key,val)
PN *= PT(data,cls_n,key,val)
return {cls_y:PY,cls_n:PN}


if __name__ == "__main__":

#data
data = [
{"outlook":"sunny", "temp":"hot", "humidity":"high", "wind":"weak", "class":"no" },
{"outlook":"sunny", "temp":"hot", "humidity":"high", "wind":"strong", "class":"no" },
{"outlook":"overcast", "temp":"hot", "humidity":"high", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"high", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"cool", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"cool", "humidity":"normal", "wind":"strong", "class":"no" },
{"outlook":"overcast", "temp":"cool", "humidity":"normal", "wind":"strong", "class":"yes" },
{"outlook":"sunny", "temp":"mild", "humidity":"high", "wind":"weak", "class":"no" },
{"outlook":"sunny", "temp":"cool", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"sunny", "temp":"mild", "humidity":"normal", "wind":"strong", "class":"yes" },
{"outlook":"overcast", "temp":"mild", "humidity":"high", "wind":"strong", "class":"yes" },
{"outlook":"overcast", "temp":"hot", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"high", "wind":"strong", "class":"no" },
]

#calculate
print (NB(data,{"outlook":"sunny","temp":"cool","humidity":"high","wind":"strong"},"yes","no"))

原文链接:http://www.cnblogs.com/joyho/articles/3527672.html

 

朴素贝叶斯(naive bayes)

标签:bsp   weak   ==   main   attr   htm   win   items   www   

原文地址:http://www.cnblogs.com/onemorepoint/p/6765080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!