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

Contents

## 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 combine the two vectors of the array’s outer level, the numpy outer() function requires more than two levels of arguments 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- 1-dimensional.
2. b: array_like – It is the second input vector. The input is flattened if it is not 1- 1-dimensional.
3. Out: ndarray – It is optional in the function. It is the location where we store the results.

## 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 of Use Numpy outer() Function in the Best Way

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

### 1. Using linspace function to calculate the numpy outer product

A linspace() function returns number of 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), meaning the result will be 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 output value. Hence, we can see that the output is printed on the screen.

### 2. Using 1-D matrix To Calculate the Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will take 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 the arrays as array1 and array2. At last, we applied the numpy outer() function and printed the output value. The output is printed as arr1[0], which is multiplied with both the values of arr2, and arr1[1], which 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 the Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will take 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 the arrays as array1 and array2. At last, we applied the numpy outer() function and printed the output value. The out is printed as all the values of arr1 are multiplied by all the values of arr2. Hence, we can see that the output is printed on the screen.

### 4. Using 3-D matrix To Calculate the Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will take 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 the arrays as array1 and array2. At last, we applied the numpy outer() function and printed the output value. The out is printed as all the values of arr1 are multiplied by all the values of arr2. Hence, we can see that the output is printed on the screen.

### 5. Using out parameter To Calculate the Numpy Outer Product

In this example, we will be importing the numpy module as np. Then, we will take the input in a 1-D matrix. Then, we will specify the out parameter due to 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 the arrays as array1 and array2. Finally, we have applied the numpy outer() function without as the parameter and printed the output value in the result variable. The output is printed as arr1[0], which is multiplied with both the values of arr2, and arr1[1], which 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 computes 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 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 form a matrix, and the inner() we can see that each value multiplied with its same index and adds itself, then gives the output.

## Numpy outer() vs Numpy dot()

Numpy.outer() : The Numpy outer() function computes 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 applied the outer() and dot() and printed the output. In the note, I explained 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 needs of the project and program.

Subscribe
Notify of