-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmostFrequent.py
34 lines (30 loc) · 1.17 KB
/
mostFrequent.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
# mostFrequent.py
# ---------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero ([email protected]) and Dan Klein ([email protected]).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
import util
import classificationMethod
class MostFrequentClassifier(classificationMethod.ClassificationMethod):
"""
The MostFrequentClassifier is a very simple classifier: for
every test instance presented to it, the classifier returns
the label that was seen most often in the training data.
"""
def __init__(self, legalLabels):
self.guess = None
self.type = "mostfrequent"
def train(self, data, labels, validationData, validationLabels):
"""
Find the most common label in the training data.
"""
counter = util.Counter()
counter.incrementAll(labels, 1)
self.guess = counter.argMax()
def classify(self, testData):
"""
Classify all test data as the most common label.
"""
return [self.guess for i in testData]