Flash Sale: Flat 70% + Addl. 25% Off on all Courses | Use Coupon DS25 in Checkout | Offer Expires In

  Enroll Now

Please follow the instructions below to compare the computational time while using Python array and Numpy array. Based on your observations which one is faster?

INSTRUCTIONS

Time comparison of a NumPy operation and a corresponding Python code

Let us create the below function (multiply_loops) which takes two arrays as input and computes their multiplication using normal Python way.

def multiply_loops(A, B):
    c=np.zeros((A.shape[0], B.shape[1]))
    for i in range(A.shape[0]):
        for k in range(B.shape[1]):
            c[i,k] = 0
            for j in range(B.shape[0]):
                n = A[i,j] * B[j,k]
                c[i,k] += n
    return c

Now, let us create the below function (multiply_vector) which takes two arrays as input and computes their multiplication using NumPy's vector multiplication way.

def multiply_vector(A, B):
     return A @ B

Let us create two randomly generated 100x100 matrices - X and Y - to test the above functions

X = np.random.random((100, 100))
Y = np.random.random((100, 100))

Now execute the below command (timeit) in Jupyter, which will output you the time taken by each of these functions

%timeit multiply_loops(X, Y)
%timeit multiply_vector(X, Y)

Note - Having trouble with the assessment engine? Follow the steps listed here


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...