-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdumbClassifiers.py
112 lines (89 loc) · 2.94 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
108
109
110
111
112
"""
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!
"""
return self.mostFrequentClass
def train(self, X, Y):
'''
just figure out what the most frequent class is and store it in self.mostFrequentClass
'''
self.mostFrequentClass = 1 if Y.sum() >= 0 else -1
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
"""
if X[0] > 0:
return self.classForPos
else:
return self.classForNeg
def train(self, X, Y):
'''
just figure out what the most frequent class is for each value of X[:,0] and store it
'''
positive_sum, negative_sum = 0, 0
for i, v in enumerate(X):
if v[0] > 0:
positive_sum += Y[i]
else:
negative_sum += Y[i]
self.classForPos = 1 if positive_sum >= 0 else -1
self.classForNeg = 1 if negative_sum >= 0 else -1