-
Notifications
You must be signed in to change notification settings - Fork 4
/
dumbClassifiers.py
107 lines (83 loc) · 2.69 KB
/
dumbClassifiers.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
"""
In dumbClassifiers.py, we implement the world's simplest classifiers:
1) Always predict +1
2) Always predict the most frequent label from the training data
3) Just use the sign of the first feature to decide on label
"""
from binary import *
from numpy import *
import util
class AlwaysPredictOne(BinaryClassifier):
"""
This defines the classifier that always predicts +1.
"""
def __init__(self, opts):
"""
do nothing
"""
def online(self):
return False
def __repr__(self):
return "AlwaysPredictOne"
def predict(self, X):
return 1 # return our constant prediction
def train(self, X, Y):
"""
do nothing
"""
class AlwaysPredictMostFrequent(BinaryClassifier):
"""
This defines the classifier that always predicts the
most frequent label from the training data.
"""
def __init__(self, opts):
"""
if we haven't been trained, assume most frequent class is +1
"""
self.mostFrequentClass = 1
def online(self):
return False
def __repr__(self):
return "AlwaysPredictMostFrequent(%d)" % self.mostFrequentClass
def predict(self, X):
"""
X is an vector and we want to make a single prediction: Just
return the most frequent class!
"""
### TODO: YOUR CODE HERE
util.raiseNotDefined()
def train(self, X, Y):
'''
just figure out what the most frequent class is and store it in self.mostFrequentClass
'''
### TODO: YOUR CODE HERE
util.raiseNotDefined()
class FirstFeatureClassifier(BinaryClassifier):
"""
This defines the classifier that always predicts on the basis of
the first feature only. In particular, we maintain two
predictors: one for when the first feature is >0, one for when the
first feature is <= 0.
"""
def __init__(self, opts):
"""
if we haven't been trained, always return 1
"""
self.classForPos = 1 # what class should we return if X[0] > 0
self.classForNeg = 1 # what class should we return if X[0] <= 0
def online(self):
return False
def __repr__(self):
return "FirstFeatureClassifier(%d,%d)" % (self.classForPos, self.classForNeg)
def predict(self, X):
"""
check the first feature and make a classification decision based on it
"""
### TODO: YOUR CODE HERE
util.raiseNotDefined()
def train(self, X, Y):
'''
just figure out what the most frequent class is for each value of X[:,0] and store it
'''
### TODO: YOUR CODE HERE
util.raiseNotDefined()