0
0
NumPydata~5 mins

np.einsum() for efficient computation in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.einsum() do in numpy?

np.einsum() lets you write simple formulas to do many math operations on arrays efficiently, like sums, products, and transposes, all in one step.

Click to reveal answer
intermediate
How does np.einsum() improve performance compared to regular numpy operations?

It reduces extra steps and temporary arrays by combining operations, so calculations use less memory and run faster.

Click to reveal answer
beginner
Explain the meaning of the subscripts in np.einsum('ij,jk->ik', A, B).

'ij' means matrix A with rows i and columns j.<br>'jk' means matrix B with rows j and columns k.<br>'ik' means the output matrix with rows i and columns k.<br>This does matrix multiplication.

Click to reveal answer
intermediate
Can np.einsum() be used for operations like dot product, outer product, and trace? Give a simple example.

Yes! For example:<br>Dot product: np.einsum('i,i->', a, b)<br>Outer product: np.einsum('i,j->ij', a, b)<br>Trace: np.einsum('ii', A)

Click to reveal answer
beginner
Why might beginners find np.einsum() tricky at first?

Because it uses a special string notation to describe operations, which can look confusing until you practice reading and writing these formulas.

Click to reveal answer
What does np.einsum('ij,jk->ik', A, B) compute?
ASum of all elements in A and B
BMatrix multiplication of A and B
CElement-wise multiplication of A and B
DTranspose of matrix A
Which of these is a benefit of using np.einsum()?
AIt can combine multiple operations efficiently
BIt replaces Python loops with slower code
CIt only works on 1D arrays
DIt always uses more memory
What does the subscript 'ii' mean in np.einsum('ii', A)?
AOuter product of A with itself
BSum of all elements in A
CTranspose of A
DSum of diagonal elements (trace) of A
How would you use np.einsum() to compute the dot product of two vectors a and b?
A<code>np.einsum('i,i->', a, b)</code>
B<code>np.einsum('i,j->ij', a, b)</code>
C<code>np.einsum('ij,jk->ik', a, b)</code>
D<code>np.einsum('ii', a)</code>
Which is NOT true about np.einsum()?
AIt can perform tensor contractions
BIt requires understanding subscript notation
CIt only works with 2D arrays
DIt can reduce temporary arrays for speed
Describe how np.einsum() can be used to perform matrix multiplication. Include an example with explanation.
Think about how rows and columns match in matrix multiplication.
You got /4 concepts.
    Explain why np.einsum() can be more efficient than chaining multiple numpy operations.
    Consider what happens when you do many steps separately versus all at once.
    You got /4 concepts.