Home > Backend Development > Python Tutorial > Calculate outer product of given two vectors in Python using NumPy

Calculate outer product of given two vectors in Python using NumPy

WBOY
Release: 2023-09-01 15:41:05
forward
945 people have browsed it

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]
Copy after login

where,

  • a, b are vectors.

  • # Represents the element-wise multiplication of two vectors.

The output of the outer product is a matrix, where i and j are the elements of the matrix, where the i-th row is the vector obtained by multiplying the i-th element of the vector 'a' by the i-th element of the vector 'b' .

Use Numpy to calculate outer product

In Numpy, we have a function called outer(), which is used to calculate the outer product of two vectors.

grammar

The following is the syntax of the outer() function -

np.outer(array1, array2)
Copy after login

where,

  • The outside is a function.

  • array1 and array2 are input arrays.

Example

In the following example, we try to use the outer() function to calculate the outer product of two numpy arrays -

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) 
Copy after login

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]]
Copy after login

Example

Let's look at another example where we use the outer() function to calculate the outer product of a two-dimensional array -

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)
Copy after login

Output

The following is the output of the outer product of two arrays.

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]]
Copy after login

Example

Now, let's try to calculate the outer product of a 3D array.

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)
Copy after login

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]]
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template