11 print(
"Please install pandas: pip3 install pandas")
19 print(
"Please install hep_ml: pip3 install hep_ml")
31 """ Constructor of the state object """
36 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
38 Create hep_ml classifier and store it in a State object.
39 The features are used as train_features in uboost and the spectators are used as uniform_features.
40 You can pass additional parameters as a json-encoded string via m_config to the model.
41 I assume that the parameters are passed as a dictionary,
42 the key 'base_estimator' is passed to DecisionTreeClassifier as keyword arguments
43 other keys are passed to uBoostClassifier as keyword arguments
45 if isinstance(parameters, collections.Mapping)
and 'base_estimator' in parameters:
46 base_tree = hep_ml.uboost.DecisionTreeClassifier(**parameters[
'base_estimator'])
47 del parameters[
'base_estimator']
49 base_tree = hep_ml.uboost.DecisionTreeClassifier(max_depth=3)
51 train_features = list(range(number_of_features))
52 uniform_features = [number_of_features + i
for i
in range(number_of_spectators)]
54 if isinstance(parameters, collections.Mapping):
55 if 'uniform_label' not in parameters:
56 parameters[
'uniform_label'] = [0, 1]
57 parameters[
'train_features'] = train_features
58 parameters[
'uniform_features'] = uniform_features
59 clf = hep_ml.uboost.uBoostClassifier(base_estimator=base_tree, **parameters)
61 clf = hep_ml.uboost.uBoostClassifier(uniform_features=uniform_features, uniform_label=[0, 1],
62 base_estimator=base_tree, train_features=train_features)
66 def feature_importance(state):
68 Return a list containing the feature importances
75 Load sklearn estimator into state
82 Apply estimator to passed data.
83 If the estimator has a predict_proba it is called, otherwise call just predict.
85 X = pandas.DataFrame(X)
86 if hasattr(state.estimator,
'predict_proba'):
87 x = state.estimator.predict_proba(X)[:, 1]
89 x = state.estimator.predict(X)
90 return np.require(x, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
93 def begin_fit(state, X, S, y, w):
95 Initialize lists which will store the received data
104 def partial_fit(state, X, S, y, w, epoch):
106 Stores received training data.
107 HepML is usually not able to perform a partial fit.
111 state.y.append(y.flatten())
112 state.w.append(w.flatten())
118 Merge received data together and fit estimator
120 X = pandas.DataFrame(np.hstack([np.vstack(state.X), np.vstack(state.S)]))
121 state.estimator = state.estimator.fit(X, np.hstack(state.y), np.hstack(state.w))
122 return state.estimator