Get the Outer Product of an array with vector of letters using NumPy in Python
Last Updated :
28 Jun, 2022
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]]
Similar Reads
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Vector outer product with Einstein summation convention using NumPy in Python In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read
Make grid for computing a Mandelbrot set with outer product using NumPy in Python In this article let's learn how to make a grid for computing a Mandelbrot set with an outer product in Numpy using Python. numpy.outer() method: In Python, the numpy.outer() method is used to acquire the outer product of an array and a vector of elements. In linear algebra, a matrix is the outer pr
4 min read
Python Program to Get dot product of multidimensional Vectors using NumPy Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy.Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1]Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30Dot Product i
3 min read