For Loop Optimization in Python
<p>I have run the following code for matrices of size 100 million rows and 100 columns.</p>

<p>I am looking to optimize the following code. Any help would be really appreciated. </p>

<pre><code>def matrix_factorization(R, P, Q, K, steps=5000, alpha=0.0002, beta=0.02):

    Q = Q.T

    for step in range(steps):
        e = 0
        for i in range(len(R)):
            for j in range(len(R[i])):
                if R[i][j] &gt; 0:
                    temp_dot = numpy.dot(P[i,:],Q[:,j])
                    eij = R[i][j] - temp_dot
                    e = e + eij*eij
                    for k in range(K):
                        P[i][k] = P[i][k] + alpha * (2 * eij * Q[k][j] - beta * P[i][k])
                        Q[k][j] = Q[k][j] + alpha * (2 * eij * P[i][k] - beta * Q[k][j])
                        e = e + (beta/2) * ((P[i][k]*P[i][k]) + (Q[k][j]*Q[k][j]))
        if e &lt; 0.001:
            break

    return P, Q.T
</code></pre>