0

I have some questions about how np.lib.stride_tricks.as_strided differs from just using np.ndarray and if the as_strided can be intercepted by sub-classes to np.ndarray.

Given an array:

a = np.arange(10, dtype=np.int8)

Is there any difference (effectively) between using as_strided and creating the ndarray with the appropriate strides directly?

np.lib.stride_tricks.as_strided(a, (9,2), (1,1))
np.ndarray((9,2), np.int8, buffer=a.data, strides=(1,1))

as_strided just seems to be more implicit about what it is doing. Can either function be intercepted by a sub-classes for np.ndarray? If a in the example above was a custom subclass of ndarray will there be some function that gets called before returning the strided view? I know that setting subok for as_strided will preserve any subclass, but is this actually doing anything else than casting the array?

Kevin
  • 3,096
  • 2
  • 8
  • 37
  • `as_strided` is python code which you can study. – hpaulj May 10 '21 at 00:33
  • Ok, [this seems to be it](https://github.com/numpy/numpy/blob/38bdff05da0859f45bebf5a0c32869d58365b02f/numpy/lib/stride_tricks.py#L96) and it looks like it does the same as using ```np.ndarray```. I thought at first it would be similar to ```np.reshape``` since both methods are just changing metadata. – Kevin May 10 '21 at 01:11

0 Answers0