Label smoothing (soft targets) in Pandas
<p>In Pandas there is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html" rel="nofollow"><code>get_dummies</code></a> method that one-hot encodes categorical variable. Now I want to do label smoothing as described in section 7.5.1 of <a href="http://www.deeplearningbook.org/" rel="nofollow">Deep Learning</a> book:</p>

<blockquote>
  <p>Label smoothing regularizes a model based on a softmax with <em>k</em> output values by replacing the hard <em>0</em> and <em>1</em> classification targets with targets of <code>eps / k</code> and <code>1 - (k - 1) / k * eps</code>, respectively.  </p>
</blockquote>

<p>What would be the most efficient and/or elegant way to do label smothing in Pandas dataframe? </p>

<p>First, lets use much simpler equation (<code>Ïµ</code> denotes how much probability mass you move from "true label" and distribute to all remaining ones).</p>

<pre><code>1 -&gt; 1 - Ïµ
0 -&gt; Ïµ / (k-1) 
</code></pre>

<p>You can simply use nice mathematical property of the above, since all you have to do is</p>

<pre><code>x -&gt; x * (1 - Ïµ) + (1-x) * Ïµ / (k-1)
</code></pre>

<p>thus if your dummy columns are <code>a, b, c, d</code> just do</p>

<pre><code>indices = ['a', 'b', 'c', 'd']
eps = 0.1
df[indices] = df[indices] * (1 - eps) + (1-df[indices]) * eps / (len(indices) - 1)
</code></pre>

<p>which for</p>

<pre><code>&gt;&gt;&gt; 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
</code></pre>

<p>returns</p>

<pre><code>        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
</code></pre>

<p>as expected.</p>

python pandas machine-learning