11 print(
"Please install sklearn: pip3 install sklearn")
23 """ Constructor of the state object """
28 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
30 Create SKLearn classifier and store it in a State object
32 from sklearn.ensemble
import GradientBoostingClassifier
33 if isinstance(parameters, collections.Mapping):
34 clf = GradientBoostingClassifier(**parameters)
35 elif isinstance(parameters, collections.Sequence):
36 clf = GradientBoostingClassifier(*parameters)
38 clf = GradientBoostingClassifier()
42 def feature_importance(state):
44 Return a list containing the feature importances
46 from sklearn.ensemble
import GradientBoostingClassifier
47 if isinstance(state.estimator, GradientBoostingClassifier):
48 return [x
for x
in state.estimator.feature_importances_]
54 Load sklearn estimator into state
61 Apply estimator to passed data.
62 If the estimator has a predict_proba it is called, otherwise call just predict.
64 if hasattr(state.estimator,
'predict_proba'):
65 x = state.estimator.predict_proba(X)[:, 1]
67 x = state.estimator.predict(X)
68 return np.require(x, dtype=np.float32, requirements=[
'A',
'W',
'C',
'O'])
71 def begin_fit(state, X, S, y, w):
73 Initialize lists which will store the received data
81 def partial_fit(state, X, S, y, w, epoch):
83 Stores received training data.
84 SKLearn is usually not able to perform a partial fit.
87 state.y.append(y.flatten())
88 state.w.append(w.flatten())
94 Merge received data together and fit estimator
96 state.estimator = state.estimator.fit(np.vstack(state.X), np.hstack(state.y), np.hstack(state.w))
97 return state.estimator