17 print(
"Please install xgboost: pip3 install xgboost")
31 def __init__(self, num_round=0, parameters=None):
32 """ Constructor of the state object """
41 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
43 Return default xgboost model
45 param = {
'bst:max_depth': 2,
'bst:eta': 1,
'silent': 1,
'objective':
'binary:logistic'}
47 if 'nTrees' in parameters:
48 nTrees = parameters[
'nTrees']
49 del parameters[
'nTrees']
50 if isinstance(parameters, collections.Mapping):
51 param.update(parameters)
52 return State(nTrees, param)
55 def feature_importance(state):
57 Return a list containing the feature importances
64 Load XGBoost estimator into state
67 f = tempfile.NamedTemporaryFile(delete=
False)
70 state.estimator = xgb.Booster({})
71 state.estimator.load_model(f.name)
78 Apply estimator to passed data.
81 result = state.estimator.predict(data)
82 return np.require(result, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
85 def begin_fit(state, Xtest, Stest, ytest, wtest, nBatches):
87 Initialize lists which will store the received data
93 state.ytest = ytest.flatten()
94 state.wtest = wtest.flatten()
98 def partial_fit(state, X, S, y, w, epoch, batch):
100 Stores received training data.
101 XGBoost is usually not able to perform a partial fit.
104 state.y.append(y.flatten())
105 state.w.append(w.flatten())
111 Merge received data together and fit estimator
113 dtrain = xgb.DMatrix(np.vstack(state.X), label=np.hstack(state.y).astype(int), weight=np.hstack(state.w))
115 if len(state.Xtest) > 0:
116 dtest = xgb.DMatrix(state.Xtest, label=state.ytest.astype(int), weight=state.wtest)
117 evallist = [(dtest,
'eval'), (dtrain,
'train')]
119 evallist = [(dtrain,
'train')]
121 state.estimator = xgb.train(state.parameters, dtrain, state.num_round, evallist)
122 f = tempfile.NamedTemporaryFile(delete=
False)
124 state.estimator.save_model(f.name)
125 with open(f.name,
'rb')
as f2:
num_round
Number of boosting rounds used in xgboost training.
estimator
XGBoost estimator.
def __init__(self, num_round=0, parameters=None)
parameters
Parameters passed to xgboost model.