w3resource

Numpy - Calculate outer product of two large arrays using For loops and Optimization


NumPy: Performance Optimization Exercise-16 with Solution


Write a function to calculate the outer product of two large 1D NumPy arrays using nested for loops. Optimize it using NumPy's outer() function.

Sample Solution:

Python Code:

import numpy as np

# Generate two large 1D NumPy arrays with random integers
array1 = np.random.randint(1, 1000, size=1000)
array2 = np.random.randint(1, 1000, size=1000)

# Function to calculate the outer product using nested for loops
def outer_product_with_loops(arr1, arr2):
    result = np.empty((len(arr1), len(arr2)), dtype=int)
    for i in range(len(arr1)):
        for j in range(len(arr2)):
            result[i, j] = arr1[i] * arr2[j]
    return result

# Calculate the outer product using the nested for loops method
outer_with_loops = outer_product_with_loops(array1, array2)

# Calculate the outer product using NumPy's outer() function
outer_with_numpy = np.outer(array1, array2)

# Display the first 10x10 section of the result to verify
print("Outer product using for loops (first 10x10 elements):")
print(outer_with_loops[:10, :10])

print("Outer product using NumPy (first 10x10 elements):")
print(outer_with_numpy[:10, :10])

Output:

Outer product using for loops (first 10x10 elements):
[[486878 486878 401402  43576 415648 473470 128214 455034  71230 451682]
 [339885 339885 280215  30420 290160 330525  89505 317655  49725 315315]
 [449113 449113 370267  40196 383408 436745 118269 419739  65705 416647]
 [245182 245182 202138  21944 209312 238430  64566 229146  35870 227458]
 [513023 513023 422957  45916 437968 498895 135099 479469  75055 475937]
 [230076 230076 189684  20592 196416 223740  60588 215028  33660 213444]
 [296310 296310 244290  26520 252960 288150  78030 276930  43350 274890]
 [392756 392756 323804  35152 335296 381940 103428 367068  57460 364364]
 [128982 128982 106338  11544 110112 125430  33966 120546  18870 119658]
 [540911 540911 445949  48412 461776 526015 142443 505533  79135 501809]]
Outer product using NumPy (first 10x10 elements):
[[486878 486878 401402  43576 415648 473470 128214 455034  71230 451682]
 [339885 339885 280215  30420 290160 330525  89505 317655  49725 315315]
 [449113 449113 370267  40196 383408 436745 118269 419739  65705 416647]
 [245182 245182 202138  21944 209312 238430  64566 229146  35870 227458]
 [513023 513023 422957  45916 437968 498895 135099 479469  75055 475937]
 [230076 230076 189684  20592 196416 223740  60588 215028  33660 213444]
 [296310 296310 244290  26520 252960 288150  78030 276930  43350 274890]
 [392756 392756 323804  35152 335296 381940 103428 367068  57460 364364]
 [128982 128982 106338  11544 110112 125430  33966 120546  18870 119658]
 [540911 540911 445949  48412 461776 526015 142443 505533  79135 501809]]

Explanation:

  • Importing numpy: We first import the numpy library for array manipulations.
  • Generating large arrays: Two large 1D NumPy arrays with random integers are generated.
  • Defining the function: A function outer_product_with_loops is defined to calculate the outer product using nested for loops.
  • Calculating with loops: The outer product is calculated using the nested for loops method.
  • Calculating with numpy: The outer product is calculated using NumPy's built-in outer() function.
  • Displaying results: The first 10x10 section of the outer product from both methods is printed out to verify correctness.

Python-Numpy Code Editor: