0
0
NumPydata~5 mins

np.take() and np.put() for advanced selection in NumPy - Cheat Sheet & Quick Revision

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

np.take() selects elements from an array at specified indices. It is like picking items from a list using a list of positions.

Click to reveal answer
beginner
How does np.put() work in NumPy?

np.put() replaces elements in an array at given indices with new values. Think of it as putting new items into specific spots in a list.

Click to reveal answer
intermediate
What is the difference between np.take() and simple indexing like arr[indices]?

np.take() can handle multi-dimensional arrays with an axis argument, allowing selection along a specific axis. Simple indexing selects elements but may not support advanced axis selection.

Click to reveal answer
intermediate
Can np.put() modify elements along a specific axis?

No, np.put() works on the flattened version of the array by default. To modify along an axis, you need to reshape or use other methods.

Click to reveal answer
advanced
What happens if you use np.put() with indices outside the array size?

Indices are taken modulo the array size, so they wrap around. This means out-of-range indices loop back to valid positions.

Click to reveal answer
What does np.take(arr, indices) return?
AElements from <code>arr</code> at positions in <code>indices</code>
BA new array with elements replaced at <code>indices</code>
CThe sum of elements at <code>indices</code>
DThe shape of <code>arr</code>
Which function would you use to replace elements in an array at specific positions?
Anp.take()
Bnp.put()
Cnp.sum()
Dnp.reshape()
What does np.put(arr, [0, 2], [10, 20]) do?
ASelects elements at positions 0 and 2
BDeletes elements at positions 0 and 2
CReturns the sum of elements at positions 0 and 2
DReplaces elements at positions 0 and 2 with 10 and 20
If you want to select elements along a specific axis, which function is better?
Anp.put()
Bnp.flatten()
Cnp.take() with axis argument
Dnp.sum()
What happens if np.put() receives an index larger than the array size?
AWraps around using modulo
BRaises an error
CIgnores the index
DDeletes the last element
Explain how np.take() can be used to select elements from a 2D array along a specific axis.
Think about how you pick rows or columns by specifying the axis.
You got /4 concepts.
    Describe the behavior of np.put() when given indices and values, including what happens with out-of-range indices.
    Consider how the function modifies the array and handles indices beyond its size.
    You got /4 concepts.