Numpy: Reliable (non-conservative) indicator if a numpy array is displayed

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

Related questions have come up many times ( here , here , here ) and people have suggested some solutions, but everyone seems to have problems:

  • The test used in pandas should now call something if my_array.base is not None . It seems like it is always eye-catching, but it also offers a lot of false positives (situations where he reports something are kind, even if they are not).
  • numpy.may_share_memory() will check for two specific arrays, but will not respond in general
    • (@RobertKurn says the best tool since 2012 is any change?)
  • flags['OWNDATA']) reported (third comment of the first answer) in some cases fail.

(The reason for my interest is that I am working on a copy-on-write implementation for pandas, and a conservative indicator leads to redundant copying.)

+45
python arrays numpy pandas
Nov 04 '15 at 19:48
source share
1 answer

Depending on your customs, flags['OWNDATA'] will do the job. In fact, there is no problem with your link. In some cases, this does not work . He will always do what he must do.

According to http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.require.html : the flag "provides an array that owns its own data."

In your "counterexample" they use the code:

 print (b.flags['OWNDATA']) #False -- apparently this is a view e = np.ravel(b[:, 2]) print (e.flags['OWNDATA']) #True -- Apparently this is a new numpy object. 

But in the second case, this is the normal behavior of True.

It comes from the definition of ravel (from http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.ravel.html ).

Returns a continuous flattened array. A 1-D array containing input elements is returned. A copy is made only if necessary.

A copy is required here, so a copy is made. Thus, the variable e really owns its own data. This is not "view b", "link to b", "alias to part b". This is a real new array containing a copy of some elements of b.

So, I think this is not possible without tracking the entire data source to detect this behavior . I believe that you should create your program with this flag.

+1
Jun 06 '16 at 22:12
source share



All Articles