-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaive_bayes.py
More file actions
104 lines (81 loc) · 3.19 KB
/
Copy pathnaive_bayes.py
File metadata and controls
104 lines (81 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import math
def train(X, y):
'''
This function trains a Naive Bayes classifier.
Parameters:
-----------
X: The matrix in which the ith row corresponds to the ith input vector of ith training
example (each training example contains input vector and correct output); its shape
is (<# training examples>, <# dimensions of input space>).
y: The vector (actually the matrix whose shape is (<# training examples>, 1)) in which
the ith element corresponds to the ith correct output of ith training example.
Returns:
--------
(Here I just describe the shapes of returned variables. Please read the pdf file for the
meaning of these variables.)
phi: a scalar.
phi0: a vector whose shape is (<# dimensions of input space>,).
phi1: a vector whose shape is (<# dimensions of input space>,).
'''
# TODO
# Cal phi
phi = 0.0
vecLabelMail = y[:, 0]
arrSpam = [1 for x in vecLabelMail if x == 1]
phi = len(arrSpam)*1.0/len(vecLabelMail)
# Cal phi0 and phi1
phi0 = []
phi1 = []
for vecMaili in X.T:
arrNumeratorPhi0 = [1]
arrNumeratorPhi1 = [1]
arrNumeratorPhi0 = [1 for m, l in zip(vecMaili, vecLabelMail) if m == 1 and l == 0]
arrNumeratorPhi1 = [1 for m, l in zip(vecMaili, vecLabelMail) if m == 1 and l == 1]
denominatorPhi0 = 2.0 + len(vecLabelMail) - len(arrSpam)
denominatorPhi1 = 2.0 + len(arrSpam)
phi0.append(((len(arrNumeratorPhi0) + 1)/(denominatorPhi0)))
phi1.append(((len(arrNumeratorPhi1) + 1)/(denominatorPhi1)))
return (phi, phi0, phi1)
def predict_spam(phi, phi0, phi1, X_test):
'''
This function predicts whether an email is spam using the trained Naive Bayes classifier.
Parameters:
-----------
phi, phi0, phi1: Parameters of the trained Naive Bayes classifier.
X_test: The matrix in which the ith row corresponds to the ith test input vector.
Returns:
--------
A vector in which the ith element corresponds to the prediction of ith test input vector.
'''
# TODO
x_label = []
for vecVocaOfMail in X_test[:,]:
label1 = 0.0
label0 = 0.0
for m, p1, p0 in zip(vecVocaOfMail, phi1, phi0):
label1 = label1 + math.log10(m*p1 + (1-m)*(1-p1))
label0 = label0 + math.log10(m*p0 + (1-m)*(1-p0))
label1 = label1 + math.log10(phi)
label0 = label0 + math.log10(1 - phi)
x_label.append(0 if label1 < label0 else 1)
return x_label
def test(phi, phi0, phi1, X_test, y_test):
'''
This function tests the trained Naive Bayes classifier.
Parameters:
-----------
phi, phi0, phi1: Parameters of the trained Naive Bayes classifier.
X_test: The matrix in which the ith row corresponds to the ith input vector of ith test
example (each test example contains input vector and correct output); its shape
is (<# test examples>, <# dimensions of input space>).
y_test: The vector (actually the matrix whose shape is (<# test examples>, 1)) in which
ith element corresponds to the ith correct output of ith test example.
Returns:
--------
The accuracy (in [0, 1]) of the trained Naive Bayes classifier on the test data.
'''
# TODO
y_label = predict_spam(phi, phi0, phi1, X_test)
countTrue = [1 for labelGuess, labelTest in zip(y_label, y_test) if labelGuess == labelTest]
return len(countTrue)*1.0/len(y_test)