Open In App

Compute the inner product of vectors for 1-D arrays using NumPy in Python

Last Updated : 29 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 evaluated

Returns: Inner Product of two arrays

Example 1:

Python3




# Importing library
import numpy as np
  
# Creating two 1-D arrays
array1 = np.array([6,2])
array2 = np.array([2,5])
  
print("Original 1-D arrays:")
print(array1)
print(array2)
  
# Output
print("Inner Product of the two array is:")
result = np.inner(array1, array2)
print(result)


Output:

Original 1-D arrays:
[6 2]
[2 5]
Inner Product of the two array is:
22

Example 2:

Python3




# Importing library
import numpy as np
  
# Creating two 1-D arrays
array1 = np.array([1,3,5])
array2 = np.array([0,1,5])
  
print("Original 1-D arrays:")
print(array1)
print(array2)
  
# Output
print("Inner Product of the two array is:")
result = np.inner(array1, array2)
print(result)


Output:

Original 1-D arrays:
[1 3 5]
[0 1 5]
Inner Product of the two array is:
28

Example 3:

Python3




# Importing library
import numpy as np
  
# Creating two 1-D arrays
array1 = np.array([1,2,2,8])
array2 = np.array([2,1,0,6])
  
print("Original 1-D arrays:")
print(array1)
print(array2)
  
# Output
print("Inner Product of the two array is:")
result = np.inner(array1, array2)
print(result)


Output:

Original 1-D arrays:
[1 2 2 8]
[2 1 0 6]
Inner Product of the two array is:
52


Similar Reads

Compute the outer product of two given vectors using NumPy in Python
In Python, we can use the outer() function of the NumPy package to find the outer product of two matrices. Syntax : numpy.outer(a, b, out = None) Parameters : a : [array_like] First input vector. Input is flattened if not already 1-dimensional. b : [array_like] Second input vector. Input is flattened if not already 1-dimensional. out : [ndarray, op
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) vectors. Let's see the examples: Example 1: Cross p
2 min read
Calculate inner, outer, and cross products of matrices and vectors using NumPy
Let's discuss how to find the inner, outer, and cross products of matrices and vectors using NumPy in Python. Inner Product of Vectors and Matrices To find the inner product of the vectors and matrices, we can use the inner() method of NumPy.Syntax: numpy.inner(arr1, arr2) Code : C/C++ Code # Python Program illustrating # numpy.inner() method impor
2 min read
Compute pearson product-moment correlation coefficients of two given NumPy arrays
In NumPy, We can compute pearson product-moment correlation coefficients of two given arrays with the help of numpy.corrcoef() function. In this function, we will pass arrays as a parameter and it will return the pearson product-moment correlation coefficients of two given arrays. Syntax: numpy.corrcoef(x, y=None, rowvar=True, bias=, ddof=) Return:
1 min read
Compute the Kronecker product of two multidimension NumPy arrays
Given an m X n matrix A and a p X q matrix B, their Kronecker product is A ⊗ B, also called their matrix direct product, is an (m*p) X (n*q) matrix. A = | (a00)  (a01) |  | (a10)  (a11) | B = | (b00)  (b01) |  | (b10)  (b11) | A ⊗ B = | (a00)*(b00)  (a00)*(b01)  (a01)*(b00)  (a01)*(b00) |         | (a00)*(b01)  (a00)*(b11)  (a01)*(b01)  (a01)*(b11)
2 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) = 30 Dot Product is the sum of the product of elements at each po
4 min read
numpy.inner() in python
numpy.inner(arr1, arr2): Computes the inner product of two arrays. Parameters : arr1, arr2 : array to be evaluated. Return: Inner product of the two arrays. Code #1 : # Python Program illustrating # numpy.inner() method import numpy as geek # Scalars product = geek.inner(5, 4) print("inner Product of scalar values : ", product) # 1D array
1 min read
How to Compute Pairwise Distance Between Two Vectors in PyTorch
In this article, we will discuss how to compute the pairwise distance between two vectors in PyTorch. The vector size should be the same and we can use PairwiseDistance() method to compute the pairwise distance between two vectors. PairwiseDistance() method PairwiseDistance() method computes the pairwise distance between two vectors using the p-nor
2 min read
Compute the covariance matrix of two given NumPy arrays
In NumPy for computing the covariance matrix of two given arrays with help of numpy.cov(). In this, we will pass the two arrays and it will return the covariance matrix of two given arrays. Syntax: numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None) Example 1: Python Code import numpy as np array1 = np.array([0, 1
2 min read
How to compute cross-correlation of two given NumPy arrays?
In the Numpy program, we can compute cross-correlation of two given arrays with the help of correlate(). In this first parameter and second parameter pass the given arrays it will return the cross-correlation of two given arrays. Syntax : numpy.correlate(a, v, mode = ‘valid’) Parameters :a, v : [array_like] Input sequences.mode : [{‘valid’, ‘same’,
1 min read
Article Tags :
Practice Tags :