Calculate Numpy inner product in Python with Examples

Calculate Numpy inner product in Python with Examples

Popular Domains for just 99 Cents at Namecheap!

The inner product is just like same as the dot product.  You can multiply only finite vectors in dot product but in the case of inner product, you can multiple infinite vectors. In NumPy, there are many functions to manipulate the NumPy array. The function numpy.inner() calculate the inner product of two vectors in space. In this tutorial, you will know how to find the inner product in python using NumPy with various examples.

 

Examples of Numpy inner Product

Example 1: Numpy inner product on two vectors in one dimension

Let’s create two vectors of a single dimension.  You can create a NumPy array using the numpy.array() method. Let’s create it.

array1 = np.array([10,20,30])
array2= np.array([2,3,4])

After the creation, you have to pass it as an argument inside the numpy.array() method. Execute the below lines of code.

import numpy as np
array1 = np.array([10,20,30])
array2= np.array([2,3,4])
print(np.inner(array1,array2))

Output

Inner product of two 1D numpy array
The inner product of two 1D NumPy array

You can see the there is scalar output after doing the inner product on two single dimension vectors.

 

Example 2: Inner product on two vectors in Multi dimension

Now let’s find the inner product on two multi-dimensional arrays. The first one will be a two-dimensional array and the second one is a single dimensional array. Let’s create both of them.

array1 = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2= np.array([2,3,4]

Execute the below code to find the inner product.

import numpy as np
array1 = np.array([[10,20,30],[40,50,60],[70,80,90]])
array2= np.array([2,3,4])
print(np.inner(array1,array2))

Output

Inner product one multi and one single dimensional array
Inner product one multi and one single dimensional array

The output can be a single or multi-dimensional array that will depend on input values.

 

Example 3: Inner product on one multi-dimensional array and a scalar value

In the last example, let’s find the inner product on a two-dimensional array and a scalar value. Let’s create a two-dimensional array.

array1 = np.array([[10,20,30],[40,50,60]])

After creating it, now I want to perform the inner product of the above array with 11 as a scalar value.

Execute the below lines of code.

import numpy as np
array1 = np.array([[10,20,30],[40,50,60]])
print(np.inner(array1,11))

Output

Inner product of one multi array with scalar value
The inner product of one multi-array with a scalar value

The output array will be of the same shape as the input multi-dimensional array.

 

That’s all for now. These are examples of how to calculate the inner product on two vectors. Hope these examples have cleared your all queries on it. Even if you have any doubt then you can contact us for more help.

Source:

Numpy Inner Product Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner