separate pytwrpper from predictorWrappery
[trackerpp.git] / python / predictor.py
1 from sklearn.linear_model import LogisticRegression
2 try:
3     import cPickle as pickle
4 except:
5     import pickle
6
7
8 predictors = None
9
10 def init(fname = './model.pkl'):
11     global predictors
12     f = open(fname, 'rb')
13     predictors = pickle.load(f)
14     f.close()
15
16 def dump():
17     global predictors
18     ss = '\n'
19     for i in predictors:
20         ss += str(i.__dict__)
21         ss += '\n'
22     return ss
23
24
25 def predict(index, features):
26     pp = predictors[index]
27     true_class = int(pp.classes_[1] == 1)
28     prob = pp.predict_proba([features])[0, true_class]
29     print prob
30     return prob
31
32
33 if __name__ == '__main__':
34     init('./model.pkl')
35     dump()
36     feature=[1,1,1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,1,1,  2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2]
37     print predict(len(predictors) - 1, feature)
38