-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathXGBModel.py
More file actions
142 lines (98 loc) · 4.15 KB
/
XGBModel.py
File metadata and controls
142 lines (98 loc) · 4.15 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import xgboost as xgb
import json
from pandas import DataFrame, concat
from numpy import unique
from collections import deque
def main():
obj = XGBObject()
class XGBObject():
def __init__(self, parameter_file = "", variables=[], target_names = {}, filename = "" ):
self.variables = variables
self.models = []
try:
if filename: self.load(filename)
elif not parameter_file or not variables:
raise Warning("Warning! Object not defined. Load from file or set 'params' and 'variables'")
else:
with open(parameter_file,"r") as FSO:
params = json.load(FSO)
self.params = params
except Warning as e:
print e
self.params = []
if target_names: self.target_names = target_names
def load(self, filename):
with open(filename + ".dict", 'rb') as FSO:
tmp_dict = json.load(FSO)
print "Loading model from: " + filename
self.__dict__.clear()
self.__dict__.update(tmp_dict)
self.models = []
for model in tmp_dict["models"]:
self.models.append( xgb.Booster({'nthread':8}) )
self.models[-1].load_model(model)
def save(self, filename):
placeholders = []
tmp_models = []
for i,model in enumerate(self.models):
modelname = filename + ".fold{0}".format(i)
model.save_model( modelname )
tmp_models.append(model)
placeholders.append( modelname )
self.models = placeholders
with open(filename + ".dict", 'wb') as FSO:
json.dump(self.__dict__, FSO)
self.models = tmp_models
def train(self, samples):
if type(samples) is list:
samples = deque(samples)
N_classes = len( unique( samples[0]["target"].values ) )
if N_classes > 2:
self.params = self.params["multiclass"]
self.params["num_class"] = N_classes
else:
self.params = self.params["binary"]
for i in xrange( len(samples) ):
test = samples[0]
train = [ samples[1] ]
for j in xrange(2, len(samples) ):
train.append( samples[j] )
train = concat(train , ignore_index=True).reset_index(drop=True)
self.models.append( self.trainSingle( train, test ) )
samples.rotate(-1)
print "Finished training!"
def trainSingle(self, train, test ):
dtrain = xgb.DMatrix( train[self.variables].values,
label=train['target'].values,
missing=-10.0,
weight=train['train_weight'].values )
dtest = xgb.DMatrix( test[self.variables].values,
label=test['target'].values,
missing=-10.0,
weight=test['train_weight'].values )
bst = xgb.train(params = self.params,
dtrain=dtrain,
num_boost_round=self.params["n_estimators"],
verbose_eval=2,
evals = [(dtest,"test")],
early_stopping_rounds = self.params["early_stopping"]
)
print bst.attributes()
return bst
def predict(self, samples, where=""):
predictions = []
if type(samples) is list:
samples = deque(samples)
for i in xrange( len(samples) ):
test = samples[0]
if where: test = test.query( where ).reset_index(drop=True)
predictions.append( self.testSingle( test, i ) )
samples.rotate(-1)
return predictions
def testSingle(self, test, fold):
devents = xgb.DMatrix( test[ self.variables ].values )
prediction = DataFrame( self.models[fold].predict( devents ) )
return DataFrame(dtype = float, data = {"predicted_class":prediction.idxmax(axis=1).values,
"predicted_prob": prediction.max(axis=1).values } )
if __name__ == '__main__':
main()