-
Notifications
You must be signed in to change notification settings - Fork 0
/
grade.py
177 lines (164 loc) · 5.74 KB
/
grade.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
import numpy as np
import time
import os
import sys
import reader
import naive_bayes as nb
import naive_bayes_mixture as nbm
from sklearn.metrics import confusion_matrix
import pprint
import argparse
import pickle
import json
import mp2
BIGRAM_PENALTY = 0.25
def bigram_check():
train_set, train_labels, dev_set, dev_labels = reader.load_dataset(
"data/bigram_check/train",
"data/bigram_check/dev",
stemming=False,
lower_case=False,
use_tqdm=False,
)
predicted_labels = nbm.naiveBayesMixture(
train_set,
train_labels,
dev_set,
unigram_smoothing_parameter=1.0,
bigram_smoothing_parameter=1.0,
bigram_lambda=1.0,
pos_prior=0.5,
)
if isinstance(predicted_labels, np.ndarray):
predicted_labels = list(predicted_labels.reshape(-1))
if predicted_labels == [1, 1]:
return BIGRAM_PENALTY
else:
return 1.0
def test_unigram_dev_stem_false_lower_false():
print("Running unigram test..."+'\n')
train_set, train_labels, dev_set, dev_labels = reader.load_dataset(
"data/spam_data/train",
"data/spam_data/dev",
stemming=False,
lower_case=False,
use_tqdm=False
)
predicted_labels = nb.naiveBayes(
train_set, train_labels, dev_set, smoothing_parameter=1.0, pos_prior=0.5)
if len(predicted_labels) != len(dev_labels):
print("The length of the list of predictions is not equivalent to the length of the list of development labels.")
errorDict = {
'name': 'Unigram test on dev set without stemming and without lowercase',
'score': 0,
'max_score': 20,
'visibility': 'visible'
}
return json.dumps(errorDict, indent=1)
(
accuracy,
f1,
precision,
recall,
) = mp2.compute_accuracies(predicted_labels, dev_set, dev_labels)
print("Accuracy:",accuracy)
print("F1-Score:",f1)
print("Precision:",precision)
print("Recall:",recall)
total_score = 0
if accuracy >= 0.81:
total_score += 5
print("+ 5 points for accuracy above " + str(0.81))
else:
print("Accuracy needs to be above " + str(0.81))
if accuracy >= 0.86:
total_score += 5
print("+ 5 points for accuracy above " + str(0.86))
else:
print("Accuracy needs to be above " + str(0.86))
if accuracy >= 0.91:
total_score += 5
print("+ 5 points for accuracy above " + str(0.91))
else:
print("Accuracy needs to be above " + str(0.91))
if accuracy >= 0.95:
total_score += 5
print("+ 5 points for accuracy above " + str(0.95))
else:
print("Accuracy needs to be above " + str(0.95))
resultDict = {
'name': 'Unigram test on dev set without stemming and without lowercase',
'score': total_score,
'max_score': 20,
'visibility': 'visible'
}
return json.dumps(resultDict, indent=1)
def test_bigram_dev_stem_false_lower_false():
print("Running mixture model test..."+'\n')
train_set, train_labels, dev_set, dev_labels = reader.load_dataset(
"data/spam_data/train",
"data/spam_data/dev",
stemming=False,
lower_case=False,
use_tqdm=False
)
predicted_labels = nbm.naiveBayesMixture(train_set, train_labels, dev_set, bigram_lambda=0.05, unigram_smoothing_parameter=1,
bigram_smoothing_parameter=0.005, pos_prior=0.5)
if len(predicted_labels) != len(dev_labels):
print("The length of the list of predictions is not equivalent to the length of the list of development labels.")
errorDict = {
'name': 'Mixture model test on dev set without stemming and without lowercase',
'score': 0,
'max_score': 5,
'visibility': 'visible'
}
return json.dumps(errorDict, indent=1)
(
accuracy,
f1,
precision,
recall
) = mp2.compute_accuracies(predicted_labels, dev_set, dev_labels)
print("Accuracy:",accuracy)
print("F1-Score:",f1)
print("Precision:",precision)
print("Recall:",recall)
total_score = 0
if accuracy >= 0.80:
total_score += 1.25
print("+ 1.25 points for accuracy above " + str(0.80))
else:
print("Accuracy needs to be above " + str(0.80))
if accuracy >= 0.85:
total_score += 1.25
print("+ 1.25 points for accuracy above " + str(0.85))
else:
print("Accuracy needs to be above " + str(0.85))
if accuracy >= 0.90:
total_score += 1.25
print("+ 1.25 points for accuracy above " + str(0.90))
else:
print("Accuracy needs to be above " + str(0.90))
if accuracy >= 0.95:
total_score += 1.25
print("+ 1.25 points for accuracy above " + str(0.95))
else:
print("Accuracy needs to be above " + str(0.95))
if bigram_check() == BIGRAM_PENALTY:
print(f"We hypothesize that your implementation of naiveBayesMixture is not correct. "
f"Therefore, we applied a penalty multiplier of {BIGRAM_PENALTY} to your score.")
total_score *= BIGRAM_PENALTY
resultDict = {
'name': 'Mixture test on dev set without stemming and without lowercase',
'score': total_score,
'max_score': 5,
'visibility': 'visible'
}
return json.dumps(resultDict, indent = 1)
def print_results():
unigram_test = test_unigram_dev_stem_false_lower_false()
print('\n'+unigram_test+'\n')
mixture_test = test_bigram_dev_stem_false_lower_false()
print('\n'+mixture_test+'\n')
if __name__ == '__main__':
print_results()