16 from basf2
import find_file
20 from skopt.space
import Integer
21 from sklearn.externals.joblib
import Parallel, delayed
22 import matplotlib.pyplot
as plt
25 def f(x, general_options, process_number):
26 """Returns the figure of merit for the optimization.
27 The functions trains the classifier with the given hyperparameters on the training sample and
28 calculates the AUC on the independent test sample.
30 g_options = general_options
31 g_options.m_identifier = f
"test{process_number}.xml"
32 options = basf2_mva.FastBDTOptions()
33 options.m_nTrees = int(x[0])
34 options.m_nLevels = int(x[1])
35 basf2_mva.teacher(g_options, options)
37 p, t = m.apply_expert(test_data, general_options.m_treename)
41 if __name__ ==
"__main__":
42 train_file = find_file(
"mva/train_D0toKpipi.root",
"examples")
43 test_file = find_file(
"mva/test_D0toKpipi.root",
"examples")
45 training_data = basf2_mva.vector(train_file)
46 test_data = basf2_mva.vector(test_file)
48 general_options = basf2_mva.GeneralOptions()
49 general_options.m_datafiles = training_data
50 general_options.m_treename =
"tree"
51 general_options.m_variables = basf2_mva.vector(
'p',
'pz',
'daughter(0, kaonID)',
'chiProb',
'M')
52 general_options.m_target_variable =
"isSignal"
55 optimizer = skopt.Optimizer(dimensions=[Integer(10, 1000), Integer(2, 6)], n_initial_points=3)
58 initial_guess = [10, 2]
59 initial_res = f(initial_guess, general_options, 0)
60 optimizer.tell(initial_guess, initial_res)
64 x = optimizer.ask(n_points=2)
65 y = Parallel(n_jobs=-1)(delayed(f)(v, general_options, index)
for index, v
in enumerate(x))
66 res = optimizer.tell(x, y)
70 skopt.plots.plot_convergence(res)
71 plt.savefig(
'convergence.png')
72 skopt.plots.plot_evaluations(res)
73 plt.savefig(
'evaluations.png')
74 skopt.plots.plot_objective(res)
75 plt.savefig(
'objective.png')
78 skopt.dump(res,
'opt-result.pkl')
def calculate_auc_efficiency_vs_background_retention(p, t, w=None)