import numpy as np arr = np.arange(10) sliced = arr[2:8] print(sliced.flags['C_CONTIGUOUS'], sliced.flags['F_CONTIGUOUS'])
The sliced array is a view of the original 1D array. For 1D arrays, both C_CONTIGUOUS and F_CONTIGUOUS flags are True since there is no distinction between row- and column-major order.
import numpy as np arr = np.arange(12) reshaped = arr.reshape((3,4)) print(reshaped.shape, reshaped.strides)
The array has shape (3,4). The stride for the first dimension is 4 elements * 8 bytes = 32 bytes, and for the second dimension is 8 bytes (one element).
import numpy as np from numpy.lib.stride_tricks import as_strided arr = np.arange(5) window = as_strided(arr, shape=(3,3), strides=(8,8)) print(window)
The as_strided function creates a view with given shape and strides without bounds checking. Here, the maximum memory offset is 32 bytes (within the 40-byte buffer), so no error occurs. It prints [[0 1 2] [1 2 3] [2 3 4]].
import numpy as np arr = np.array([[1,2,3],[4,5,6]], dtype=np.int64) print(arr.strides)
Strides (24,8) mean to move 24 bytes to go to next row (3 elements * 8 bytes) and 8 bytes to go to next column. This matches row-major order.
import numpy as np from numpy.lib.stride_tricks import as_strided arr = np.arange(5)
To create sliding windows of size 3 over a 1D array of length 5, the shape should be (3,3) and strides (8,8) to move one element at a time for both dimensions. This produces [[0,1,2], [1,2,3], [2,3,4]]. Option B does this.