# K-fold CV in TMVA::Experimental::RReader

**URL:** <https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779>\
**Category:** TMVA\
**Tags:** rdataframe\
**Created:** [June 25, 2024, 7:52am UTC](https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779 "2024-06-25T07:52:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anfeng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@anfeng](https://root-forum.cern.ch/u/anfeng)\
**Post date:** [June 25, 2024, 7:52am UTC](https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779/1 "2024-06-25T07:52:46Z")

</div>

Hi,

I am using TMVA::Experimental::RReader with k-fold cross validation and datasets split by deterministic SplitExpr to apply MVA prediction using RDataFrame. However, I am getting wrong MVA responses, which are very close to -1.

The code snippet I am using is like:

```auto
using namespace TMVA::Experimental;
ROOT::EnableImplicitMT(48);

RReader model("/path/to/TMVAdataset/weights/TMVAClassification_BDTG.weights.xml");

auto training_variables = model.GetVariableNames();
auto spectator_variables = model.GetSpectatorNames();
std::vector<std::string> variables(training_variables.size() + spectator_variables.size());
std::merge(training_variables.begin(), training_variables.end(), spectator_variables.begin(), spectator_variables.end(), variables.begin());

ROOT::RDataFrame rdf("DecayTree", "/path/to/input.root");
auto rdf2 = rdf.Define("BDTG_response", Compute<21, float>(model), variables);
rdf2.Snapshot("DecayTree", "/path/to/output.root");

```

and the response in the output file is like

```auto
+-----+-----------------+
| Row | BDTG_B_response |
+-----+-----------------+
| 0 | -0.999969 |
+-----+-----------------+
| 1 | -0.999946 |
+-----+-----------------+
| 2 | -0.999880 |
+-----+-----------------+
| 3 | -0.999982 |
+-----+-----------------+
| 4 | -0.999988 |
+-----+-----------------+
| 5 | -0.999983 |
+-----+-----------------+
| 6 | -0.999998 |
+-----+-----------------+
| 7 | -0.999995 |
+-----+-----------------+
| 8 | -0.999997 |
+-----+-----------------+
| 9 | -0.999997 |
+-----+-----------------+

```

So does TMVA::Experimental::RReader support k-fold CV with SplitExpr for now and what is the right way to use it?

The ROOT version I am using is 6.32.00.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 26, 2024, 4:37am UTC](https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779/2 "2024-06-26T04:37:11Z")

</div>

Hi,

Thanks for the post.  
It’s not fully clear what the `Compute<21,float>` methods is from the example you post. Maybe you can give some details? I add in the loop @moneta for the specific question at the end of the post.

best,  
D

---

<div class="post-metadata">

**Author:** ![anfeng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@anfeng](https://root-forum.cern.ch/u/anfeng)\
**Post date:** [June 26, 2024, 6:44am UTC](https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779/3 "2024-06-26T06:44:13Z")

</div>

Hi Danilo,

Thanks for your reply. The `Compute<21,float>` is from [tutorials/tmva/tmva003\_RReader.C](https://root.cern.ch/doc/master/tmva003__RReader_8C.html) (I think it is [TMVA::Experimental::Compute](https://root.cern/doc/master/namespaceTMVA_1_1Experimental.html#a1ff0a536f0ce7697e41b15d0b4197321)), where the `21` is the number of training\_variables + the number of spectator\_variables.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 28, 2024, 3:00pm UTC](https://root-forum.cern.ch/t/k-fold-cv-in-tmva-rreader/59779/4 "2024-06-28T15:00:38Z")

</div>

Hi,  
The RReader class is just to perform the evaluation of a trained model, reading the TMVA XML file.  
I think it should work when using CV, although I am not sure how much this is tested. Otherwise you can try to use directly the Reader class as shown in this tutorial  
[https://root.cern.ch/doc/master/TMVACrossValidationApplication\_8C.html](https://root.cern.ch/doc/master/TMVACrossValidationApplication_8C.html)

Lorenzo
