The outer product is an extremely useful linear algebra concept that has a variety of applications in quantum theory, signal processing, image compression, and a number of statistical algorithms.

In this tutorial, we’ll demonstrate how to find the outer products of two vectors or matrices with Python. Like many other linear algebra operations, the NumPy library in Python allows you to easily find the outer products of two vectors. NumPy is a powerful linear algebra workhorse!

## Outer Product of Vectors

Let’s first see how to find the outer product of two one-dimensional vectors. If you don’t have NumPy installed, go ahead and install it with:

`pip install numpy`

In the script below, we’ll create two one-dimensional NumPy arrays. Each of the two arrays contain three integer items. The arrays have been named `outer()`

method from the NumPy library to find the outer product of two vectors. Simply pass the two vectors as parameter values to the `outer()`

method. The output of the script below prints the two original vectors

```
import numpy as np
v1 = np.array([4,1,6])
v2 = np.array([7,2,4])
print("Original Vectors:")
print(v1)
print(v2)
print("Outer product:")
op = np.outer(v1, v2)
print(op)
```

**Output:**

```
Original Vectors:
[4 1 6]
[7 2 4]
Outer product:
[[28 8 16]
[ 7 2 4]
[42 12 24]]
```

## Outer Product of Matrices

With NumPy, you can use the `outer()`

method to find the outer product of multi-dimensional matrices as well. The following script defines two 2 x 2 matrices,

```
import numpy as np
m1 = np.array([[4,1],
[3,5]])
m2 = np.array([[7,2],
[3,1]])
print("Original Vectors:")
print(m1)
print(m2)
print("Outer product:")
op = np.outer(m1, m2)
print(op)
```

**Output:**

```
Original Vectors:
[[4 1]
[3 5]]
[[7 2]
[3 1]]
Outer product:
[[28 8 12 4]
[ 7 2 3 1]
[21 6 9 3]
[35 10 15 5]]
```

Let’s see one more example showing how the NumPy libraries `outer()`

function is used to find the outer product of two matrices - this time with two 3 x 3 arrays. The script below calculates the outer product of two 3 x 3 matrices and prints the output on the console.

```
import numpy as np
m1 = np.array([[4,1,8],
[3,5,7],
[7,2,6]])
m2 = np.array([[7,2,9],
[3,1,2],
[4,2,9]])
print("Original Vectors:")
print(m1)
print(m2)
print("Outer product:")
op = np.outer(m1, m2)
print(op)
```

**Output:**

```
Original Vectors:
[[4 1 8]
[3 5 7]
[7 2 6]]
[[7 2 9]
[3 1 2]
[4 2 9]]
Outer product:
[[28 8 36 12 4 8 16 8 36]
[ 7 2 9 3 1 2 4 2 9]
[56 16 72 24 8 16 32 16 72]
[21 6 27 9 3 6 12 6 27]
[35 10 45 15 5 10 20 10 45]
[49 14 63 21 7 14 28 14 63]
[49 14 63 21 7 14 28 14 63]
[14 4 18 6 2 4 8 4 18]
[42 12 54 18 6 12 24 12 54]]
```

Can you imagine how much of a pain that would be to do by hand? This is why I love programming! For weekly Python tutorials like this, go ahead and subscribe using the form below.

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.