在熊猫中标记平滑(软目标)

Ser*_*ych 4 python machine-learning pandas

在Pandas中,有一种get_dummies方法可以对分类变量进行单热编码.现在我想按照深度学习书7.5.1节中的描述进行标签平滑:

标签平滑规则化基于与一个添加Softmax模型ķ通过更换硬输出值01与目标分类目标eps / k1 - (k - 1) / k * eps分别.

在Pandas数据帧中做标签熏制的最有效和/或最优雅的方法是什么?

lej*_*lot 8

首先,让我们使用更简单的方程式(?表示从"真实标签"移动的概率质量,并分配给所有剩余的方程).

1 -> 1 - ?
0 -> ? / (k-1) 
Run Code Online (Sandbox Code Playgroud)

你可以简单地使用上面很好的数学属性,因为你所要做的就是

x -> x * (1 - ?) + (1-x) * ? / (k-1)
Run Code Online (Sandbox Code Playgroud)

因此,如果您的虚拟列a, b, c, d只是做

indices = ['a', 'b', 'c', 'd']
eps = 0.1
df[indices] = df[indices] * (1 - eps) + (1-df[indices]) * eps / (len(indices) - 1)
Run Code Online (Sandbox Code Playgroud)

哪个

>>> df
   a  b  c  d
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
Run Code Online (Sandbox Code Playgroud)

回报

        a         b         c         d
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
Run Code Online (Sandbox Code Playgroud)

正如所料.