Question: Exercise 1: Loops vs Numpy operations In [ ]: Image ('lab2_exercisel.png', width = 1000) Out[2] : EXERCISE 1: Loops vs Numpy operations arr2d 3[0,0] =

 Exercise 1: Loops vs Numpy operations In [ ]: Image ('lab2_exercisel.png',

Exercise 1: Loops vs Numpy operations In [ ]: Image ('lab2_exercisel.png', width = 1000) Out[2] : EXERCISE 1: Loops vs Numpy operations arr2d 3[0,0] = arr2d 1[0,0] +arr2d 2[0, 0] arr2d_3[0, 1] = arr2d_1[0, 1] + arr2d_2[0, 1] arr2d_1 + arr2d_2 = arr2d_3 arr2d_3[N, N] = arr2d_1[N, N] + arr2d_2[N, N] In lab2_report_template.pynb, we provided numpy array variables 'array2d_1' and 'array2d_2'. Both have dimensions (1000, 1000) We want to perform elementwise addition between two and create a new array called 'array2d_3 Your task is to implement this operation in two ways: Using a loop without numpy (using two nested loop) Using an appropriate numpy function Run pre-written code to ensure two outputs are equal Measure and compare the computation time for each operation using the code in the template. Report which operation is faster and by how much In [ ]: import time # Import time to measure computational efficiency of the code In [ ]: arr2d_1 = np.random.randn(1000, 1000) * 10 arr2d_2 = np.random.randn(1000, 1000) * 10 In [ ]: * Elementwise addition using loop arr2d_3_loop = np.zeros((1000, 1000)) # Create a placeholder array for arr2d_3 start_time_loop = time.time() # start time of the code * YOUR CODE HERE FOR ELEMENTWISE ADDITION USING TWO NESTED LOOPS end_time_loop = time.time() # end time of the code elapsed_time_loop = end_time_loop - start_time_loop # end time - start time -> elapsed time in print(elapsed_time_loop) In [ ]: # Elementwise addition using Numpy function start_time_np = time.time) arr2d_3_np = # YOUR CODE HERE end_time_np = time.time() start_time_np elapsed_time_np = end_time_np print(elapsed_time_np) In [ ]: * Make sure two outputs are equivalent np. sum(arr2d_3_loop == arr2d_3_np) == 1000 * 1000 # Should output True if the outputs are same Which computation is faster and by what factor? e.g. a code that takes 0.1s is faster by a factor of 10 compared to a code that takes 1s

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!