Will the results of numpy.lib.stride_tricks.as_strided
depend on the dtype of the NumPy array?
This question arises from the definition of .strides
, which is
Tuple of bytes to step in each dimension when traversing an array.
Take the following function that I've used in other questions here. It takes a 1d or 2d array and creates overlapping windows of length window
. The result will one dimension greater than the input.
def rwindows(a, window):
if a.ndim == 1:
a = a.reshape(-1, 1)
shape = a.shape[0] - window + 1, window, a.shape[-1]
strides = (a.strides[0],) + a.strides
windows = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
return np.squeeze(windows)
# examples
# rwindows(np.arange(5), window=2)
# rwindows(np.arange(20).reshape((5,4)), window=2)
Because of the definition of strides and because, for instance, otherwise equivalent arrays of dtype float32
and float64
will have different strides, will this ever blow up my rwindows
function above?
I've tried to test but it's been in a non-exhaustive way and am looking for an answer that (1) explains whether the disclaimer/warning from the function doc has anything to do with what I'm asking here and (2) explains why or why not otherwise-equivalent arrays with different dtypes & strides would yield different results in the above.