First, let's use a much simpler equation ( Ο΅ denotes how much mass of probability you are moving from the βtrue labelβ and extending to everyone else).
1 -> 1 - Ο΅ 0 -> Ο΅ / (k-1)
You can simply use the nice math property above, since all you have to do is
x -> x * (1 - Ο΅) + (1-x) * Ο΅ / (k-1)
this way if your dummy columns a, b, c, d just do
indices = ['a', 'b', 'c', 'd'] eps = 0.1 df[indices] = df[indices] * (1 - eps) + (1-df[indices]) * eps / (len(indices) - 1)
which for
>>> df abcd 0 1 0 0 0 1 0 1 0 0 2 0 0 0 1 3 1 0 0 0 4 0 1 0 0 5 0 0 1 0
returns
abcd 0 0.900000 0.033333 0.033333 0.033333 1 0.033333 0.900000 0.033333 0.033333 2 0.033333 0.033333 0.033333 0.900000 3 0.900000 0.033333 0.033333 0.033333 4 0.033333 0.900000 0.033333 0.033333 5 0.033333 0.033333 0.900000 0.033333
as was expected.
source share