Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (50.2k points)

Looking for a way to reliably identify if a numpy object is a view.

  • The test used in pandas now is to call something a view if my_array.base is ‘not’ None. This seems to always catch views, but also offers lots of false positives (situations where it reports something is a view even if it isn't).

  • numpy.may_share_memory() will check for two specific arrays, but won't answer generically

1 Answer

0 votes
by (108k points)

The situations described as false positives seem to be when an operation that "creates a copy" actually returns a view of a copy. If this is to be considered "not a view", you might try checking the ref count of the array's base to see whether there are any other references to it, but that won't be perfect. I don't think it's possible to reliably detect this kind of thing. It's impossible without tracking the entire origin of the data to detect that kind of behavior. 

You can refer to the following link for more information regarding the same:

If you are interested in learning Numpy then visit this Python Course by Intellipaat.

Related questions

Browse Categories