[Numpy-discussion] Determining the 'viewness' of an array, and flags.owndata confusion
Larsen, Brian A
balarsen@lanl....
Tue Feb 28 17:12:48 CST 2012
Stack overflow post related to this:
http://stackoverflow.com/questions/9164269/can-you-tell-if-an-array-is-a-view-of-another
On Feb 28, 2012, at 4:01 PM, Kurt Smith wrote:
For an arbitrary numpy array 'a', what does 'a.flags.owndata' indicate?
I originally thought that owndata is False iff 'a' is a view. But
that is incorrect.
Consider the following:
In [119]: a = np.zeros((3,3))
In [120]: a.flags.owndata # should be True; zeros() creates and
returns a non-view array.
Out[120]: True
In [121]: a_view1 = a[2:, :]
In [122]: a_view1.flags.owndata # expected to be False
Out[122]: False
In [123]: a_fancy1 = a[[0,1], :]
In [124]: a_fancy1.flags.owndata # expected to be True, a_fancy1 is a
fancy-indexed array.
Out[124]: True
In [125]: a_fancy2 = a[:, [0,1]]
In [126]: a_fancy2.flags.owndata # expected to be True, a_fancy2 is a
fancy-indexed array.
Out[126]: False
So when I query an array's flags.owndata, what is it telling me? What
I want to know is whether an array is a view or not. If flags.owndata
has nothing to do with the 'viewness' of an array, how would I
determine if an array is a view?
In the previous example, a_fancy2 does not own its data, as indicated
by 'owndata' being False. But when I modify a_fancy2, 'a' is not
modified, as expected, but contrary to what 'owndata' would seem to
indicate.
The numpybook's documentation of owndata could perhaps be a bit
clearer on the subject:
"""
OWNDATA (O) the array owns the memory it uses or if it borrows it from
another object (if this is False, the base attribute retrieves a
reference to the object this array obtained its data from)
"""
>From my reading, owndata has nothing to do with the 'viewness' of an
array. Is this correct? What is the intent of this flag, then? Its
wording could perhaps be improved: owndata is True iff (1) the array
owns the memory it uses or (2) the array borrows it from another
object. The second clause seems to indicate that the array **does
not** own its data since it is borrowing it from another object.
However, flags.owndata will be true in this case.
If I cannot use flags.owndata, what is a reliable way to determine
whether or not an array is a view?
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org<mailto:NumPy-Discussion@scipy.org>
http://mail.scipy.org/mailman/listinfo/numpy-discussion
--
Brian A. Larsen
ISR-1 Space Science and Applications
Los Alamos National Laboratory
PO Box 1663, MS-D466
Los Alamos, NM 87545
USA
(For overnight add:
SM-30, Bikini Atoll Road)
Phone: 505-665-7691
Fax: 505-665-7395
email: balarsen@lanl.gov<mailto:balarsen@lanl.gov>
Correspondence /
Technical data or Software Publicly Available
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120228/9eeb88ad/attachment-0001.html
More information about the NumPy-Discussion
mailing list