Numpy outer() Method Explained In-Depth With 5 Examples

Introduction to Numpy outer

Numpy outer() is the function in the numpy module in the python language. It is used to compute the outer level of products like vectors, arrays, etc. If we try to combine the two vectors of the array’s outer level, the numpy outer() function requires more than two levels of arguments that are passed into the function. It will be the array-like format, i.e., single or multi-parameter arguments. We can store the results in the out parameter. If we need to do the scientific calculations in the numpy library, those are calculated using dot operators.

What is Numpy outer() ?

The Numpy outer() function is used to compute the outer products of two vectors.

Syntax

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

Parameters of Numpy Outer

  1. a: array_like – It is the first input vector. The input is flattened if it is not 1- dimensional.
  2. b: array_like – It is the second input vector. The input is flattened if it is not 1- dimensional.
  3. Out: ndarray – It is optional in the function. It is the location where we store the result.

Return value

It returns the result as the outer products of two vectors. suppose i and j are the number of values in the array a and b. then, out[i, j] = a[i] * b[j].

Examples to Use Numpy outer() Function in the Best Way

Let us understand the outer function of the numpy module in details with the help of examples:

1. using linspace function to calculate numpy outer product

A linspace() function is used to return number spaces evenly concerning the interval. In this. we will import the numpy library as np. Then, we will take the input as np.ones() and np.linspace(). At last, we will apply the numpy outer() function for the result.

#Numpy outer() program
#using linspace()

import numpy as np

x = np.ones(6)
y = np.linspace(2, 6, 3)

Output = np.outer(x, y)

print("Result : ",Output)

Output:

Result :  [[2. 4. 6.]
 [2. 4. 6.]
 [2. 4. 6.]
 [2. 4. 6.]
 [2. 4. 6.]
 [2. 4. 6.]]

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, we have used np.ones(6) which means the result will get printed 6 times. Thirdly, we have used the linspace function, which returns the even space numbers evenly w,r,t the interval. np.linspace(2, 6, 3) means that from range 2 to 6, it will be divided into 3 parts, i.e., 2, 4, and 6. At last, we have applied the numpy outer() function and printed the value of output. Hence, we can see that the output is printed on the screen.

2. using 1-d matrix To Calculate Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will be taken the input in a 1-d matrix. Finally, we will apply the numpy outer() function to produce the outer products of two vectors or arrays.

#using a 1-d matrix
#importing numpy as np

import numpy as np

arr1 = np.array([2, 6])
arr2 = np.array([5, 8])

print("Array1 : ",arr1)
print("Array2 : ",arr2)

output = np.outer(arr1, arr2)
print("Result : ",output)

Output:

Array1 :  [2 6]
Array2 :  [5 8]
Result :  [[10 16]
 [30 48]]

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, we have taken the input of two 1-d Array as np.array() in arr1 and arr2. Thirdly, we have printed the value of both the arrays as array1 and array2. At last, we have applied the numpy outer() function and printed the value of output. The output is printed as arr1[0] is multiplied with both the values of arr2, and arr1[1] is also multiplied with both values of arr2. Hence, we can see that the output is printed on the screen.

3. using 2-d matrix To Calculate Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will be taken the input in a 2-d matrix. Finally, we will apply the numpy outer() function to produce the outer products of two vectors or arrays.

#using 2-d matrix
#importing numpy as np

import numpy as np

arr1 = np.array([[2, 5], [3, 5]])
arr2 = np.array([[1, 2], [2, 6]])

print("Array1 : ",arr1)
print("Array2 : ",arr2)

output = np.outer(arr1, arr2)
print("Result : ",output)

Output:

Array1 :  [[2 5]
 [3 5]]
Array2 :  [[1 2]
 [2 6]]
Result :  [[ 2  4  4 12]
 [ 5 10 10 30]
 [ 3  6  6 18]
 [ 5 10 10 30]]

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, we have taken the input of two 2-d Array as np.array() in arr1 and arr2. Thirdly, we have printed the value of both the arrays as array1 and array2. At last, we have applied the numpy outer() function and printed the value of output. The out is printed as all the values of arr1 are multiplied with all the values of arr2. Hence, we can see that the output is printed on the screen.

4. using 3-d matrix To Calculate Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will be taken the input in a 3-d matrix. At last, we will apply the numpy outer() function to produce the outer products of two vectors or arrays.

#using 3-d matrix
#importing numpy as np

import numpy as np

arr1 = np.array([[2, 1,3], [0, 2, 5], [0, 1, 2]])
arr2 = np.array([[1, 2, 3], [2, 5, 6], [2, 3, 4]])

