Get the Inner product of two arrays in Python

The inner product of two arrays is computed using NumPy's inner() method. For 1-D arrays, it calculates the ordinary inner product of vectors. For higher dimensions, it performs a sum product over the last axes.

Syntax

numpy.inner(a, b)

Parameters:

  • a, b ? Input arrays. If non-scalar, their last dimensions must match

Basic Inner Product Example

Let's calculate the inner product of two 1-D arrays ?

import numpy as np

# Create two 1-D arrays
arr1 = np.array([5, 10, 15])
arr2 = np.array([20, 25, 30])

print("Array1:", arr1)
print("Array2:", arr2)

# Calculate inner product
result = np.inner(arr1, arr2)
print("Inner Product:", result)
Array1: [ 5 10 15]
Array2: [20 25 30]
Inner Product: 800

How It Works

The inner product calculation: (5×20) + (10×25) + (15×30) = 100 + 250 + 450 = 800

2-D Array Inner Product

For 2-D arrays, inner() performs sum product over the last axes ?

import numpy as np

# Create two 2-D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

print("Array1:")
print(arr1)
print("\nArray2:")
print(arr2)

# Calculate inner product
result = np.inner(arr1, arr2)
print("\nInner Product:")
print(result)
Array1:
[[1 2]
 [3 4]]

Array2:
[[5 6]
 [7 8]]

Inner Product:
[[17 23]
 [39 53]]

Different Data Types

The inner product works with different numeric data types ?

import numpy as np

# Integer and float arrays
int_array = np.array([1, 2, 3])
float_array = np.array([1.5, 2.5, 3.5])

result = np.inner(int_array, float_array)
print("Inner product (int × float):", result)
print("Result type:", type(result))
Inner product (int × float): 17.0
Result type: <class 'numpy.float64'>

Comparison with Other Methods

Method Purpose 1-D Arrays Result
np.inner() Inner product Scalar (sum of element-wise products)
np.dot() Dot product Same as inner for 1-D
np.outer() Outer product 2-D array of all combinations

Conclusion

Use numpy.inner() to compute the inner product of arrays. For 1-D arrays, it returns the sum of element-wise products. The method handles different data types and works efficiently with multi-dimensional arrays.

Updated on: 2026-03-26T19:57:21+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements