0
0
NumPydata~5 mins

Contiguous arrays and stride tricks in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean for a NumPy array to be contiguous?
A contiguous NumPy array stores its data in a single, continuous block of memory. This means the elements are laid out one after another without gaps, which makes operations faster and memory access efficient.
Click to reveal answer
intermediate
What is the purpose of stride tricks in NumPy?
Stride tricks let you create new views of an array by changing how NumPy steps through the data in memory, without copying it. This helps to create sliding windows or reshape data efficiently.
Click to reveal answer
beginner
How can you check if a NumPy array is contiguous?
You can check if an array is contiguous by using the attributes arr.flags['C_CONTIGUOUS'] or arr.flags['F_CONTIGUOUS']. C_CONTIGUOUS means row-major order, and F_CONTIGUOUS means column-major order.
Click to reveal answer
advanced
What happens if you use stride tricks incorrectly?
Using stride tricks incorrectly can lead to unexpected results or even crashes because you might access memory outside the array bounds. It’s important to ensure the new view respects the original data layout.
Click to reveal answer
beginner
Explain the difference between a view and a copy in NumPy.
A view shares the same data buffer as the original array but may have a different shape or strides. Changes to the view affect the original array. A copy creates a new array with its own data, so changes do not affect the original.
Click to reveal answer
Which attribute tells you if a NumPy array is stored in a continuous block of memory in row-major order?
Aarr.shape
Barr.strides
Carr.flags['C_CONTIGUOUS']
Darr.flags['F_CONTIGUOUS']
What does the stride of a NumPy array represent?
AThe total number of elements in the array
BThe number of bytes to step in each dimension when traversing the array
CThe shape of the array
DThe data type of the array elements
What is a key benefit of using stride tricks in NumPy?
ACreating new views without copying data
BChanging the data type of an array
CCreating new arrays with copied data
DSorting array elements
If an array is not contiguous, what might happen when performing operations?
AOperations might be slower due to non-contiguous memory access
BThe array will automatically become contiguous
CThe array data will be lost
DThe array shape will change
Which function in NumPy can be used to create a sliding window view using stride tricks?
Anumpy.concatenate
Bnumpy.copy
Cnumpy.reshape
Dnumpy.lib.stride_tricks.sliding_window_view
Describe what contiguous arrays are in NumPy and why they matter for performance.
Think about how data is stored in memory and how that affects speed.
You got /4 concepts.
    Explain how stride tricks work and give an example of when you might use them.
    Consider how you can look at the same data in different shapes without copying.
    You got /4 concepts.