-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_bayes_negation_binary_SA.py
216 lines (170 loc) · 6.46 KB
/
naive_bayes_negation_binary_SA.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import numpy as np
import math
import nltk
import pandas as pd
import random
from nltk.corpus import movie_reviews
class NaiveBayesClassifierNegationBinary:
def __init__(self):
self.word_probs = []
self.log_prior = {}
self.pos_words = {}
self.neg_words = {}
self.log_likelihood_pos = {}
self.log_likelihood_neg = {}
def word_count(self, word, pos_neg):
if pos_neg:
if word in self.pos_words:
self.pos_words[word] += 1
else:
self.pos_words[word] = 1
else:
if word in self.neg_words:
self.neg_words[word] += 1
else:
self.neg_words[word] = 1
def calculate_frequency(self, training_data):
# 각 Class 빈도 계산
num_pos = 0
num_neg = 0
exceptions = [',', '.', 't']
for docu in training_data:
docu_word = [{}, {}]
negation = False # Negation Check
pos_neg = (docu[1] == 'pos')
#positive <- True / negative <- False
if docu[1] == 'pos':
num_pos += 1
else:
num_neg += 1
for word in docu[0]:
# if len(word) == 1 and word not in exceptions:
# continue
if word == ',' or word == '.':
negation = False
continue
if word == 'not' or word == 'no' or word == 't':
negation = True
continue
if negation: # Negation 시 not_word
word = 'not_'+ word
if word not in docu_word[pos_neg]:
self.word_count(word, pos_neg)
docu_word[pos_neg][word] = 1
return (num_pos, num_neg)
def log_likelihood(self):
# Log-likelihood 계산
count_all_pos = 0
count_all_neg = 0
for word in self.pos_words:
count_all_pos = count_all_pos + self.pos_words[word] + 1
for word in self.neg_words:
count_all_neg = count_all_neg + self.neg_words[word] + 1
for word in self.pos_words:
self.log_likelihood_pos[word] = math.log2((self.pos_words[word] + 1)
/ count_all_pos)
for word in self.neg_words:
self.log_likelihood_neg[word] = math.log2((self.neg_words[word] + 1)
/ count_all_neg)
self.smoothing_pos = math.log2(1 / count_all_pos)
self.smoothing_neg = math.log2(1 / count_all_neg)
def train(self, training_data):
# Triaing 과정
num_doc = len(training_data)
print('num doc : %d' %(num_doc))
# pos/neg 단어 빈도수 계산
num_pos, num_neg = self.calculate_frequency(training_data)
self.log_prior['pos'] = math.log2(num_pos / num_doc)
self.log_prior['neg'] = math.log2(num_neg / num_doc)
self.log_likelihood()
def predict(self, test_data):
sum_pos = self.log_prior['pos']
sum_neg = self.log_prior['neg']
word_in_docu_pos = {}
word_in_docu_neg = {}
negation = False
# Bag-of-words로 구현
for word in test_data[0]:
if word == 'not' or word == 'no' or word == 't':
negation = True
continue
if negation:
word = 'not_'
if word in self.log_likelihood_pos:
if word in word_in_docu_pos:
continue
else:
sum_pos += self.log_likelihood_pos[word]
word_in_docu_pos[word] = 1
else:
if not word in word_in_docu_pos:
sum_pos += self.smoothing_pos
word_in_docu_pos[word] = 1
if word in self.log_likelihood_neg:
if word in word_in_docu_neg:
continue
else:
sum_neg += self.log_likelihood_neg[word]
word_in_docu_neg[word] = 1
else:
if not word in word_in_docu_neg:
sum_neg += self.smoothing_neg
word_in_docu_neg[word] = 1
if sum_pos > sum_neg:
return 'pos'
else:
return 'neg'
# Accuracy 계산
def accuracy(self, test_data):
data_size = len(test_data)
correct_num = 0
for docu in test_data:
if self.predict(docu) == docu[1]:
correct_num += 1
return float(correct_num / data_size)
def fold_10(set_of_documents):
"""
Simple 10 folds CV
"""
size = len(set_of_documents)
index_interval = int(size / 10)
ten_folds = []
for i in range(10):
start_index = int(i * index_interval)
if i == 9:
ten_folds.append(set_of_documents[start_index:])
else:
ten_folds.append(set_of_documents[start_index:start_index + index_interval])
res = []
for i in range(10):
_test_set = ten_folds[i]
_training_set = []
for j in range(10):
if j == i:
continue
for elem in ten_folds[j]:
_training_set.append(elem)
ith_fold = [_training_set, _test_set]
res.append(ith_fold)
return res
def main():
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
# Random Shuffle with the random seed
random.seed(19941225)
random.shuffle(documents)
accuracy_list = []
iter_ = 0
for training_test in fold_10(documents):
training_data = training_test[0]
test_data = training_test[1]
naive_bayes = NaiveBayesClassifierNegationBinary()
naive_bayes.train(training_data)
accuracy = naive_bayes.accuracy(test_data)
accuracy_list.append(accuracy)
iter_ += 1
print(accuracy_list)
print('average : ', (sum(accuracy_list) / len(accuracy_list)))
if __name__ == '__main__':
main()