朴素贝叶斯分类器 (Naive Bayes Classifier) python实现

简单实现来自b站大神的视频讲解:https://www.bilibili.com/video/BV1qs411a7mT

详情可以看视频链接,讲的非常好。

 1 # coding=utf-8
 2 from __future__ import division
 3 from numpy import array
 4 
 5 def naive_bs(failed_number, drunk_number, shopping_number, study_number, expected_drunk, expected_shopping, expected_study):
 6 
 7     ex_failed = 0
 8     ex_not_failed = 0
 9 
10     for i in range(0, len(failed_number)):
11         if study_number[i] == 0:
12             ex_failed = ex_failed + 1
13         elif study_number[i] == 1:
14             ex_not_failed = ex_not_failed + 1
15 
16     # 统计p(x?|y)出现的概率
17     # 计算不同情况
18     aa = 0
19     bb = 0
20     for i in range(0, len(drunk_number)):
21         if expected_drunk == drunk_number[i] and failed_number[i] == 1:
22             aa = aa + 1
23         elif expected_drunk == drunk_number[i] and failed_number[i] == 0:
24             bb = bb + 1
25     p_aa = aa / ex_failed #x1|y x1=0,y=1
26     p_bb = bb / ex_not_failed#x1|y x1=0,y=0
27 
28     aaa = 0
29     bbb = 0
30 
31     for i in range(0, len(shopping_number)):
32         if expected_shopping == shopping_number[i] and failed_number[i] == 1:
33             aaa = aaa + 1
34         elif expected_shopping == shopping_number[i] and failed_number[i] == 0:
35             bbb = bbb + 1
36     p_aaa = aaa / ex_failed
37     p_bbb = bbb / ex_not_failed
38 
39     a4 = 0
40     b4 = 0
41     for i in range(0, len(study_number)):
42         if expected_study == study_number[i] and failed_number[i] == 1:
43             a4 = a4 + 1
44         elif expected_study == study_number[i] and failed_number[i] == 0:
45             b4 = b4 + 1
46     p_a4 = a4 / ex_failed
47     p_b4 = b4 / ex_not_failed
48 
49     # print("aa:%s , aaa=%s a4=%s" % ( aa,aaa,a4))
50     # print("p_aa:%s , aaa=%s a4=%s" % (p_aa, p_aaa, p_a4))
51     # print("bb:%s , bbb=%s b4=%s" % (bb, bbb, b4))
52     # print("p_bb:%s , bbb=%s b4=%s" % (p_bb, p_bbb, p_b4))
53     final_failed_p = p_aa * p_aaa* p_a4 * ex_failed / len(failed_number)
54     final_not_p = p_bb * p_bbb * p_b4* ex_not_failed / len(failed_number)
55     print("挂科的概率=%s"%(final_failed_p))
56     print("不挂科的概率=%s"%(final_not_p))
57     if final_not_p>final_not_p:
58         print("预测会挂科")
59     else:
60         print("预测不会挂科")
61 
62 
63 if __name__ == "__main__":
64     failed_number = [1, 0, 0, 1, 1, 0, 0, 1]
65     drunk_number = [1, 0, 1, 1, 0, 0, 0, 0]
66     shopping_number=[1, 0, 0, 0, 1, 1, 1, 0]
67     study_number = [0, 1, 1, 0, 0, 1, 0, 1]
68 
69     expected_drunk = 0
70     expected_shopping = 0
71     expected_study = 1
72     #不喝酒 不逛街 学习 的挂科概率
73     print(drunk_number, study_number, shopping_number)
74     naive_bs(failed_number, drunk_number, shopping_number,study_number,
75              expected_drunk, expected_shopping,expected_study)

代码和自己做的PPT百度云链接:

链接:https://pan.baidu.com/s/1WKn9sY295Qp9JSn3It0yxg
提取码:23cd
复制这段内容后打开百度网盘手机App,操作更方便哦--来自百度网盘超级会员V5的分享

 

上一篇:解决weditor启动后,报错ValueError: not enough values to unpack (expected 2, got 1)


下一篇:assert断言