I am new in Python/Numpy and is trying to improve my triple for loop into a more efficient calculation, but can't quiet figure out how to do it.
The calculations is carried out on a grid of the size (25,35) and the shapes of arrays is:
A = (8760,25,35)
B = (12,25,35)
The first dimensions in A corresponds to the number hours in a year (~8760), and the first dimension in B is the number of months(12). I want to use the values in B[0,:,:] for the first month, and B[1,:,:] for the second etc.
So far I created, in a very unrefined way, a index array filled with 1,1,1...,2,2,2...,12 to extract the values from B. My code with some random numbers
N,M = (25, 35)
A = np.random.rand(8760,N,M)
B = np.random.rand(12,N,M)
q = len(A)/12
index = np.hstack((np.full((1,q),1),np.full((1,q),2),np.full((1,q),3),np.full((1,q),4),np.full((1,q),5),np.full((1,q),6),np.full((1,q),7),np.full((1,q),8),np.full((1,q),9),np.full((1,q),10),np.full((1,q),11),np.full((1,q),12)))-1
results = np.zeros((len(A),N,M))
for t in xrange(len(A)):
for i in xrange(N):
for j in xrange(M):
results[t][i][j] = some_function(A[t][i][j], B[index[(0,t)]][i][j],H = 80.)
def some_function(A,B,H = 80.0):
results = A*np.log(H/B)/np.log(10./B)
return results
How can increase the speed of this calculation?