-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeanClassifier.py
67 lines (50 loc) · 2.34 KB
/
MeanClassifier.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
#http://danielhnyk.cz/creating-your-own-estimator-scikit-learn/
from sklearn.base import BaseEstimator, ClassifierMixin
class MeanClassifier(BaseEstimator, ClassifierMixin):
"""An example of classifier"""
def __init__(self, intValue=0, stringParam="defaultValue", otherParam=None):
"""
Called when initializing the classifier
"""
self.intValue = intValue
self.stringParam = stringParam
# THIS IS WRONG! Parameters should have same name as attributes
self.differentParam = otherParam
def fit(self, X, y=None):
"""
This should fit classifier. All the "work" should be done here.
Note: assert is not a good choice here and you should rather
use try/except blog with exceptions. This is just for short syntax.
"""
print (len(X))
print (len(y))
assert (type(self.intValue) == int), "intValue parameter must be integer"
assert (type(self.stringParam) == str), "stringValue parameter must be string"
assert (len(X) == 20), "X must be list with numerical values."
self.treshold_ = (sum(X)/len(X)) + self.intValue # mean + intValue
return self
def _meaning(self, x):
# returns True/False according to fitted classifier
# notice underscore on the beginning
return( True if x >= self.treshold_ else False )
def predict(self, X, y=None):
try:
getattr(self, "treshold_")
except AttributeError:
raise RuntimeError("You must train classifer before predicting data!")
return([self._meaning(x) for x in X])
def score(self, X, y=None):
# counts number of values bigger than mean
return(sum(self.predict(X)))
from sklearn.grid_search import GridSearchCV
from sklearn.utils.estimator_checks import check_estimator
check_estimator(MeanClassifier) # passes
trainJZ = [i for i in range(0, 150, 5)]
testJZ = [i + 3 for i in range(-5, 5, 5)]
tuned_params = {"intValue" : [-10,-1,0,1,10]}
gs = GridSearchCV(MeanClassifier(), tuned_params)
# for some reason I have to pass y with same shape
# otherwise gridsearch throws an error. Not sure why.
y=[1 for i in range(20)]
gs.fit(trainJZ, y)
print gs.best_params_ # {'intValue': -10} # and that is what we expect :)