[Numpy-discussion] Determining the 'viewness' of an array, and flags.owndata confusion

Jonathan Rocher jrocher@enthought....
Tue Feb 28 20:06:40 CST 2012


Thank you all for your answers. Kurt and I were training new developers on
numpy and telling them that
- fancy indexing creates a copy
- owndata was a good way to know if an array is a view or a copy.
It turns out that these were both correct statements but we didn't think
that the fancy indexing would sometimes return a view after making a copy
(BTW is that necessary? It sounds to me like it is complicating things for
no real benefit.)

So a more precise question then is "is a_fancy a view on a's data or does
it copy the data and its data is independent?" and the answer is probably:
since "a_fancy1.base is a" is false then the 2 array's data are independent
from each other.

Do I have it right now?

Jonathan

On Tue, Feb 28, 2012 at 5:31 PM, Nathaniel Smith <njs@pobox.com> wrote:

> On Tue, Feb 28, 2012 at 11:01 PM, Kurt Smith <kwmsmith@gmail.com> wrote:
> > For an arbitrary numpy array 'a', what does 'a.flags.owndata' indicate?
>
> I think what it really indicates is whether a's destructor should call
> free() on a's data pointer.
>
> > 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.
>
> It looks like the fancy indexing code in this case actually creates a
> new array, and then instead of returning it directly, returns a
> (reshaped) view on this new array. If you look at a_fancy2.base,
> you'll see this new array.
>
> So: a_fancy2 *is* a view... it's just not a view of 'a'. It's a view
> of this other array.
>
> > If I cannot use flags.owndata, what is a reliable way to determine
> > whether or not an array is a view?
>
> owndata *is* a reliable way to determine whether or not an array is a
> view; it just turns out that this is not a very useful question to
> ask.
>
> What are you actually trying to do? There's probably another way to
> accomplish it.
>
> -- Nathaniel
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Jonathan Rocher, PhD
Scientific software developer
Enthought, Inc.
jrocher@enthought.com
1-512-536-1057
http://www.enthought.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120228/c1390c47/attachment.html 


More information about the NumPy-Discussion mailing list