# Multiclass capability for more methods?

**URL:** <https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278>\
**Category:** TMVA\
**Created:** [May 6, 2020, 3:36am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278 "2020-05-06T03:36:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [May 6, 2020, 3:36am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/1 "2020-05-06T03:36:06Z")

</div>

Hello all,

I was just wondering if work was being done to enable multiclass capability with more methods, in particular Fisher Discriminant Analysis? From [this tutorial](https://root.cern.ch/doc/v608/TMVAMulticlass_8C.html) it looks like only a handful of the methods have multiclass support? Are these the only methods which have multiclass support, or is there somewhere that lists all the methods that do or the methods that don’t?

Methods involving linear combinations of variables such as FDA are appealing in that what they do appears more transparent than methods involving hidden layers. From [here](https://en.wikipedia.org/wiki/Linear_discriminant_analysis#Multiclass_LDA) it looks like multiclass FDA exists.

---

<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:** [May 6, 2020, 9:52am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/2 "2020-05-06T09:52:12Z")

</div>

Hi,  
I am not aware of a full list of the methods that support multi-class classification. Maybe @kialbert, who worked recently on improving the Multiclass support in TMVA knows this better.  
however, if a method does not support multi-class it should give you a warning message when you are booking it.  
The Fisher discriminant is for example not supporting multi-class. As you pointed out in the reference you provide, we could extend for supporting it. It is maybe something that external users can contribute to it, if you want to try doing it and create a PR for it, we will be grateful.

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 17, 2020, 1:38am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/3 "2020-08-17T01:38:40Z")

</div>

So in principle, multiclass Fisher/Linear Discriminant Analysis (LDA) should be achievable with Function Discriminant Analysis (FDA). When I try simulating a LDA with FDA as described on page 89 of the TMVA User’s Guide, I appear to get NaN coefficients. Because of this I have fallen back to a one-versus-rest methodology with the LDA binaries, as done in the attached macro.

[trainStokesFisherOvRResponse.C](https://root-forum.cern.ch/uploads/short-url/uaDx2AhJpHO6NqYX6eRaamk08ah.C) (26.6 KB)

One thing I am uncertain of, though, is if the option “NormMode” should be set to “None” to behave as the Multiclass method does? Or should weights be explicitly set for the class samples? Especially if the class samples vary in size. Could you or @kialbert comment on this? The question is if Multiclass does some sort of balancing of samples added from different class trees, that isn’t done in TMVA binary classification.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 20, 2020, 5:13pm UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/4 "2020-08-20T17:13:26Z")

</div>

Hi,

To exactly replicate the _default_ weighting of the multiclass version you can use use `NormMode=None` and manually scale the each input tree by `n_0/n_i` where `n_0` is the number of events in the _first_ class and `n_i` is the number of events in the current class/tree (assuming there is one class per input tree).

```auto
dataloader.AddBackgroundTree(&chain0, n_0/n_0);
dataloader.AddBackgroundTree(&chain1, n_0/n_1);
// ...

```

`EqualNumEvents` normalises the number of _effective_ events in each class to the number of events in the first class.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 20, 2020, 7:15pm UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/5 "2020-08-20T19:15:07Z")

</div>

Thank you, Kim!

Just to clarify from your response, are any of these equivalent to the default? Assuming `chain0` is the chain for the one signal class with `n0` events and every other `chaini` is a background class with `ni` events:

(1 & 2) With `NormMode=None` or `NormMode=EqualNumEvents`:

```auto
dataloader.AddSignalTree(& chain0, 1 / n0);
dataloader.AddBackgroundTree(& chain1, 1 / n1);
dataloader.AddBackgroundTree(& chain2, 1 / n2);

```

(3) With `NormMode=EqualNumEvents`:

```auto
dataloader.AddSignalTree(& chain0);
dataloader.AddBackgroundTree(& chain1, 1 / n1);
dataloader.AddBackgroundTree(& chain2, 1 / n2);

```

(4 & 5) With `NormMode=None` or `NormMode=EqualNumEvents`:

```auto
dataloader.AddSignalTree(& chain0);
dataloader.AddBackgroundTree(& chain1, n0 / n1);
dataloader.AddBackgroundTree(& chain2, n0 / n2);

```

My suspicion is that (1), (3), and (4) are equivalent, but I just want to be sure.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 24, 2020, 9:43am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/6 "2020-08-24T09:43:16Z")

</div>

Hi,

Glad to be of help!

(1) and (2) are equivalent (and equivalent to TMVA `NormMode=NumEvents`).

(3), (4) and (5) are equivalent.

The difference between (1, 2) and (3, 4, 5) is that in the former the sum of the event weights for a class is equal to 1. In the latter the average event weight is ~approximately~ equal to 1 (in the case of very unbalanced classes this does not hold).

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 24, 2020, 10:23am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/7 "2020-08-24T10:23:14Z")

</div>

Interesting! Not what I was expecting. In case (3) why do the weights (1 / n\_1, 1 / n\_2) have the same effect as weights (n\_0 / n\_1, n\_0 / n\_2) in case (5)? The single signal tree of n\_0 events is weighted the same in these cases.

And so the default multiclass behavior would be replicated in cases (1) and (2), yes?

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 24, 2020, 1:30pm UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/8 "2020-08-24T13:30:07Z")

</div>

I think I was too quick in my previous answer. For some reason I missed that you have only signal and background in your examples 🤦‍♂️

(4) is equivalent to `EqualNumEvents` where average event weight is ~1 (unless very unbalanced classes).

(1) is a closely related version that where sum of _multiclass_ event weights is 1 per class.

To exemplify this we can look at this mockup code which mimics the TMVA process.

```auto
import numpy as np

def norm(cs, mode='EqualNumEvents'):
    if mode == 'EqualNumEvents':
       c0 = cs[0]
       class_weights = np.asarray([c0/c for c in cs])
    elif mode == 'None':
        class_weights = np.asarray([1.0 for c in cs])
    return class_weights

# Signal: 100 evs,
# Bkg1 : 200 evs,
# Bkg2 : 300 evs.
x = np.asarray([100, 200, 300])

x1 = np.asarray([1 , 1 + 1])
x2 = np.asarray([1 , 1 + 1])
x3 = np.asarray([100, 1 + 1])
x4 = np.asarray([100, 100 + 100])
x5 = np.asarray([100, 100 + 100])

np.set_printoptions(formatter={'float': lambda x: f'{x:>3.0f}'})
print('Multiclass weights:', norm(x))
print('Multiclass effective events:', norm(x)*x)

print(f'x1 weights={norm(x1, mode="None")}, eff_evs={norm(x1, mode="None")*x1}')
print(f'x2 weights={norm(x2) }, eff_evs={norm(x2)*x2 }')
print(f'x3 weights={norm(x3) }, eff_evs={norm(x3)*x3 }')
print(f'x4 weights={norm(x4, mode="None")}, eff_evs={norm(x4, mode="None")*x4}')
print(f'x5 weights={norm(x5) }, eff_evs={norm(x5)*x5 }')

# Output
# Multiclass weights: [1. 0.5 0.33]
# Multiclass effective events: [100. 100. 100.]
#
# x1 weights=[1.0 1.0], eff_evs=[1.0 2.0]
# x2 weights=[1.0 0.5], eff_evs=[1.0 1.0]
# x3 weights=[1.0 50.0], eff_evs=[100.0 100.0]
# x4 weights=[1.0 1.0], eff_evs=[100.0 200.0] # <- Equivalent to multiclass EqualNumEvents
# x5 weights=[1.0 0.5], eff_evs=[100.0 100.0]

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 24, 2020, 9:22pm UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/9 "2020-08-24T21:22:14Z")

</div>

Thank you for sharing this code, Kim!

So if I’m understanding correctly, `x1, x2, x3, x4, x5` are supposed to correspond one-to-one with the cases (1, 2, 3, 4, 5) that I outlined above. Then is `x4` corresponding to (4) the default behavior in mullticlass? If that is correct, is that the correct approach for very unbalanced classes? Or is `x1` corresponding to (1) more reasonable? How are they different for training purposes with a Fisher discriminant analysis?

From our discussions I have updated the macro I shared earlier to reflect case (1).

[trainStokesFisherOvRResponse.C](https://root-forum.cern.ch/uploads/short-url/nfQ9RVsWLE3ZACmXLDDP2osnjOd.C) (31.6 KB)

If case (4) is what I should be using, I have to explicitly set the weights on the background samples to (n\_0 / n\_1, n\_0 / n\_2) and keep the signal sample weight set to the default value of 1, while setting `NormMode=None`. Correct?

And case (4) is different from case (6):

```auto
NormMode=EqualNumEvents

dataloader.AddSignalTree(& chain0);
dataloader.AddBackgroundTree(& chain1);
dataloader.AddBackgroundTree(& chain2);

```

Correct?

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [August 25, 2020, 10:57am UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/10 "2020-08-25T10:57:56Z")

</div>

> [@jwruss](#):
>
> So if I’m understanding correctly, `x1, x2, x3, x4, x5` are supposed to correspond one-to-one with the cases (1, 2, 3, 4, 5) that I outlined above. Then is `x4` corresponding to (4) the default behavior in mullticlass?

Yes

> [@jwruss](#):
>
> If that is correct, is that the correct approach for very unbalanced classes? Or is `x1` corresponding to (1) more reasonable? How are they different for training purposes with a Fisher discriminant analysis?

To my understanding they are functionally equivalent, but there could be e.g. numerical issues that a particular algorithm is sensitive to. E.g. if event weights are small and the algorithm requires the square the number might underflow.

I am not familiar enough with the FDA/LDA to be able to say what goes here.

> [@jwruss](#):
>
> If case (4) is what I should be using, I have to explicitly set the weights on the background samples to (n\_0 / n\_1, n\_0 / n\_2) and keep the signal sample weight set to the default value of 1, while setting `NormMode=None` . Correct?

Yes

> [@jwruss](#):
>
> And case (4) is different from case (6):

Yes

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [August 25, 2020, 5:45pm UTC](https://root-forum.cern.ch/t/multiclass-capability-for-more-methods/39278/11 "2020-08-25T17:45:35Z")

</div>

Thank you!
