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?
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, B.shape)) for i in range(A.shape): for k in range(B.shape): c[i,k] = 0 for j in range(B.shape): 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 -
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