2

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.]])
3
  • For starters, why is temp a list? It can simply be a float/int that keeps a running sum. Commented Oct 22, 2019 at 4:10
  • I think you can sum directly without building the temp list result[i][l]=np.sum(Z[i][j]*(R[j][l]+AL[j][l]*X[i][l]) for j in range(M)) Commented Oct 22, 2019 at 4:10
  • thank you @Jean-François Fabre Commented Oct 22, 2019 at 15:45

1 Answer 1

3

When using numpy, prefer using matrix and array operations instead of for iterations. The performance is drastically better.

Your solution can be written as:

result = Z.dot(R) + Z.dot(AL) * X

Output:

array([[ 18.,  36.,  60.],
       [ 95., 156., 231.],
       [232., 360., 510.],
       [429., 648., 897.]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.