AppDividend
Latest Code Tutorials

# Numpy outer: How to Calculate Product of Vectors in Python

Numpy outer() is used to calculate the outer product of two given vectors. Now the question is what is the outer product? Suppose we have two vectors A [ a,a1,a2,..an] and B [ b0,b1,b2,…bn], the outer product of these two vectors will be:

[ [ a0*b0 a0*b1 a0*b2 … a0*bn]

[ a1*b0 a0*b1 a1*b2 … a1*bn]

[ ……………………………….]

## Numpy.outer()

To calculate the outer product of two vectors in Python, use the numpy outer() function.

### Syntax

```numpy.outer(arr1, arr2, out = None)
```

### Parameters

The outer() function takes two main parameters, which are:

1. arr1: This is the first array.
2. arr2: This is the second array.

Also, there is one optional parameter :

1. out: This is the location where the result is stored.

### Return Value

The outer() function returns a vector that contains the outer product of the given vectors.

### Programming Example

#### Program to find outer() of two numeric vectors

```# Program to find outer() of two numeric vectors
import numpy as np

# Declaring the first array
arr1 = np.array([-2, -1, 0, 1, 2, 3, 4, 5])
arr2 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

print("First array is :\n", arr1)
print("Second array is :\n", arr2)

# Calculating the outer product
ans = np.outer(arr1, arr2)

print("Outer Product of these vectors are:\n", ans)```

#### Output

```First array is :
[-2 -1  0  1  2  3  4  5]
Second array is :
[0 1 2 3 4 5 6 7]
Outer Product of these vectors are:
[[  0  -2  -4  -6  -8 -10 -12 -14]
[  0  -1  -2  -3  -4  -5  -6  -7]
[  0   0   0   0   0   0   0   0]
[  0   1   2   3   4   5   6   7]
[  0   2   4   6   8  10  12  14]
[  0   3   6   9  12  15  18  21]
[  0   4   8  12  16  20  24  28]
[  0   5  10  15  20  25  30  35]]```

#### Explanation

First, we have created two arrays. Then we have printed those two arrays. Then we have called numpy.outer() to get the outer vector product. The output is produced using the np.outer() method.

### Find the product of characters using an outer() method

What if we take two arrays and one has characters, and one has integers.

```# Program to find outer() when the given product has characters:
import numpy as np

# Declaring the first array
arr1 = np.array(['a', 'b', 'c', 'd'], dtype=object)
arr2 = np.array([1, 2, 3, 4])

print("First array is :\n", arr1)
print("Second array is :\n", arr2)

# Calculating the outer product
ans = np.outer(arr1, arr2)

print("Outer Product of these vectors are:\n", ans)
```

#### Output

```First array is :
['a' 'b' 'c' 'd']
Second array is :
[1 2 3 4]
Outer Product of these vectors are:
[['a' 'aa' 'aaa' 'aaaa']
['b' 'bb' 'bbb' 'bbbb']
['c' 'cc' 'ccc' 'cccc']
['d' 'dd' 'ddd' 'dddd']]```

#### Explanation

In this example, we have first created a vector of character type and one vector which contains numeric values. Now we want to calculate the outer vector product of these two different variable type vectors.

When we call this, we can see a different result. As per the string rule, when we multiply any character/string with any number, we get that many copies of that character or string. As we can see, a*1=a, a*2=aa, and so on.

That is it for the numpy outer() method.