-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_bayes.py
82 lines (74 loc) · 2.73 KB
/
naive_bayes.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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
# CS114 Spring 2020 Programming Assignment 2
# Naive Bayes Classifier and Evaluation
import os
import numpy as np
from collections import defaultdict
class NaiveBayes():
def __init__(self):
# be sure to use the right class_dict for each data set
self.class_dict = {0: 'neg', 1: 'pos'}
#self.class_dict = {0: 'action', 1: 'comedy'}
self.feature_dict = {}
self.prior = None
self.likelihood = None
'''
Trains a multinomial Naive Bayes classifier on a training set.
Specifically, fills in self.prior and self.likelihood such that:
self.prior[class] = log(P(class))
self.likelihood[class][feature] = log(P(feature|class))
'''
def train(self, train_set):
self.feature_dict = self.select_features(train_set)
# iterate over training documents
for root, dirs, files in os.walk(train_set):
for name in files:
with open(os.path.join(root, name)) as f:
# collect class counts and feature counts
pass
# normalize counts to probabilities, and take logs
'''
Tests the classifier on a development or test set.
Returns a dictionary of filenames mapped to their correct and predicted
classes such that:
results[filename]['correct'] = correct class
results[filename]['predicted'] = predicted class
'''
def test(self, dev_set):
results = defaultdict(dict)
# iterate over testing documents
for root, dirs, files in os.walk(dev_set):
for name in files:
with open(os.path.join(root, name)) as f:
# create feature vectors for each document
pass
# get most likely class
return results
'''
Given results, calculates the following:
Precision, Recall, F1 for each class
Accuracy overall
Also, prints evaluation metrics in readable format.
'''
def evaluate(self, results):
# you may find this helpful
confusion_matrix = np.zeros((len(self.class_dict),len(self.class_dict)))
pass
'''
Performs feature selection.
Returns a dictionary of features.
'''
def select_features(self, train_set):
# almost any method of feature selection is fine here
return {0: 'fast', 1: 'couple', 2: 'shoot', 3: 'fly'}
if __name__ == '__main__':
nb = NaiveBayes()
# make sure these point to the right directories
nb.train('movie_reviews/train')
#nb.train('movie_reviews_small/train')
results = nb.test('movie_reviews/dev')
#results = nb.test('movie_reviews_small/test')
nb.evaluate(results)