Inner Product of Numpy Arrays – A Quick Guide

Inner Product Numpy

In this article, we will learn how to perform an inner product between two arrays. We will look at both 1-D arrays and multi-dimensional arrays. Let’s start by looking at what are Numpy arrays.

What are NumPy arrays?

Numpy is an open-source python library that is used for scientific computations. Numpy arrays are similar to lists except that it contains objects of similar data types and is much faster than lists.

They are one of the most important data structures in Python for scientific computing. A numpy array is efficient, versatile, and easy to use. They’re also multidimensional, meaning they can store data in more than one dimension. The number of dimensions is called the rank of the array. Arrays can have any rank, but most arrays have either one or two dimensions.

Let’s see how to create a Numpy array.

import numpy as np
a=np.array([1,2,3])
print (a)

Output

[1 2 3]

Inner Product on Numpy Arrays

We can perform the inner product of arrays with the help of a simple numpy.inner() function.

Syntax:

numpy.inner(arr1, arr2)=sum(array1[:] , array2[:])

Inner Product of 1-D Numpy Arrays

You can use the following code for the 1-D inner product of Numpy arrays.

import numpy as np 
a= np.array([1,2,3])
b= np.array([0,1,0])
product=np.inner(a,b) 
print(product)

Output

2

The output product here equates to [1*0+2*1+3*0]=2

Inner Product of Multi-Dimensional Arrays

You can use the following code for the multi-dimensional arrays.

import numpy as np 
a = np.array([[1,3], [4,5]]) 
b = np.array([[11, 12], [15, 16]]) 

product=np.inner(a,b)
print(product)

Output

[[ 47  63]
 [104 140]]

Conclusion

In summary, we learned how to perform the inner product on Numpy arrays. Hope you found this article useful!