-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify.py
99 lines (77 loc) · 2.62 KB
/
classify.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
"""
Classify texts containing the word 'mouse' whether they refer to a mouse the
animal or the computer mouse.
Heikal Badrulhisham, 2019 <[email protected]>
"""
import csv
import random
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
def get_data():
"""
Get mouse text data from .csv files. Associate each text with a label
('animal' vs. 'computer')
:return: list of texts, list of labels
"""
data = []
for label in ['animal', 'computer']:
with open(f'datasets/{label}.csv', 'r') as f:
reader = csv.reader(f)
data += [(r[0], label) for r in reader if r][1:]
random.shuffle(data)
texts = [d[0] for d in data]
labels = [d[1] for d in data]
return texts, labels
def get_vectors(texts):
"""
Derive feature vectors from texts, using Term Frequency times inverse
document frequency (TFIDF).
:param texts: list of texts
:return: list of TFIDF vectors
"""
vectorizer = CountVectorizer()
count_vectors = vectorizer.fit_transform(texts)
transformer = TfidfTransformer()
tfidf = transformer.fit_transform(count_vectors)
return tfidf
def run_test(classifier, tfidf, labels):
"""
Get predictions of classifier, display its accuracy.
:param classifier: a Naive Bayes classifier
:param tfidf: list of TFIDF feature vectors
:param labels: list of correct labels to predict
"""
# Make predictions
pred = classifier.predict(tfidf)
num_correct = 0
# Display actual and predicted labels
for i in range(len(pred)):
print(f'Actual: {pred[i]}\tPrediction: {labels[i]}')
# Tally correct predictions
if pred[i] == labels[i]:
num_correct += 1
# Display accuracy
proportion = f'{num_correct}/{len(pred)}'
percentage = round((num_correct/len(pred))*100, 2)
print(f'Accuracy: {proportion}, {percentage}%')
def main():
"""
Get data, derive feature vectors, train a Naive Bayes classifier, test
said classifier.
"""
# Get data
texts, labels = get_data()
# Turn texts into vectors
tfidf = get_vectors(texts)
# Separate training and testing data
split = int(len(texts)*0.8)
train_tfidf, test_tfidf = tfidf[:split], tfidf[split:]
train_labels, test_labels = labels[:split], labels[split:]
# Train Naive Bayes classifier
classifier = MultinomialNB().fit(train_tfidf, train_labels)
# Test classifier
run_test(classifier, test_tfidf, test_labels)
if __name__ == '__main__':
main()
exit(0)