# Setup
import numpy as np
= np.arange(12).reshape(3, 4)
X = X[:2]
A = X[2:] B
Being Naughty with Numpy
Fun
Tee-hee
display(A) display(B)
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
array([[ 8, 9, 10, 11]])
# Depends only on A
np.lib.stride_tricks.as_strided(
A,=(12,),
shape=(A.itemsize,)
strides-1] = -1
)[
# A hasn't changed
display(A)
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
# Spooky!
display(B)
array([[ 8, 9, 10, -1]])
Can you figure out why?
Hint: How does the NumPy memory model work?