Get the Inner product of two multi-dimensional arrays in Python



To get the Inner product of two multi-dimensional arrays, use the numpy.inner() method in Python. Ordinary inner product of vectors for 1-D arrays, in higher dimensions a sum product over the last axes. The parameters are 1 and b, two vectors. If a and b are nonscalar, their last dimensions must match.

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 Inner product of two multi-dimensional arrays, use the numpy.inner() method in Python. Ordinary inner product of vectors for 1-D arrays, in higher dimensions a sum product over the last axes −

print("\nResult (Inner Product)...\n",np.inner(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 Inner product of two multi-dimensional arrays, use the numpy.inner() method in Python print("\nResult (Inner Product)...\n",np.inner(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 (Inner Product)... [[150 330] [330 750]]
Updated on: 2022-02-25T06:34:04+05:30

630 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements