Integer matrix to stochastic matrix normalization
<p>Suppose I have matrix with integer values. I want to make it stochastic matrix (i.e. sum of each row in matrix equal to 1)</p>

<p>I create random matrix, count sum of each row and divide each element in row for row sum.</p>

<pre><code>dt = pd.DataFrame(np.random.randint(0,10000,size=10000).reshape(100,100))
dt['sum_row'] = dt.sum(axis=1)
for col_n in dt.columns[:-1]:
    dt[col_n] = dt[col_n] / dt['sum_row']
</code></pre>

<p>After this sum of each row should be equal to 1. But it is not.</p>

<pre><code>(dt.sum_row_normalized == 1).value_counts()
&gt; False    75
&gt; True     25
&gt; Name: sum_row_normalized, dtype: int64
</code></pre>

<p>I understand that some values is not exactly 1 but very close to it. Nevertheless, how can I normalize matrix correctly?</p>

<p>You can't guarantee the floats will be exactly one, but you can check the closely to an arbitrary precision with <code>np.around</code>.</p>

<p>This is probably easier/faster without looping through pandas columns.</p>

<pre><code>X = np.random.randint(0,10000,size=10000).reshape(100,100)
X_float = X.astype(float)
Y = X_float/X_float.sum(axis=1)[:,np.newaxis]

sum(np.around(Y.sum(axis=1),decimals=10)==1) # is 100
</code></pre>

<p>(you don't need the <code>.astype(float)</code> step in python 3.x)</p>

python pandas numpy normalization stochastic