NumPy Einsum — ELI5
Imagine you are at a restaurant and the menu has 50 dishes. But there is also a secret “build your own” option where you describe exactly what you want — ingredients, cooking method, plating — in a single sentence. That is np.einsum. It is one function that can do dozens of different array operations depending on a tiny instruction string you give it.
The instruction string uses letters. Each letter represents a dimension (like rows and columns). You write which dimensions go in and which come out, and NumPy figures out the rest.
Want to sum all elements? Easy. Want to multiply two matrices? Also easy. Transpose? Diagonal? Trace? Batch dot product? All one function, different instruction strings.
For example, 'ij,jk->ik' says: “take something with rows i and columns j, combine it with something that has rows j and columns k, and give me back rows i and columns k.” That is matrix multiplication — described in eight characters.
People find einsum scary at first because the notation looks alien. But once it clicks — and it usually takes one afternoon of practice — you start seeing patterns everywhere. It is like learning to read sheet music: weird at first, then incredibly powerful.
The one thing to remember: einsum is a universal remote for array math — one function, one short string, and NumPy handles the rest.
See Also
- Python Bokeh Get an intuitive feel for Bokeh so Python behavior stops feeling unpredictable.
- Python Numpy Advanced Indexing How to cherry-pick exactly the data you want from a NumPy array using lists, masks, and fancy tricks.
- Python Numpy Broadcasting Rules How NumPy magically makes different-sized arrays work together without you writing any loops.
- Python Numpy Fft Spectral How NumPy breaks apart a signal into its hidden frequencies — like separating a chord into individual notes.
- Python Numpy Memory Views Why NumPy arrays can share the same data without copying it — and how that makes your code fast but occasionally surprising.