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
How to get the indices of the sorted array using NumPy in Python?
We can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that would sort the arra
2 min read
Calculate the outer product of two vectors using R
In this article, we will explore different ways to Calculate the outer product of two vectors using R Programming Language. What is the outer product of two vectorsthe outer product of two vectors is a matrix where each element is the product of a corresponding element from the first vector with a c
3 min read
Return the Norm of the vector over given axis in Linear Algebra using NumPy in Python
In this article, we will how to return the Norm of the vector over a given axis in Linear Algebra in Python. numpy.linalg.norm() method The numpy.linalg.norm() method is used to return the Norm of the vector over a given axis in Linear algebra in Python. Depending on the value of the ord parameter,
3 min read
How to compute the cross product of two given vectors using NumPy?
Let's see the program to compute the cross product of two given vectors using NumPy. For finding the cross product of two given vectors we are using numpy.cross() function of NumPy library.Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[Return: cross product of two (arrays of) vec
1 min read
How to split the element of a given NumPy array with spaces?
To split the elements of a given array with spaces we will use numpy.char.split(). It is a function for doing string operations in NumPy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr. Parameters:arr : array_like of str or unicode.Input array
2 min read