user40314 - 9 months ago 55

Python Question

Simple slices form views into the parent array. The strides of the view is generically the multiple of the strides of the parent array.

Given 2d parent array with strides

`(s0, s1)`

`(s0+s1)`

Is there a way to create such a view in top-level Python/numpy? Thank you in advance.

Answer

With `as_strided`

I can do what you want:

```
In [298]: X=np.eye(5)
In [299]: X.strides
Out[299]: (40, 8)
In [300]: np.lib.stride_tricks.as_strided(X,shape=(5,),strides=(48,))
Out[300]: array([ 1., 1., 1., 1., 1.])
```

though some would argue the `as_strided`

is a step closer to the 'guts' than most of `numpy`

Python code.

Source (Stackoverflow)