17 print(
"Please install pandas: pip3 install pandas")
25 print(
"Please install hep_ml: pip3 install hep_ml")
37 """ Constructor of the state object """
42 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
44 Create hep_ml classifier and store it in a State object.
45 The features are used as train_features in uboost and the spectators are used as uniform_features.
46 You can pass additional parameters as a json-encoded string via m_config to the model.
47 I assume that the parameters are passed as a dictionary,
48 the key 'base_estimator' is passed to DecisionTreeClassifier as keyword arguments
49 other keys are passed to uBoostClassifier as keyword arguments
51 if isinstance(parameters, collections.Mapping)
and 'base_estimator' in parameters:
52 base_tree = hep_ml.uboost.DecisionTreeClassifier(**parameters[
'base_estimator'])
53 del parameters[
'base_estimator']
55 base_tree = hep_ml.uboost.DecisionTreeClassifier(max_depth=3)
57 train_features = list(range(number_of_features))
58 uniform_features = [number_of_features + i
for i
in range(number_of_spectators)]
60 if isinstance(parameters, collections.Mapping):
61 if 'uniform_label' not in parameters:
62 parameters[
'uniform_label'] = [0, 1]
63 parameters[
'train_features'] = train_features
64 parameters[
'uniform_features'] = uniform_features
65 clf = hep_ml.uboost.uBoostClassifier(base_estimator=base_tree, **parameters)
67 clf = hep_ml.uboost.uBoostClassifier(uniform_features=uniform_features, uniform_label=[0, 1],
68 base_estimator=base_tree, train_features=train_features)
72 def feature_importance(state):
74 Return a list containing the feature importances
81 Load sklearn estimator into state
88 Apply estimator to passed data.
89 If the estimator has a predict_proba it is called, otherwise call just predict.
91 X = pandas.DataFrame(X)
92 if hasattr(state.estimator,
'predict_proba'):
93 x = state.estimator.predict_proba(X)[:, 1]
95 x = state.estimator.predict(X)
96 return np.require(x, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
99 def begin_fit(state, X, S, y, w):
101 Initialize lists which will store the received data
110 def partial_fit(state, X, S, y, w, epoch):
112 Stores received training data.
113 HepML is usually not able to perform a partial fit.
117 state.y.append(y.flatten())
118 state.w.append(w.flatten())
124 Merge received data together and fit estimator
126 X = pandas.DataFrame(np.hstack([np.vstack(state.X), np.vstack(state.S)]))
127 state.estimator = state.estimator.fit(X, np.hstack(state.y), np.hstack(state.w))
128 return state.estimator
estimator
Pickable sklearn estimator.
def __init__(self, estimator=None)