-
Notifications
You must be signed in to change notification settings - Fork 1
/
naive_bayes.txt
57 lines (47 loc) · 1.95 KB
/
naive_bayes.txt
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
words, labels = load_file(training_file)
training_dic = dict(zip(words, labels))
words, labels = load_file(development_file)
development_dic = dict(zip(words, labels))
features_matrix = np.zeros((len(training_dic), 2))
lab_vec = np.zeros(len(training_dic))
i = 0
for word in training_dic.keys():
features_matrix[i, 0] = len(word)
count = counts[word]
if count == 0:
fixed_word = re.sub(pattern="-", repl="", string = word)
count = counts[fixed_word]
features_matrix[i, 1] = count
lab_vec[i] = training_dic[word]
i += 1
features_matrix[ :, 0] = norm(features_matrix[ :, 0])
features_matrix[ :, 1] = norm(features_matrix[ :, 1])
dev_matrix = np.zeros((len(development_dic), 2))
dev_vec = np.zeros(len(development_dic))
i = 0
for word in development_dic.keys():
dev_matrix[i, 0] = len(word)
count = counts[word]
if count == 0:
fixed_word = re.sub(pattern="-", repl="", string = word)
count = counts[fixed_word]
dev_matrix[i, 1] = count
dev_vec[i] = development_dic[word]
i += 1
dev_matrix[ :, 0] = norm(dev_matrix[ :, 0])
dev_matrix[ :, 1] = norm(dev_matrix[ :, 1])
clf.fit(features_matrix, lab_vec)
train_pred = clf.predict(features_matrix)
dev_pred = clf.predict(dev_matrix)
tprecision = get_precision(train_pred, lab_vec)
trecall = get_recall(train_pred, lab_vec)
tfscore = get_fscore(train_pred, lab_vec)
dprecision = get_precision(dev_pred, dev_vec)
dfscore = get_fscore(dev_pred, dev_vec)
drecall = get_recall(dev_pred, dev_vec)
print("Naive Bayes Performance Statistics")
test_predictions(dev_pred, dev_vec)
print("Training F-Score: " + str(tfscore))
training_performance = [tprecision, trecall, tfscore]
development_performance = [dprecision, drecall, dfscore]
return training_performance, development_performance