5dc12af8241ef481cd1b0963ddf5fe3540c08e70
[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 #        ss += str(i.coef_)
23 #        ss += '\n'
24     return ss
25
26
27 def predict(index, features):
28     pp = predictors[index]
29     true_class = int(pp.classes_[1] == 1)
30     prob = pp.predict_proba([features])[0, true_class]
31     print prob
32     return prob
33
34
35 if __name__ == '__main__':
36     init('./model.pkl')
37     dump()
38     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]
39     print predict(len(predictors) - 1, feature)
40