Belle II Software  release-06-00-14
bayesian_optimization.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # A simple example to use bayesian optimization for the hyperparameters of a FastBDT.
12 # The package used in this example is https://github.com/scikit-optimize
13 # and can be installed with
14 # pip3 install scikit-optimize
15 
16 # Training and test sample can be downloaded:
17 # http://ekpwww.ekp.kit.edu/~tkeck/train.root
18 # http://ekpwww.ekp.kit.edu/~tkeck/test.root
19 
20 
21 import basf2_mva
22 import basf2_mva_util
23 import skopt
24 import matplotlib.pyplot as plt
25 
26 
27 def f(x):
28  """Returns the figure of merit for the optimization.
29  The functions trains the classifier with the given hyperparameters on the training sample and
30  calculates the AUC on the independent test sample.
31  """
32  g_options = general_options
33  g_options.m_identifier = "test.xml"
34  options = basf2_mva.FastBDTOptions()
35  options.m_nTrees = int(x[0])
36  options.m_nLevels = int(x[1])
37  basf2_mva.teacher(g_options, options)
38  m = basf2_mva_util.Method(g_options.m_identifier)
39  p, t = m.apply_expert(test_data, general_options.m_treename)
41 
42 
43 if __name__ == "__main__":
44 
45  training_data = basf2_mva.vector("train.root")
46  test_data = basf2_mva.vector("test.root")
47 
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"
53 
54  # Start optimization
55  res = skopt.gp_minimize(f, # the function to minimize
56  [(10, 1000), (2, 6)], # the bounds on each dimension of x
57  x0=[10, 2], # initial guess
58  n_calls=20) # number of evaluations of f
59 
60  # Give some results
61  print(res)
62  skopt.plots.plot_convergence(res)
63  plt.savefig('convergence.png')
64  skopt.plots.plot_evaluations(res)
65  plt.savefig('evaluations.png')
66  skopt.plots.plot_objective(res)
67  plt.savefig('objective.png')
68 
69  # Store result of optimization
70  skopt.dump(res, 'opt-result.pkl')
def calculate_roc_auc(p, t)