这是我能网上所能找到的最易于入门的实例了(代码拷贝于网络)
例子是关于天气案例(天气,温感,湿度,风量,是否正常气候)
通过读取测试集的数据,算出P(C1|X)*P(C1)/P(X),P(C2|X)*P(C2)/P(X),考虑到P(X)相同,省去
具体的是P(C1|X)=P(X1|C1)*P(X2|C1)*P(X3|C1)......P(C1)
同理算出P(C2|X),比较两个值,大的值就是最有可能的结果
#Calculate the Prob. of class:cls 求P("yes"),p("no") 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) 求P(Xn|Cn) 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(): #我记得是key是索引,val是值 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"))