I have this piece of code:
n = np.load(matrix)["arr_0"]
shape = n.shape
##colsums and rowsums
rows = {}
cols = {}
for i in xrange(shape[0]): #row
rows[i] = np.sum(n[i,:])
for j in xrange(shape[1]): #cols
cols[j] = np.sum(n[:,j])
##looping over bins
for i in xrange(shape[0]): #row
print i
for j in xrange(shape[1]): #column
if rows[i] == 0 or cols[j] == 0:
continue
n[i,j] = n[i,j]/math.sqrt(rows[i]*cols[j])
It basically loops over a numpy matrix with shape (50000,50000) and I need to divide each value for the square root of the product of the sum of the corresponding column by the sum of the corresponding row. My implementation takes ages. Do you have any suggestions to improve its performance?
np.array. If you are then you are probably over-complicating something.