I'm trying to calculate the mutual information for unigrams in a dataset. When trying to do this, I'm trying to improve the speed when looping through numpy ndarrays. I have the following code where I'm using an already created matrix 'C' with 6018 rows and 27721 columns in order to compute the PMI matrix. Any ideas how to improve the for loop speed (currently it takes almost 4 hours to run)? I read in some other post about using Cython, but are there any alternatives? In advance, thanks for your help.
# MAKE MUTUAL INFO MATRIX, PMI
print "Creating mutual information matrix"
N = C.sum()
invN = 1/N # replaced divide by N with multiply by invN in formula below
PMI = np.zeros((C.shape))
row, col = C.shape
for r in xrange(row): # u
for c in xrange(r): # w
if C[r,c]!=0: # if they co-occur
numerator = C[r,c]*invN # getting number of reviews where u and w co-occur and multiply by invN (numerator)
denominator = (sum(C[:,c])*invN) * (sum(C[r])*invN)
pmi = log10(numerator*(1/denominator))
PMI[r,c] = pmi
PMI[c,r] = pmi