For 1D arrays, np.inner(a, b) is identical to np.dot(a, b) and the plain vector dot product. Where it diverges is for higher-dimensional inputs: np.inner() sums over the last axis of both a and b, treating each 'row' along that axis independently and producing a result whose shape is the combination of the leading dimensions of both a and b — which is a genuinely different generalization from np.dot()'s rule, making the two functions agree only for the simple 1D case.
1Understanding np.inner()
For 1D arrays, np.inner(a, b) is identical to np.dot(a, b) and the plain vector dot product. Where it diverges is for higher-dimensional inputs: np.inner() sums over the last axis of both a and b, treating each 'row' along that axis independently and producing a result whose shape is the combination of the leading dimensions of both a and b — which is a genuinely different generalization from np.dot()'s rule, making the two functions agree only for the simple 1D case.
np.inner() and np.dot() agree exactly for 1D vectors, but diverge for higher-dimensional arrays — don't assume they're interchangeable beyond the simple vector case without checking the specific shape rules for your use case.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))2Practical Example
Here is a real-world application of np.inner() showing how it is used in production NumPy code.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.inner(a, b))3Best Practices
Follow these guidelines when working with np.inner():
1. Use np.inner() for the simple vector dot-product case where it's equivalent to np.dot(), for whichever reads more naturally in context
2. Test and verify the exact resulting shape when using np.inner() on arrays with more than 1 dimension, since its generalization differs from np.dot()'s
3. Reach for np.tensordot() instead of either dot() or inner() when you need explicit control over which specific axes are summed over in a higher-dimensional contraction
Tip: np.inner() and np.dot() agree exactly for 1D vectors, but diverge for higher-dimensional arrays — don't assume they're interchangeable beyond the simple vector case without checking the specific shape rules for your use case.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))