17 print(
"Please install xgboost: pip3 install xgboost")
30 def __init__(self, num_round=0, parameters=None):
31 """ Constructor of the state object """
40 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
42 Return default xgboost model
44 param = {
'bst:max_depth': 2,
'bst:eta': 1,
'silent': 1,
'objective':
'binary:logistic'}
46 if 'nTrees' in parameters:
47 nTrees = parameters[
'nTrees']
48 del parameters[
'nTrees']
49 if isinstance(parameters, collections.Mapping):
50 param.update(parameters)
51 return State(nTrees, param)
54 def feature_importance(state):
56 Return a list containing the feature importances
63 Load XGBoost estimator into state
66 f = tempfile.NamedTemporaryFile(delete=
False)
69 state.estimator = xgb.Booster({})
70 state.estimator.load_model(f.name)
77 Apply estimator to passed data.
80 result = state.estimator.predict(data)
81 return np.require(result, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
84 def begin_fit(state, Xtest, Stest, ytest, wtest):
86 Initialize lists which will store the received data
92 state.ytest = ytest.flatten()
93 state.wtest = wtest.flatten()
97 def partial_fit(state, X, S, y, w, epoch):
99 Stores received training data.
100 XGBoost is usually not able to perform a partial fit.
103 state.y.append(y.flatten())
104 state.w.append(w.flatten())
110 Merge received data together and fit estimator
112 dtrain = xgb.DMatrix(np.vstack(state.X), label=np.hstack(state.y).astype(int), weight=np.hstack(state.w))
114 if len(state.Xtest) > 0:
115 dtest = xgb.DMatrix(state.Xtest, label=state.ytest.astype(int), weight=state.wtest)
116 evallist = [(dtest,
'eval'), (dtrain,
'train')]
118 evallist = [(dtrain,
'train')]
120 state.estimator = xgb.train(state.parameters, dtrain, state.num_round, evallist)
121 f = tempfile.NamedTemporaryFile(delete=
False)
123 state.estimator.save_model(f.name)
124 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.