# Cannot evaluate multiclass BDT response with bagging

**URL:** <https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241>\
**Category:** TMVA\
**Tags:** bug\
**Created:** [March 31, 2021, 2:47pm UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241 "2021-03-31T14:47:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [March 31, 2021, 2:47pm UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241/1 "2021-03-31T14:47:32Z")

</div>

If I do a multiclass analysis with bagging enabled, the BDT response histogram has all entries assigned to the overflow bin. Effectively, this means I cannot evaluate my BDT response in a multiclass analysis with bagging. I have no such problem with a standard classification analysis.

Reproducer:

* * *

```python
import ROOT as r

r.RDataFrame(1000).Define("v", "gRandom->Gaus(5, 5)").Define("u", "gRandom->Landau(5, 5)").Define("e", "rdfentry_").Snapshot("atree", "sig.root")
r.RDataFrame(1000).Define("v", "gRandom->Gaus(1, 3)").Define("u", "gRandom->Landau(1, 3)").Define("e", "rdfentry_").Snapshot("atree", "bkg.root")
r.RDataFrame(1000).Define("v", "gRandom->Gaus(-1, 10)").Define("u", "gRandom->Landau(-1, 10)").Define("e", "rdfentry_").Snapshot("atree", "oth.root")
fsig = r.TFile.Open("sig.root")
tsig = fsig.atree
fbkg = r.TFile.Open("bkg.root")
tbkg = fbkg.atree
foth = r.TFile.Open("oth.root")
toth = foth.atree
fout = r.TFile.Open("out.root", "recreate")

dl = r.TMVA.DataLoader("dataset")
dl.AddVariable("v", "Gaussian distribution", "", "F")
dl.AddVariable("u", "Landau distribution", "", "F")
dl.AddSpectator("e", "entry number", "")
dl.AddTree(tsig, "sig")
dl.AddTree(tbkg, "bkg")
dl.AddTree(toth, "oth")

dl.PrepareTrainingAndTestTree("", r"nTest_sig=0:nTest_bkg=0:nTest_oth=0:NormMode=NumEvents:!V:SplitSeed=100:SplitMode=Random")
fact = r.TMVA.Factory("TMVAClassification", fout, r"!V:!Silent:AnalysisType=Multiclass")
fact.BookMethod(
    dl,
    r.TMVA.Types.kBDT,
    "BDT",
    r"!H:!V:nTrees=500:BoostType=Grad"
    r":UseBaggedGrad" # problem
)
fact.TrainAllMethods()
fact.TestAllMethods()
fact.EvaluateAllMethods()

fout.Close()

f = r.TFile.Open("out.root")
h = f.dataset.Method_BDT.BDT.MVA_BDT_Test_sig_prob_for_sig
overflowbin = h.GetNbinsX() + 1
if h.GetEntries() == h.GetBinContent(overflowbin):
    print("BDT response assigned to overflow")
else:
    print("BDT response assigned properly")

```

Th BDT response is assigned properly after commenting/removing the line marked `# problem`.

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [April 1, 2021, 5:52pm UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241/2 "2021-04-01T17:52:36Z")

</div>

@swunsch @moneta can you please take a look whenever you will have a time? Thank you!

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 13, 2021, 7:59am UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241/3 "2021-04-13T07:59:24Z")

</div>

@moneta ping

---

<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:** [April 13, 2021, 8:36am UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241/4 "2021-04-13T08:36:41Z")

</div>

Hi,  
The problem is that you are using a large number of trees and few events for training and this does not work well when using Bagging. If I increase for example the total number of input events/category to 10000 and use 100 trees it works fine.

Note that the option UseBaggedGrad is deprecated and you should use `UseBaggedBoost`

Lorenzo

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [April 13, 2021, 12:38pm UTC](https://root-forum.cern.ch/t/cannot-evaluate-multiclass-bdt-response-with-bagging/44241/5 "2021-04-13T12:38:03Z")

</div>

Ah, I see. Thank you for the help!
