-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
DOC: Created an explanation document for copies and views #19791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did an initial pass, I think this will turn out very informative! I left a couple of suggestions.
Thank you for your comments @melissawm! I will make these improvements. |
076aad1
to
10738ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks really good, thanks @Mukulikaa !
@rossbar want to give a quick look? |
doc/source/user/basics.copies.rst
Outdated
How to tell if the array is a view or a copy | ||
============================================ | ||
|
||
The :attr:`base <.ndarray.base>` attribute of the ndarray makes it easy | ||
to tell if an array is a view or a copy. The base attribute of a view returns | ||
the original array while for a copy it returns ``None``. | ||
|
||
>>> x = np.arange(9) | ||
>>> x | ||
array([0, 1, 2, 3, 4, 5, 6, 7, 8]) | ||
>>> y = x.reshape(3, 3) | ||
>>> y | ||
array([[0, 1, 2], | ||
[3, 4, 5], | ||
[6, 7, 8]]) | ||
>>> y.base # .reshape() creates a view | ||
array([0, 1, 2, 3, 4, 5, 6, 7, 8]) | ||
>>> z = y[[2, 1]] | ||
>>> z | ||
array([[6, 7, 8], | ||
[3, 4, 5]]) | ||
>>> z.base is None # advanced indexing creates a copy | ||
True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't have to be for this PR, but I think it would be very useful to have a section above this that highlights what sort of operations create copies/views.
The example here has two nice demonstrations of this: reshaping and advanced indexing creating views/copies, respectively. IMO it would be beneficial to highlight these and explain why - in fact I think this should be the main emphasis of this document!
For example, I would move the indexing section and reshaping sections above this one.
This is mostly subjective though - these organizational ideas aren't blockers and we could handle them in followup PRs
Thanks for the suggestions @rossbar; I like them!
I wanted this to be the main focus too but I haven't found many resources to understand this concept. The ones I have read (links in the description above), mostly talk about indexing. Do you have any sources or maybe someone who could enlighten me? |
c752512
to
2b4563a
Compare
The rendered page is here. I rebased my branch but the build is still failing for some reason. |
Not sure why, but in any case it's not related to your work here. |
Thanks @Mukulikaa. The page looks great. If any tweaks are needed they can be done in follow-on PRs. |
This is an attempt to address issue #15793 by creating an explanation document about copies and views in NumPy. I would appreciate suggestions and feedback on how to do justice to this topic and any additional resources. The references specific to this topic that I found useful are:
cc: @melissawm @rossbar