I have a very large dataset and I am using following code. It's taking too much time for computation and I want to reduce number of iterations.
How can I improve the code's performance?
import numpy as np
Z=np.asarray([[1,2],
[3,4],
[5,6],
[7,8]])
R=np.asarray([[1,2,3],
[4,5,6]])
AL=np.asarray([[1,2,3],
[4,5,6]])
X=np.asarray([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])
N = 4
M = 2
D = 3
result = np.ones([N, D])
for i in range(N):
for l in range(D):
temp=[]
for j in range(M):
temp.append(Z[i][j]*(R[j][l]+AL[j][l]*X[i][l]))
result[i][l] = np.sum(temp)
print(result)
Output is:
array([[ 18., 36., 60.],
[ 95., 156., 231.],
[232., 360., 510.],
[429., 648., 897.]])
tempa list? It can simply be a float/int that keeps a running sum.result[i][l]=np.sum(Z[i][j]*(R[j][l]+AL[j][l]*X[i][l]) for j in range(M))