Contiguous arrays store data in a continuous block of memory. This helps programs run faster. Stride tricks let you view the same data in different shapes without copying it.
Contiguous arrays and stride tricks in NumPy
import numpy as np # Creating a contiguous array array = np.array([1, 2, 3, 4, 5]) # Checking if array is contiguous is_contiguous = array.flags['C_CONTIGUOUS'] # Using stride tricks to create a sliding window view from numpy.lib.stride_tricks import sliding_window_view windowed_array = sliding_window_view(array, window_shape=3)
Contiguous means data is stored in one continuous block in memory.
Stride tricks create new views without copying data, so changes affect the original array.
import numpy as np # Empty array empty_array = np.array([]) print(empty_array.flags['C_CONTIGUOUS'])
import numpy as np # One element array one_element_array = np.array([10]) print(one_element_array.flags['C_CONTIGUOUS'])
import numpy as np # Non-contiguous array by slicing array = np.array([1, 2, 3, 4, 5]) sliced_array = array[::2] print(sliced_array.flags['C_CONTIGUOUS'])
import numpy as np from numpy.lib.stride_tricks import sliding_window_view array = np.array([1, 2, 3, 4, 5]) windowed = sliding_window_view(array, 2) print(windowed)
This program shows how to check if arrays are contiguous, how slicing can create non-contiguous arrays, and how stride tricks create sliding windows without copying data. It also shows that modifying the window view changes the original array.
import numpy as np from numpy.lib.stride_tricks import sliding_window_view # Create a 1D numpy array original_array = np.array([10, 20, 30, 40, 50]) # Check if the array is contiguous print(f"Is original array contiguous? {original_array.flags['C_CONTIGUOUS']}") # Create a sliced array with step 2 (non-contiguous) sliced_array = original_array[::2] print(f"Sliced array: {sliced_array}") print(f"Is sliced array contiguous? {sliced_array.flags['C_CONTIGUOUS']}") # Use stride tricks to create sliding windows of size 3 windows = sliding_window_view(original_array, window_shape=3) print(f"Sliding windows (size 3):\n{windows}") # Modify a value in the sliding window view windows[0, 0] = 999 print(f"Modified original array after changing window view:\n{original_array}")
Time complexity: Checking contiguity is O(1). Creating sliding windows is O(1) because it creates views, not copies.
Space complexity: Sliding windows use no extra space for data, only for metadata.
Common mistake: Assuming sliced arrays are always contiguous. Slicing with steps often creates non-contiguous arrays.
Use stride tricks when you want efficient views without copying data. Use copying if you need independent arrays.
Contiguous arrays store data in one continuous block for fast access.
Stride tricks create new views with different shapes without copying data.
Modifying views created by stride tricks changes the original array.
Practice
contiguous?Solution
Step 1: Understand contiguous memory
A contiguous array stores all its elements in one continuous block of memory, which helps with fast access and efficient computation.Step 2: Compare with other options
Options B, C, and D describe other properties unrelated to memory layout.Final Answer:
Its data is stored in one continuous block of memory. -> Option CQuick Check:
Contiguous = continuous memory block [OK]
- Confusing contiguous with array shape
- Thinking contiguous means sorted data
- Assuming contiguous means a copy
Solution
Step 1: Identify stride tricks usage
The functionnp.lib.stride_tricks.as_stridedcreates a view with a new shape and strides without copying data.Step 2: Check other options
Options B, C, and D create copies or flatten arrays, not views with stride tricks.Final Answer:
np.lib.stride_tricks.as_strided(arr, shape=(3,3), strides=(8,8)) -> Option DQuick Check:
Stride tricks use as_strided() [OK]
- Using copy() instead of as_strided()
- Flattening instead of changing strides
- Confusing reshape with stride tricks
import numpy as np arr = np.arange(6) view = np.lib.stride_tricks.as_strided(arr, shape=(3,), strides=(2*arr.itemsize,)) view[0] = 100 print(arr)
Solution
Step 1: Understand stride and view
The view selects elements with stride 2*itemsize, so it accesses arr[0], arr[2], arr[4]. Changing view[0] changes arr[0], changing view[1] changes arr[2], etc.Step 2: Analyze the assignment and print
view[0] = 100 sets arr[0] = 100. The print shows arr with arr[0] changed, arr[2] and arr[4] unchanged.Final Answer:
[100 1 2 3 4 5] -> Option BQuick Check:
Stride view modifies original array [OK]
- Assuming view is a copy, not a view
- Ignoring stride size effect
- Expecting all elements to change
import numpy as np arr = np.arange(4) view = np.lib.stride_tricks.as_strided(arr, shape=(3,3), strides=(8,8)) print(view)
Solution
Step 1: Check shape vs original array size
The original array has 4 elements, but the requested view shape is (3,3) = 9 elements, which is larger than original data.Step 2: Understand stride_tricks constraints
as_strided creates views without copying, so shape must fit within original data size. Here, it does not, causing invalid memory access.Final Answer:
The shape is incompatible with the original array size. -> Option AQuick Check:
View shape must fit original array size [OK]
- Ignoring array size vs view shape
- Assuming as_strided copies data
- Misunderstanding stride meaning
Solution
Step 1: Understand sliding window with stride tricks
Sliding window views use as_strided with shape=(number_of_windows, window_size) and strides that move one element at a time for rows and one element for columns.Step 2: Check options for correct usage
Usenp.lib.stride_tricks.as_stridedwith shape=(len(arr)-3, 4) and strides=(arr.itemsize, arr.itemsize). correctly sets shape and strides to create overlapping windows without copying. Options B and C reshape but do not create sliding windows. Usearr.flatten()and then slice manually. flattens but does not create windows.Final Answer:
Use np.lib.stride_tricks.as_strided with shape=(len(arr)-3, 4) and strides=(arr.itemsize, arr.itemsize). -> Option AQuick Check:
Sliding windows need as_strided with correct shape and strides [OK]
- Using reshape instead of stride tricks
- Copying data unnecessarily
- Incorrect stride values causing wrong windows
Ready to prove it?
Pass the test and this pattern moves unknown
