Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 5 commits into from
Oct 28, 2021

Conversation

Mukulikaa
Copy link
Contributor

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

@rossbar rossbar self-assigned this Sep 9, 2021
Copy link
Member

@melissawm melissawm left a 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.

@Mukulikaa
Copy link
Contributor Author

Thank you for your comments @melissawm! I will make these improvements.

@Mukulikaa Mukulikaa marked this pull request as ready for review September 26, 2021 16:50
Copy link
Member

@melissawm melissawm left a 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 !

@melissawm
Copy link
Member

@rossbar want to give a quick look?

Comment on lines 39 to 61
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
Copy link
Contributor

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

@Mukulikaa
Copy link
Contributor Author

Thanks for the suggestions @rossbar; I like them!

very useful to have a section above this that highlights what sort of operations create copies/views.

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?

@Mukulikaa
Copy link
Contributor Author

The rendered page is here. I rebased my branch but the build is still failing for some reason.

@melissawm
Copy link
Member

Not sure why, but in any case it's not related to your work here.

@mattip mattip merged commit b513957 into numpy:main Oct 28, 2021
@mattip
Copy link
Member

mattip commented Oct 28, 2021

Thanks @Mukulikaa. The page looks great. If any tweaks are needed they can be done in follow-on PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants