w3resource

Optimizing Dot product calculation of large NumPy arrays

NumPy: Performance Optimization Exercise-3 with Solution

Write a function to compute the dot product of two large NumPy arrays using a nested for loop. Then, optimize it using NumPy's dot() function.

Sample Solution:

Python Code:

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

# Function to compute the dot product using nested for loops
def dot_product_using_loops(arr1, arr2):
    result = np.zeros((arr1.shape[0], arr2.shape[1]))
    for i in range(arr1.shape[0]):
        for j in range(arr2.shape[1]):
            for k in range(arr1.shape[1]):
                result[i, j] += arr1[i, k] * arr2[k, j]
    return result

# Compute the dot product using the nested for loops
dot_product_loops = dot_product_using_loops(array1, array2)
print("Dot product using nested for loops (first 5x5 block):\n", dot_product_loops[:5, :5])

# Optimize the dot product computation using NumPy's dot() function
dot_product_numpy = np.dot(array1, array2)
print("Dot product using NumPy's dot() function (first 5x5 block):\n", dot_product_numpy[:5, :5])

Output:

Dot product using nested for loops (first 5x5 block):
 [[24.22324898 25.99396249 24.81243363 26.47654967 21.87978998]
 [23.34320941 27.11995317 25.42488123 26.39399097 24.27911587]
 [26.08843058 27.77525101 25.13556487 27.49009703 24.78556125]
 [24.07227323 25.87107398 23.25500579 25.07011204 23.13590395]
 [24.31020899 28.82889983 26.5881906  26.41558584 23.82757658]]
Dot product using NumPy's dot() function (first 5x5 block):
 [[24.22324898 25.99396249 24.81243363 26.47654967 21.87978998]
 [23.34320941 27.11995317 25.42488123 26.39399097 24.27911587]
 [26.08843058 27.77525101 25.13556487 27.49009703 24.78556125]
 [24.07227323 25.87107398 23.25500579 25.07011204 23.13590395]
 [24.31020899 28.82889983 26.5881906  26.41558584 23.82757658]]

Explanation:

  • Generate two 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 dot_product_using_loops computes the dot product using nested for loops.
  • Calculate dot product with loops: The dot product of the arrays is calculated using the nested for loops, and the first 5x5 block of the result is printed.
  • Optimize with NumPy: The dot product computation is optimized using NumPy's dot() function, 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 element-wise addition of large NumPy arrays.
Next: Optimizing row-wise Mean calculation of large 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://www.w3resource.com/python-exercises/numpy/optimizing-dot-product-calculation-of-large-numpy-arrays.php