Slicing a numpy.ndarray
results in a view of the data:
a=np.linspace(0,5,6)
b=a[1:5]
b[0]+=10
a
returns array([ 0., 11., 2., 3., 4., 5.])
.
From b
, is it possible to recover the properties and data of a?
I.e., something like b.viewed_array is a
would return true, or at least b.viewed_array.shape == a.shape and (b.viewed_array==a).all()
is true?
Yes you can use base method to extract the original vector. For instance, in this case
a = np.linspace(0, 5, 6)
b = a[1:5]
# Using the base attribute
original_array = b.base
You can verify original_array is actually a by running the following
print(original_array is a or (a.base is not None and original_array is a.base)) # Should print True
print((original_array == a).all()) #Should print True