0
0
NumPydata~10 mins

np.einsum() for efficient computation in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.einsum() for efficient computation
Input Arrays
Define Subscripts
np.einsum() Parses Subscripts
Perform Summation/Product
Return Result Array
np.einsum takes input arrays and a subscript string to specify element-wise operations and summations, then computes the result efficiently.
Execution Sample
NumPy
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
result = np.einsum('ij,ij->ij', A, B)
Computes element-wise multiplication of two 2x2 arrays using np.einsum.
Execution Table
StepSubscript ParsingOperationIntermediate ResultOutput
1'ij,ij->ij'Parse indices: i,j for both arrays, output i,jArrays ready for element-wise multiplyNone yet
2'ij,ij->ij'Element-wise multiplication[[5,12],[21,32]]None yet
3No summation since output is 'ij'Keep shape (2,2)[[5,12],[21,32]][[5,12],[21,32]]
💡 Completed element-wise multiplication with output shape (2,2) as specified by 'ij' in subscript.
Variable Tracker
VariableStartAfter Step 2Final
A[[1,2],[3,4]][[1,2],[3,4]][[1,2],[3,4]]
B[[5,6],[7,8]][[5,6],[7,8]][[5,6],[7,8]]
resultNone[Intermediate: element-wise multiply][[5,12],[21,32]]
Key Moments - 2 Insights
Why does the output keep the same shape as inputs?
Because the subscript 'ij,ij->ij' tells np.einsum to multiply elements with matching indices and keep the indices 'ij' in output, so no summation happens (see execution_table step 3).
What if we change the output subscript to 'i'?
np.einsum will sum over the 'j' index, reducing the output to 1D array with shape matching 'i'. This is a summation step different from element-wise multiply (not shown in current table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the intermediate result after step 2?
A[[1,2],[3,4]]
B[[6,8],[10,12]]
C[[5,12],[21,32]]
DNone
💡 Hint
Check the 'Intermediate Result' column at step 2 in execution_table.
At which step does np.einsum parse the subscripts?
AStep 1
BStep 2
CStep 3
DNo parsing step
💡 Hint
Look at the 'Subscript Parsing' column in execution_table.
If the output subscript changes to 'i', what happens to the output shape?
AOutput shape becomes (2,2)
BOutput shape becomes (2,)
COutput shape becomes (1,2)
DOutput shape becomes scalar
💡 Hint
Refer to key_moments explanation about summation over 'j' index.
Concept Snapshot
np.einsum(subscripts, *arrays) computes sums/products using index notation.
Subscripts specify input and output indices.
Matching indices are multiplied element-wise.
Indices not in output are summed over.
Efficient for complex array operations without loops.
Full Transcript
np.einsum is a powerful numpy function that lets you specify how to multiply and sum elements of arrays using index labels. You write a string like 'ij,ij->ij' to tell it to multiply elements with matching i and j indices and keep the output shape the same. The function parses this string, multiplies elements accordingly, and sums over indices not in the output. This example showed element-wise multiplication of two 2x2 arrays, resulting in a 2x2 output. Changing the output indices can cause summation and change the output shape. This method is efficient and avoids explicit loops.