numpy: Reliable (non-conservative) indicator if numpy array is view numpy: Reliable (non-conservative) indicator if numpy array is view arrays arrays

numpy: Reliable (non-conservative) indicator if numpy array is view


Depending on your usages, flags['OWNDATA'] would do the job. In fact, there's no problem with your link. It does not fail in some cases. It will always do what it's supposed to do.

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

In your "counterexample", they use the code:

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

But, it's the normal behaviour to be True in the second case.

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

Return a contiguous flattened array. A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.

Here, a copy is needed, so a copy is made. So, the variable e really owns its own data. It's not a "view of b", "a reference to b", "an alias to a part of b". It's a real new array that contains a copy of some elements of b.

So, I think that it's impossible without tracking the entire origin of the data to detect that kind of behaviour. I believe you should be able to build your program with that flag.