Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 8b5d3b8

Browse files
committed
Merge pull request #200 from ndawe/master
[MRG] TMVA: add aux parameter for MethodCuts
2 parents f05d4c2 + f8a130e commit 8b5d3b8

File tree

7 files changed

+746
-545
lines changed

7 files changed

+746
-545
lines changed

examples/tmva/plot_multiclass.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@
4747
# The following line is necessary if events have been added individually:
4848
factory.PrepareTrainingAndTestTree(TCut('1'), 'NormMode=EqualNumEvents')
4949

50-
# Train a BDT
51-
factory.BookMethod('BDT', 'BDTG',
52-
'nCuts=20:NTrees=20:MaxDepth=4:'
53-
'BoostType=Grad:Shrinkage=0.10')
50+
# Train an MLP
51+
factory.BookMethod('MLP', 'MLP',
52+
'NeuronType=tanh:NCycles=200:HiddenLayers=N+2,2:'
53+
'TestRate=5:EstimatorType=MSE')
5454
factory.TrainAllMethods()
5555

5656
# Classify the test dataset with the BDT
5757
reader = TMVA.Reader()
5858
for n in range(2):
5959
reader.AddVariable('f{0}'.format(n), array('f', [0.]))
60-
reader.BookMVA('BDT', 'weights/classifier_BDTG.weights.xml')
61-
class_proba = evaluate_reader(reader, 'BDT', X_test)
60+
reader.BookMVA('MLP', 'weights/classifier_MLP.weights.xml')
61+
class_proba = evaluate_reader(reader, 'MLP', X_test)
6262

6363
# Plot the decision boundaries
6464
plot_colors = "rgb"
@@ -72,11 +72,10 @@
7272
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
7373
np.arange(y_min, y_max, plot_step))
7474

75-
Z = evaluate_reader(reader, 'BDT', np.c_[xx.ravel(), yy.ravel()])
75+
Z = evaluate_reader(reader, 'MLP', np.c_[xx.ravel(), yy.ravel()])
7676
Z = np.argmax(Z, axis=1) - 1
7777
Z = Z.reshape(xx.shape)
78-
plt.contourf(xx, yy, Z, cmap=cmap, vmin=Z.min(), vmax=Z.max(),
79-
levels=np.linspace(Z.min(), Z.max(), 50))
78+
plt.contourf(xx, yy, Z, cmap=cmap, alpha=0.5)
8079
plt.axis("tight")
8180

8281
# Plot the training points

root_numpy/tmva/_evaluate.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
]
1111

1212

13-
def evaluate_reader(reader, name, events):
13+
def evaluate_reader(reader, name, events, aux=0.):
1414
"""Evaluate a TMVA::Reader over a NumPy array.
1515
1616
Parameters
1717
----------
1818
reader : TMVA::Reader
19-
A TMVA::Factory instance with variables booked in
20-
exactly the same order as the columns in ``events``.
19+
A TMVA::Factory instance with variables booked in exactly the same
20+
order as the columns in ``events``.
2121
name : string
2222
The method name.
2323
events : numpy array of shape [n_events, n_variables]
24-
A two-dimensional NumPy array containing the rows of events
25-
and columns of variables.
24+
A two-dimensional NumPy array containing the rows of events and columns
25+
of variables. The order of the columns must match the order in which
26+
you called ``AddVariable()`` for each variable.
27+
aux : float, optional (default=0.)
28+
Auxiliary value used by MethodCuts to set the desired signal
29+
efficiency.
2630
2731
Returns
2832
-------
@@ -44,10 +48,11 @@ def evaluate_reader(reader, name, events):
4448
raise ValueError(
4549
"events must be a two-dimensional array "
4650
"with one event per row")
47-
return _libtmvanumpy.evaluate_reader(ROOT.AsCObject(reader), name, events)
51+
return _libtmvanumpy.evaluate_reader(
52+
ROOT.AsCObject(reader), name, events, aux)
4853

4954

50-
def evaluate_method(method, events):
55+
def evaluate_method(method, events, aux=0.):
5156
"""Evaluate a TMVA::MethodBase over a NumPy array.
5257
5358
.. warning:: TMVA::Reader has known problems with thread safety in versions
@@ -59,11 +64,15 @@ def evaluate_method(method, events):
5964
Parameters
6065
----------
6166
method : TMVA::MethodBase
62-
A TMVA::MethodBase instance with variables booked in
63-
exactly the same order as the columns in ``events``.
67+
A TMVA::MethodBase instance with variables booked in exactly the same
68+
order as the columns in ``events``.
6469
events : numpy array of shape [n_events, n_variables]
65-
A two-dimensional NumPy array containing the rows of events
66-
and columns of variables.
70+
A two-dimensional NumPy array containing the rows of events and columns
71+
of variables. The order of the columns must match the order in which
72+
you called ``AddVariable()`` for each variable.
73+
aux : float, optional (default=0.)
74+
Auxiliary value used by MethodCuts to set the desired signal
75+
efficiency.
6776
6877
Returns
6978
-------
@@ -85,4 +94,4 @@ def evaluate_method(method, events):
8594
raise ValueError(
8695
"events must be a two-dimensional array "
8796
"with one event per row")
88-
return _libtmvanumpy.evaluate_method(ROOT.AsCObject(method), events)
97+
return _libtmvanumpy.evaluate_method(ROOT.AsCObject(method), events, aux)

