11 print(
"Please install xgboost: pip3 install xgboost")
24 def __init__(self, num_round=0, parameters=None):
25 """ Constructor of the state object """
34 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
36 Return default xgboost model
38 param = {
'bst:max_depth': 2,
'bst:eta': 1,
'silent': 1,
'objective':
'binary:logistic'}
40 if 'nTrees' in parameters:
41 nTrees = parameters[
'nTrees']
42 del parameters[
'nTrees']
43 if isinstance(parameters, collections.Mapping):
44 param.update(parameters)
45 return State(nTrees, param)
48 def feature_importance(state):
50 Return a list containing the feature importances
57 Load XGBoost estimator into state
60 f = tempfile.NamedTemporaryFile(delete=
False)
63 state.estimator = xgb.Booster({})
64 state.estimator.load_model(f.name)
71 Apply estimator to passed data.
74 result = state.estimator.predict(data)
75 return np.require(result, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
78 def begin_fit(state, Xtest, Stest, ytest, wtest):
80 Initialize lists which will store the received data
86 state.ytest = ytest.flatten()
87 state.wtest = wtest.flatten()
91 def partial_fit(state, X, S, y, w, epoch):
93 Stores received training data.
94 XGBoost is usually not able to perform a partial fit.
97 state.y.append(y.flatten())
98 state.w.append(w.flatten())
104 Merge received data together and fit estimator
106 dtrain = xgb.DMatrix(np.vstack(state.X), label=np.hstack(state.y).astype(int), weight=np.hstack(state.w))
108 if len(state.Xtest) > 0:
109 dtest = xgb.DMatrix(state.Xtest, label=state.ytest.astype(int), weight=state.wtest)
110 evallist = [(dtest,
'eval'), (dtrain,
'train')]
112 evallist = [(dtrain,
'train')]
114 state.estimator = xgb.train(state.parameters, dtrain, state.num_round, evallist)
115 f = tempfile.NamedTemporaryFile(delete=
False)
117 state.estimator.save_model(f.name)
118 with open(f.name,
'rb')
as f2: