Re:A Visual Intro to NumPy and Data Representation
by improbable22 on Wednesday June 26, 2019 @02:35PM
(#20287833)
A related fun fact, when slicing several dimensions:
>>> a = np.arange(9).reshape(3,3) # a matrix
>>> a[0:3,0:3] # ranges are treated independently
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a[[0,1,2],[0,1,2]] # but arrays are treated at once
array([0, 4, 8])
Re:A Visual Intro to NumPy and Data Representation
by ovi256 on Wednesday June 26, 2019 @09:04AM
(#20284530)
A copy-on-write mechanism triggered by `y[0] = 3` would look the same and pass the test you devised, so you can't eliminate the possibility that it exists.
A better way would be to track memory use. A copy being created by either `y = x[[0, 2, 1]]` or `y[0] = 3` would show as a memory increase.
https://scipy-cookbook.readthedocs.io/items/ViewsVsCopies.ht...
You can verify there's a copy by changing the new array after putting the result in a new variable (see above link for why this makes a difference) and verifying the old one is unchanged:
Edit:But a view can be based on a slice that includes a skip parameter, and in fact you even slice in multiple dimensions and it will still be a view. That is worth discussing in the article: