Home >Backend Development >Python Tutorial >Calculate outer product of given two vectors in Python using NumPy
The outer product of two vectors is the matrix obtained by multiplying each element of vector A by each element of vector B. The outer product of vectors a and b is a ⊗ b. Below is the mathematical formula for calculating the outer product.
a ⊗ b = [a[0] * b, a[1] * b, ..., a[m-1] * b]
where,
a, b are vectors.
# Represents the element-wise multiplication of two vectors.
Use Numpy to calculate outer product
grammar
np.outer(array1, array2)where,
import numpy as np a = np.array([34,23,90,34]) b = np.array([90,34,43,23]) print("The input arrays:",a,b) outer_product = np.outer(a,b) print("The Outer product of the given input arrays:",outer_product)Output
The input arrays: [34 23 90 34] [90 34 43 23] The Outer product of the given input arrays: [[3060 1156 1462 782] [2070 782 989 529] [8100 3060 3870 2070] [3060 1156 1462 782]]
import numpy as np a = np.array([[34,23],[90,34]]) b = np.array([[90,34],[43,23]]) print("The input arrays:",a,b) outer_product = np.outer(a,b) print("The Outer product of the given input arrays:",outer_product)Output
The input arrays: [[34 23] [90 34]] [[90 34] [43 23]] The Outer product of the given input arrays: [[3060 1156 1462 782] [2070 782 989 529] [8100 3060 3870 2070] [3060 1156 1462 782]]Example
import numpy as np a = np.array([[[34,23],[90,34]],[[12,5],[14,5]]]) b = np.array([[[90,34],[43,23]],[[1,22],[7,2]]]) print("The input arrays:",a,b) outer_product = np.outer(a,b) print("The Outer product of the given input arrays:",outer_product)Output
The input arrays: [[[34 23] [90 34]] [[12 5] [14 5]]] [[[90 34] [43 23]] [[ 1 22] [ 7 2]]] The Outer product of the given input arrays: [[3060 1156 1462 782 34 748 238 68] [2070 782 989 529 23 506 161 46] [8100 3060 3870 2070 90 1980 630 180] [3060 1156 1462 782 34 748 238 68] [1080 408 516 276 12 264 84 24] [ 450 170 215 115 5 110 35 10] [1260 476 602 322 14 308 98 28] [ 450 170 215 115 5 110 35 10]]
The above is the detailed content of Calculate outer product of given two vectors in Python using NumPy. For more information, please follow other related articles on the PHP Chinese website!