Open In App

Get the Outer Product of an array with vector of letters using NumPy in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article let’s see how to get the outer product of an array with a vector of letters in Python.

numpy.outer() method

The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear algebra. The outer product of two vectors with dimensions of n and m is the m*n matrix. In general, the outer product of two tensors (multidimensional arrays of numbers) is a tensor. Tensor algebra is defined by the tensor product, often known as the outer product of tensors. So, to put it another way, the outer product is the product of all the elements of the first vector with all the elements of the second vector.

Example:

Input: a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN]

Output:

[[a0*b0  a0*b1 ... a0*bN ]
[a1*b0   a1*b1 ... a0*bN ]
[ ...    ...        ...  ]  
[aM*b0            aM*bN ]]

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

Parameters: 

  • a: (M,) array_like object. The initial input vector. If the input is not already 1-dimensional, it is flattened.
  • b: (N,) array_like object. Second vector of input. If the input is not already 1-dimensional, it is flattened.
  • out: (M, N) ndarray, optional value. The location where the outcome is saved

Return: out (M, N) ndarray. result is out[i, j] = a[i] * b[j].

Example 1

Here, we will create two NumPy vectors using np.array() method one which is a vector of letters and another one is a vector of numbers. The .ndim attribute is used to know the dimensions of the array, .shape attribute is used to find the shape of the vector. np.outer() method is used to find the outer product of the vectors created. now if we check the first line of output, it is [g*1, g*2, g*3, g*4], same goes with every other element in the letters vector.

Python3




# importing packages
import numpy as np
 
# creating arrays using np.array() method
#  vector of letters
arr1 = np.array(['g', 'e', 'e', 'k'], dtype=object)
# #  integer array
arr2 = np.array([1, 2, 3, 4])
#
# # Display the arrays
print("Array of letters is :", arr1)
print("Array of numbers is :", arr2)
#
# # Checking the dimensions
print("Array one dimension :", arr1.ndim)
print("Array two dimension", arr2.ndim)
#
# # Checking the shape of the arrays
print("Shape of array 1 is : ", arr1.shape)
print("Shape of array 2 is : ", arr2.shape)
 
# # outer product of the vectors
print("Outer product : \n", np.outer(arr1, arr2))


Output:

Array of letters is : ['g' 'e' 'e' 'k']
Array of numbers is : [1 2 3 4]
Array one dimension : 1
Array two dimension 1
Shape of array 1 is :  (4,)
Shape of array 2 is :  (4,)
Outer product : 
 [['g' 'gg' 'ggg' 'gggg']
 ['e' 'ee' 'eee' 'eeee']
 ['e' 'ee' 'eee' 'eeee']
 ['k' 'kk' 'kkk' 'kkkk']]

Example 2

In this example, we are creating an array of integers to find the outer product of the vectors created. now if we check the first line of output, it is [g*1, g*2, g*3, g*4], same goes with every other element in the letters vector.

Python3




# importing packages
import numpy as np
 
# creating arrays using np.array() method
#  vector of letters
arr1 = np.array([5, 6, 7, 8], dtype=object)
# #  integer array
arr2 = np.array([1, 2, 3, 4])
#
# # Display the arrays
print("Array of letters is :", arr1)
print("Array of numbers is :", arr2)
#
# # Checking the dimensions
print("Array one dimension :", arr1.ndim)
print("Array two dimension", arr2.ndim)
#
# # Checking the shape of the arrays
print("Shape of array 1 is : ", arr1.shape)
print("Shape of array 2 is : ", arr2.shape)
 
# # outer product of the vectors
print("Outer product : \n", np.outer(arr1, arr2))


Output:

Array of letters is : [5 6 7 8]
Array of numbers is : [1 2 3 4]
Array one dimension : 1
Array two dimension 1
Shape of array 1 is :  (4,)
Shape of array 2 is :  (4,)
Outer product : 
 [[5 10 15 20]
 [6 12 18 24]
 [7 14 21 28]
 [8 16 24 32]]


Last Updated : 28 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads