Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23189

How to reverse a NumPy array using stride_tricks.as_strided

$
0
0

Is it possible to reverse an array using as_strided in NumPy? I tried the following and got garbage results

Note : I am aware of indexing tricks like ::-1, but what to know if this can be achieved thru ax_strided.

import numpy as npfrom numpy.lib.stride_tricks import as_stridedelems = 16inp_arr = np.arange(elems).astype(np.int8)print(inp_arr)print(inp_arr.shape)print(inp_arr.strides)expanded_input = as_strided(inp_arr[15],                            shape = inp_arr.shape,                            strides = (-1,))print(expanded_input)print(expanded_input.shape)print(expanded_input.strides)

Output

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]

(16,)

(1,)

[ 15 -113 0 21 -4 -119 -53 3 79 0 0 0 0 00 2]

(16,)

(-1,)


Viewing all articles
Browse latest Browse all 23189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>