Calculating mutual information in python returns nan
<p>I've implemented the <a href="https://en.wikipedia.org/wiki/Mutual_information" rel="nofollow">mutual information</a> formula in python using <code>pandas</code> and <code>numpy</code></p>

<pre><code>def mutual_info(p):
    p_x=p.sum(axis=1)
    p_y=p.sum(axis=0)
    I=0.0
    for i_y in p.index:
        for i_x in p.columns:
           I+=(p.ix[i_y,i_x]*np.log2(p.ix[i_y,i_x]/(p_x[i_y]*p[i_x]))).values[0]
    return I
</code></pre>

<p>However, if a cell in <code>p</code> has a zero probability, then <code>np.log2(p.ix[i_y,i_x]/(p_x[i_y]*p[i_x]))</code> is negative infinity, and the whole expression is multiplied by zero and returns <code>NaN</code>.</p>

<p>What is the right way to work around that?</p>