print("Array1 : ",arr1)
print("Array2 : ",arr2)

output = np.outer(arr1, arr2)
print("Result : ",output)

Output:

Array1 :  [[2 1 3]
 [0 2 5]
 [0 1 2]]
Array2 :  [[1 2 3]
 [2 5 6]
 [2 3 4]]
Result :  [[ 2  4  6  4 10 12  4  6  8]
 [ 1  2  3  2  5  6  2  3  4]
 [ 3  6  9  6 15 18  6  9 12]
 [ 0  0  0  0  0  0  0  0  0]
 [ 2  4  6  4 10 12  4  6  8]
 [ 5 10 15 10 25 30 10 15 20]
 [ 0  0  0  0  0  0  0  0  0]
 [ 1  2  3  2  5  6  2  3  4]
 [ 2  4  6  4 10 12  4  6  8]]

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, we have taken the input of two 3-d Array as np.array() in arr1 and arr2. Thirdly, we have printed the value of both the arrays as array1 and array2. At last, we have applied the numpy outer() function and printed the value of output. The out is printed as all the values of arr1 are multiplied with all the values of arr2. Hence, we can see that the output is printed on the screen.

5. using out parameter To Calculate Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will be taken the input in a 1-d matrix. Then, we will be specifying the out parameter as a result of the same shape as the input. Finally, we will apply the numpy outer() function to produce the outer products of two vectors or arrays.

#using out parameter
#importing numpy as np

import numpy as np

arr1 = np.array([2, 6])
arr2 = np.array([5, 8])
result = np.zeros((2,2))

print("Array1 : ",arr1)
print("Array2 : ",arr2)

output = np.outer(arr1, arr2, out = result)
print("Result : ",output)

Output:

Array1 :  [2 6]
Array2 :  [5 8]
Result :  [[10. 16.]
 [30. 48.]]

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, we have taken the input of two 1-d Array as np.array() in arr1 and arr2. Thirdly, we have printed the value of both the arrays as array1 and array2. Finally, we have applied the numpy outer() function without as the parameter and printed the value of output in the result variable. The output is printed as arr1[0] is multiplied with both the values of arr2, and arr1[1] is also multiplied with both values of arr2. Hence, we can see that the output is printed on the screen.

Numpy outer() vs Numpy Inner()

Numpy.outer(): The Numpy outer() function is used to compute two vectors’ outer products.

Numpy.inner(): The Numpy outer() function is used to compute two arrays’ inner products.

Let us understand this with the help of example:

taking 1-d array

import numpy as np

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
print("output : ",np.outer(arr1,arr2))
print("\n")
print("output : ",np.inner(arr1,arr2))

Output:

output :  [[ 4  5  6]
 [ 8 10 12]
 [12 15 18]]


output :  32

Explanation:

Here firstly, we have imported the numpy module as np. Secondly, Then we have to take two input arrays as arr1 and arr2. Thirdly, we have applied outer() and inner() and printed the output. So, in the outer(), we can see that all the arr1 and arr2 multiply themselves and formed a matrix, and an inner() we can see that each value multiplied with its same index and added itself then gives the output.

Numpy outer() vs Numpy dot()

Numpy.outer() : The Numpy outer() function is used to compute two vectors’ outer products.

Numpy.dot() : The Numpy dot() function is used to compute the dot product of two arrays.

Let us understand this with the help of example:

#outer() vs dot()

import numpy as np

arr1 = np.array([[1,2], [4,5]])
arr2 = np.array([[7,8], [10,11]])

print("Outer() : ",np.outer(arr1,arr2))
print("\n")
print("dot() : ",np.dot(arr1,arr2))

Output:

Outer() :  [[ 7  8 10 11]
 [14 16 20 22]
 [28 32 40 44]
 [35 40 50 55]]


dot() :  [[27 30]
 [78 87]]

NOTE:

REMEMBER: for dot product the array should always be square

dot function is calculated as 
dot() = [[1*7 + 2*10 , 1*8 + 2*11], [4*7 + 5*10, 4*8 + 5*11]

Explanation:

Here firstly, we have imported numpy as np. Secondly, we have taken two arrays as arr1 and arr2 as input. Thirdly, We have applied the outer() and dot() and printed the output. In the note, I have explained to you how the dot product is calculated. Hence, we can see the output and the difference between the outer() and dot().

Conclusion

In this tutorial, we have discussed the outer function of the numpy module. All the parameters are explained in detail. With the help of examples, you can understand the concept in detail. You can use the out parameter according to the need of the project and program.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments