w3resource

Optimizing element-wise multiplication of large 2D NumPy arrays

NumPy: Performance Optimization Exercise-7 with Solution

Write a NumPy program that creates a function to compute the element-wise multiplication of two large 2D arrays using nested for loops. Optimize it using NumPy's vectorized operations.

Sample Solution:

Python Code:

import numpy as np

# Create two large 2D NumPy arrays with shape (1000, 1000)
array1 = np.random.rand(1000, 1000)
array2 = np.random.rand(1000, 1000)

# Function to compute element-wise multiplication using nested for loops
def elementwise_multiplication_using_loops(arr1, arr2):
    rows, cols = arr1.shape
    result = np.zeros((rows, cols))
    for i in range(rows):
        for j in range(cols):
            result[i, j] = arr1[i, j] * arr2[i, j]
    return result

# Compute the element-wise multiplication using the nested for loops
result_loop = elementwise_multiplication_using_loops(array1, array2)
print("Element-wise multiplication using for loop (first 5x5 block):\n", result_loop[:5, :5])

# Optimize the element-wise multiplication using NumPy's vectorized operations
result_vectorized = array1 * array2
print("Element-wise multiplication using vectorized operations (first 5x5 block):\n", result_vectorized[:5, :5])

Output:

Element-wise multiplication using for loop (first 5x5 block):
 [[0.42591046 0.10735024 0.296129   0.15170064 0.28291849]
 [0.12879494 0.34918637 0.05554299 0.05783684 0.22553233]
 [0.55246279 0.47389473 0.400696   0.6222842  0.53399356]
 [0.3779341  0.52980174 0.26423262 0.04478953 0.0223926 ]
 [0.14589255 0.21884822 0.52713264 0.1099746  0.75080976]]
Element-wise multiplication using vectorized operations (first 5x5 block):
 [[0.42591046 0.10735024 0.296129   0.15170064 0.28291849]
 [0.12879494 0.34918637 0.05554299 0.05783684 0.22553233]
 [0.55246279 0.47389473 0.400696   0.6222842  0.53399356]
 [0.3779341  0.52980174 0.26423262 0.04478953 0.0223926 ]
 [0.14589255 0.21884822 0.52713264 0.1099746  0.75080976]]

Explanation:

  • Create large arrays: Two large 2D NumPy arrays, each with shape (1000, 1000), are created using np.random.rand().
  • Function with nested for loops: A function elementwise_multiplication_using_loops performs element-wise multiplication of the arrays using nested for loops.
  • Compute multiplication with loops: The element-wise multiplication is calculated using the nested for loops, and the first 5x5 block of the result is printed.
  • Optimize with vectorization: The element-wise multiplication is optimized using NumPy's vectorized operations, and the first 5x5 block of the result is printed.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Optimizing Transposition of large NumPy arrays.
Next: Optimizing sum calculation of large 3D NumPy arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/optimizing-element-wise-multiplication-of-large-2d-numpy-arrays.php