Numpy.outer(): A Complete Guide

Numpy Outer

Vectors are quite fascinating entities forming the essence of navigating around different spaces & planes used in the field of mathematics. While there can be umpteen operations that can be carried out with the vectors, in this article, we will be exploring one such operation using an in-built function within the Python numpy library – the outer( ) function!

Also check: Difference Between Pandas Dataframe and Numpy Arrays

Let’s start things by importing the numpy library before getting on with the deployment of the outer( ) function using the below code.

import numpy as np

We shall cover the depths of the outer( ) function through the following sections.

  • Syntax of outer( ) function
  • Calculating Outer Product for One-Dimensional Arrays
  • Calculating Outer Product for N-Dimensional Arrays
  • Calculating Outer Product for Letters
  • Calculating Outer Product with Numpy functions

Syntax of outer( ) function

The underlying mechanism to determine the outer product of a given set of vectors is to multiply each element of one vector with each of the others and consider each corresponding result to be part of the output vector. This is all that happens within the outer( ) function whose syntax is as follows,

numpy.outer(a, b, out=None)

where,

  • a – n-dimensional array for the first input vector
  • b – n-dimensional array for the second input vector
  • out – an optional construct set to none by default, but could be used to store the results in an array of appropriate length

Calculating Outer Product for One-Dimensional Arrays

After importing the numpy library let us find the outer product of vectors using a couple of one-dimensional arrays as shown below.

ar1 = [[12, 36, 71, 99]]
ar2 = [[2, 8, 9, 56]]
np.outer(ar1, ar2)

The output array shall also contain 4 columns but the significant change would be in the count of rows which shall be 4 instead of 1. What happens here is the first element of the first array, ‘12’ gets multiplied with each of the elements in the second array, which explains the results of the first row of the output.

The first element of the output array ‘24’ is the product of 12×2, the second element ‘96’ is the product of 12×8 and so on and so forth. This iterates until the last element of the array is multiplied by 12 & then the process repeats with the next element ’36’. Now the results are recorded in a new row!

Outer Product Calculated For One Dimensional Arrays
Outer Product Calculated For One-Dimensional Arrays

Calculating Outer Product for N-Dimensional Arrays

In this section, let us calculate the outer product of vectors using arrays that have 3 columns & 2 rows. Happen to guess what the result might be?

ar3 = [[24, 25, 26],
       [5, 2, 9]]
ar4 = [[6, 8, 12],
       [4, 3, 10]]
np.outer(ar3, ar4)

A 6×6 array!

Outer Product Calculated For Two Dimensional Arrays
Outer Product Calculated For Two-Dimensional Arrays

Calculating Outer Product for Letters

The outer product of vectors ain’t limited only to the numbers, but one can also use letters by assigning their datatype as an ‘object’ as shown below.

ar5 = np.array(['x', 'y', 'z'], dtype = object)
ar6 = [[2, 6, 1]]
np.outer(ar5, ar6)

The resulting output array shall contain the letters in ‘ar5’ repeated the same number of times as specified in each element of ‘ar6’.

Outer Product Calculated For Letters
Outer Product Calculated For Letters

Calculating Outer Product with Numpy functions

One can also calculate the outer product of the vectors using a combination of ones( ) – which creates an array with ‘1’s as per the dimensions specified & linspace( ) – which creates a set of numbers by dividing the specified range into the specified number of parts.

Let’s use linspace( ) for diving the range between 2 & 8 into four parts for demonstration in this section.

ar7 = np.ones(4)
ar8 = np.linspace(2, 8, 4)
np.outer(ar7, ar8)
Outer Product Calculated With Numpy Functions
Outer Product Calculated With Numpy Functions

Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the outer( ) function from the numpy library. Here’s another article that explains how to find the square root using numpy in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!