
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Get the Outer product of two multidimensional arrays in Python
To get the Outer product of two multi-dimensional arrays, use the numpy.outer() method in Python. The 1st parameter a is the first input vector. Input is flattened if not already 1-dimensional. The 2nd parameter b is the second input vector. Input is flattened if not already 1-dimensional. The 3rd parameter out is a location where the result is stored.
Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product [1] is −
[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]]
Steps
At first, import the required libraries −
import numpy as np
Creating two numpy Two-Dimensional array using the array() method −
arr1 = np.array([[5, 10], [15, 20]]) arr2 = np.array([[6, 12], [18, 24]])
Display the arrays −
print("Array1...\n",arr1) print("\nArray2...\n",arr2)
Check the Dimensions of both the arrays −
print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim)
Check the Shape of both the arrays −
print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape)
To get the Outer product of two multi-dimensional arrays, use the numpy.outer() method −
print("\nResult (Outer Product)...\n",np.outer(arr1, arr2))
Example
import numpy as np # Creating two numpy Two-Dimensional array using the array() method arr1 = np.array([[5, 10], [15, 20]]) arr2 = np.array([[6, 12], [18, 24]]) # Display the arrays print("Array1...\n",arr1) print("\nArray2...\n",arr2) # Check the Dimensions of both the arrays print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim) # Check the Shape of both the arrays print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape) # To get the Outer product of two multi-dimensional arrays, use the numpy.outer() method in Python print("\nResult (Outer Product)...\n",np.outer(arr1, arr2))
Output
Array1... [[ 5 10] [15 20]] Array2... [[ 6 12] [18 24]] Dimensions of Array1... 2 Dimensions of Array2... 2 Shape of Array1... (2, 2) Shape of Array2... (2, 2) Result (Outer Product)... [[ 30 60 90 120] [ 60 120 180 240] [ 90 180 270 360] [120 240 360 480]]
- Related Articles
- Get the Outer product of two arrays in Python
- Get the Outer product of two One-Dimensional arrays in Python
- Return the outer product of two masked arrays in Numpy
- Get the Kronecker product of two arrays in Python
- Get the Inner product of two arrays in Python
- Return the outer product of two masked Three-Dimensional Numpy arrays
- Return the outer product of two masked One-Dimensional Numpy arrays
- Get the Kronecker product of two One-Dimensional Arrays in Python
- Get the Inner product of two multi-dimensional arrays in Python
- Get the Inner product of two One-Dimensional arrays in Python
- Return the outer product of two masked arrays with different shapes in Numpy
- Return the dot product of two multidimensional vectors in Python
- Get the Kronecker product of two arrays with different dimensions in Python
- Get the Outer product of an array and a scalar in Python
- Get the Outer Product of an array with vector of letters in Python

Advertisements