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.

pythonnumpydata-science

See Also