root_numpy/tmva/_factory.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ def add_classification_events(factory, events, labels, signal_label=None,
1717
Parameters
1818
----------
1919
factory : TMVA::Factory
20-
A TMVA::Factory instance with variables already booked in
21-
exactly the same order as the columns in ``events``.
20+
A TMVA::Factory instance with variables already booked in exactly the
21+
same order as the columns in ``events``.
2222
events : numpy array of shape [n_events, n_variables]
23-
A two-dimensional NumPy array containing the rows of events
24-
and columns of variables.
23+
A two-dimensional NumPy array containing the rows of events and columns
24+
of variables. The order of the columns must match the order in which
25+
you called ``AddVariable()`` for each variable.
2526
labels : numpy array of shape [n_events]
26-
The class labels (signal or background) corresponding to each event
27-
in ``events``.
27+
The class labels (signal or background) corresponding to each event in
28+
``events``.
2829
signal_label : float or int, optional (default=None)
2930
The value in ``labels`` for signal events, if ``labels`` contains only
3031
two classes. If None, the highest value in ``labels`` is used.
3132
weights : numpy array of shape [n_events], optional
3233
Event weights.
3334
test : bool, optional (default=False)
34-
If True, then the events will be added as test events, otherwise
35-
they are added as training events by default.
35+
If True, then the events will be added as test events, otherwise they
36+
are added as training events by default.
37+
38+
Notes
39+
-----
40+
A TMVA::Factory requires you to add both training and test events even if
41+
you don't intend to call ``TestAllMethods()``.
3642
3743
"""
3844
if not isinstance(factory, TMVA.Factory):
@@ -78,18 +84,26 @@ def add_regression_events(factory, events, targets, weights=None, test=False):
7884
Parameters
7985
----------
8086
factory : TMVA::Factory
81-
A TMVA::Factory instance with variables already booked in
82-
exactly the same order as the columns in ``events``.
87+
A TMVA::Factory instance with variables already booked in exactly the
88+
same order as the columns in ``events``.
8389
events : numpy array of shape [n_events, n_variables]
84-
A two-dimensional NumPy array containing the rows of events
85-
and columns of variables.
90+
A two-dimensional NumPy array containing the rows of events and columns
91+
of variables. The order of the columns must match the order in which
92+
you called ``AddVariable()`` for each variable.
8693
targets : numpy array of shape [n_events] or [n_events, n_targets]
87-
The target value(s) for each event in ``events``.
94+
The target value(s) for each event in ``events``. For multiple target
95+
values, ``targets`` must be a two-dimensional array with a column for
96+
each target in the same order in which you called ``AddTarget()``.
8897
weights : numpy array of shape [n_events], optional
8998
Event weights.
9099
test : bool, optional (default=False)
91-
If True, then the events will be added as test events, otherwise
92-
they are added as training events by default.
100+
If True, then the events will be added as test events, otherwise they
101+
are added as training events by default.
102+
103+
Notes
104+
-----
105+
A TMVA::Factory requires you to add both training and test events even if
106+
you don't intend to call ``TestAllMethods()``.
93107
94108
"""
95109
if not isinstance(factory, TMVA.Factory):

root_numpy/tmva/src/TMVA.pxi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ cdef extern from "2to3.h":
33
pass
44

55
cdef extern from "TMVA/Types.h" namespace "TMVA":
6+
ctypedef enum EMVA "TMVA::Types::EMVA":
7+
kCuts "TMVA::Types::kCuts"
8+
69
ctypedef enum ETreeType "TMVA::Types::ETreeType":
710
kTraining "TMVA::Types::kTraining"
811
kTesting "TMVA::Types::kTesting"
@@ -30,6 +33,7 @@ cdef extern from "TMVA/IMethod.h" namespace "TMVA":
3033

3134
cdef extern from "TMVA/MethodBase.h" namespace "TMVA":
3235
cdef cppclass MethodBase:
36+
EMVA GetMethodType()
3337
EAnalysisType GetAnalysisType()
3438
DataSetInfo DataInfo()
3539
unsigned int GetNVariables()
@@ -39,6 +43,10 @@ cdef extern from "TMVA/MethodBase.h" namespace "TMVA":
3943
vector[float] GetRegressionValues()
4044
Event* fTmpEvent
4145

46+
cdef extern from "TMVA/MethodCuts.h" namespace "TMVA":
47+
cdef cppclass MethodCuts:
48+
void SetTestSignalEfficiency(double eff)
49+
4250
cdef extern from "TMVA/Factory.h" namespace "TMVA":
4351
cdef cppclass Factory:
4452
void AddEvent(string& classname, ETreeType treetype, vector[double]& event, double weight)

0 commit comments

Comments
 (0)