Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compute the outer product of two given vectors using NumPy in Python
The outer product of two vectors is a matrix obtained by multiplying each element of the first vector with each element of the second vector. In NumPy, the outer product of vectors a and b is denoted as a ? b.
The resulting matrix has dimensions (m, n) where m is the length of the first vector and n is the length of the second vector.
Syntax
NumPy provides the outer() function to compute the outer product ?
np.outer(a, b)
Parameters:
- a First input array (flattened if multi-dimensional)
- b Second input array (flattened if multi-dimensional)
Returns: A 2D array representing the outer product.
Example 1: Outer Product of 1D Arrays
Let's compute the outer product of two simple vectors ?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
print("Vector a:", a)
print("Vector b:", b)
outer_product = np.outer(a, b)
print("Outer product:")
print(outer_product)
Vector a: [1 2 3] Vector b: [4 5] Outer product: [[ 4 5] [ 8 10] [12 15]]
Example 2: Outer Product with Larger Arrays
Here's an example with larger arrays ?
import numpy as np
a = np.array([34, 23, 90, 34])
b = np.array([90, 34, 43, 23])
print("Vector a:", a)
print("Vector b:", b)
outer_product = np.outer(a, b)
print("Outer product:")
print(outer_product)
print("Shape:", outer_product.shape)
Vector a: [34 23 90 34] Vector b: [90 34 43 23] Outer product: [[3060 1156 1462 782] [2070 782 989 529] [8100 3060 3870 2070] [3060 1156 1462 782]] Shape: (4, 4)
Example 3: Outer Product with Multi-dimensional Arrays
NumPy automatically flattens multi-dimensional arrays before computing the outer product ?
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print("Array a:")
print(a)
print("Array b:")
print(b)
outer_product = np.outer(a, b)
print("Outer product (flattened arrays):")
print(outer_product)
print("Shape:", outer_product.shape)
Array a: [[1 2] [3 4]] Array b: [[5 6] [7 8]] Outer product (flattened arrays): [[ 5 6 7 8] [10 12 14 16] [15 18 21 24] [20 24 28 32]] Shape: (4, 4)
Manual Calculation vs NumPy
You can also compute the outer product manually using broadcasting ?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
# Using np.outer()
outer_numpy = np.outer(a, b)
# Manual calculation using broadcasting
outer_manual = a[:, np.newaxis] * b
print("Using np.outer():")
print(outer_numpy)
print("\nUsing broadcasting:")
print(outer_manual)
print("\nAre they equal?", np.array_equal(outer_numpy, outer_manual))
Using np.outer(): [[ 4 5] [ 8 10] [12 15]] Using broadcasting: [[ 4 5] [ 8 10] [12 15]] Are they equal? True
Key Points
- The outer product creates a matrix where element (i,j) equals a[i] × b[j]
- Multi-dimensional arrays are automatically flattened
- The result is always a 2D array
- Broadcasting can achieve the same result as
np.outer()
Conclusion
The np.outer() function provides an efficient way to compute outer products in NumPy. It automatically handles array flattening and returns a 2D matrix representing the outer product of the input vectors.
