<p>Is that what you want?</p>
<pre><code>w_0 = (n_0+n_1+ ... +n_5) / (5.0 * n+0)
</code></pre>
<p>If so, it can be achieved by:</p>
<pre><code>    n = [n_0, n_1, n_2, ...]
    w = []
    for i in range(len(n)):
      w[i] = sum(n) / (len(n)*(n[i]))
</code></pre>
<p>Notice that <code>Sum(n)/Len(n) = Average(n)</code>, so you'd basically saying your loss-function is <code>avg(n)/ni</code>.</p>

CC BY-SA 4.0
2021-08-17T19:56:39.377Z
2021-08-17T19:56:39.377Z
Andre Goulart
<p>I have a machine learning task where I would like to weight losses based on the frequency of the categorical values appearing in the data. The binary solution can be seen below, but I'd like to know what to do about the case of n&gt;2 categories.</p>
<pre><code>w_0 = (n_0 + n_1) / (2.0 * n_0)
w_1 = (n_0 + n_1) / (2.0 * n_1)
</code></pre>
<p>The frequencies for the samples n0-n5 are:</p>
<pre><code>n_0:     1552829
n_1:     14479
n_2:     13445
n_3:     13781
n_4:     18795
n_5:     64187
</code></pre>

CC BY-SA 4.0
2021-08-17T18:54:20.813Z
What do you hope to get out of weighting the loss function?
Dave
<machine-learning><objective-functions><multiclass-classification><imbalanced-datasets>
How do I select the class weights for the loss function in the case of more than 2 classes?
Jan-Samuel Wagner
# How do I select the class weights for the loss function in the case of more than 2 classes?

(Asked by: username_0 on 2021-08-17)

I have a machine learning task where I would like to weight losses based on the frequency of the categorical values appearing in the data. The binary solution can be seen below, but I'd like to know what to do about the case of n>2 categories.
w_0 = (n_0 + n_1) / (2.0 * n_0)
w_1 = (n_0 + n_1) / (2.0 * n_1)

The frequencies for the samples n0-n5 are:
n_0:     1552829
n_1:     14479
n_2:     13445
n_3:     13781
n_4:     18795
n_5:     64187

**Tags:** machine-learning, objective-functions, multiclass-classification, imbalanced-datasets

> **Comments:**
> **username_1**: What do you hope to get out of weighting the loss function?  

---

## 1 answers

### Answer by username_2 on 2021-08-17. Score: 1

Is that what you want?
w_0 = (n_0+n_1+ ... +n_5) / (5.0 * n+0)

If so, it can be achieved by:
    n = [n_0, n_1, n_2, ...]
    w = []
    for i in range(len(n)):
      w[i] = sum(n) / (len(n)*(n[i]))

Notice that Sum(n)/Len(n) = Average(n), so you'd basically saying your loss-function is avg(n)/ni.


---

