🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.inner()

AI & DATA SCIENCE // np-inner

np.inner() computes a sum-product over the last axis of two arrays — the standard dot product for 1D vectors, but generalized differently from np.dot() for higher-dimensional arrays.

Syntax

np.inner(a, b)

Deep Dive Course

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.inner() showing how it is used in production NumPy code.

editor.html
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.inner(a, b))
localhost:3000

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))
Example 02Advanced Example
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.inner(a, b))

Best Practices

  • Use np.inner() for the simple vector dot-product case where it's equivalent to np.dot(), for whichever reads more naturally in context
  • 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
  • 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

Interview Question

For 1D arrays, why do np.inner() and np.dot() always produce the same result?

Hint: Think about what each function's general rule reduces to when there's only one axis available.

Both functions' general definitions involve summing products over some axis, and for a 1D array, there's only one axis to sum over in the first place — there's no ambiguity or difference in generalization to worry about, since neither function has extra dimensions to handle differently. It's specifically once arrays gain additional dimensions that np.dot() and np.inner() apply their respectively different rules for how those extra dimensions combine, which is where their results start to diverge.

Exercises

MediumPractice using np.inner() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.inner(a, b))

Frequently Asked Questions

For 1D arrays, why do np.inner() and np.dot() always produce the same result?

Both functions' general definitions involve summing products over some axis, and for a 1D array, there's only one axis to sum over in the first place — there's no ambiguity or difference in generalization to worry about, since neither function has extra dimensions to handle differently. It's specifically once arrays gain additional dimensions that np.dot() and np.inner() apply their respectively different rules for how those extra dimensions combine, which is where their results start to diverge.

Related Functions

np-dotnp-outernp-vdot