[Numpy-discussion] OWNDATA flag and reshape() -- views vs. copies

Eric Firing efiring@hawaii....
Sat Apr 19 21:33:47 CDT 2008


Guillaume Desjardins wrote:
> I'm pretty new to Python and numpy (longtime c / matlab programmer),
> but after a read through some of the past threads and Travis' "Guide
> to Numpy", I think I have a fairly good understanding of how the
> reshape() function / methods work, with regards to views and copies.
> For what its worth (and to make things clearer), my understanding is
> that:
> 
> * Function reshape: create a view, or if it can't (i.e. resulting
> array cannot be expressed with constant stride parameters), a copy
> * Method obj.reshape(): creates a view, or throws an exception if it can't
> 
> Now assuming that is the case, I do not understand the behaviour of
> the following code.
> 
> a = array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);
> a.flags
>  C_CONTIGUOUS : True
>  F_CONTIGUOUS : False
>  OWNDATA : True
>  WRITEABLE : True
>  ALIGNED : True
>  UPDATEIFCOPY : False
> 
> b = a[1:,1:3] # creates view of a
>>> array([[ 6,  7],
>             [10, 11]])
> b.flags
>  C_CONTIGUOUS : False
>  F_CONTIGUOUS : False
>  OWNDATA : False
>  WRITEABLE : True
>  ALIGNED : True
>  UPDATEIFCOPY : False
> 
> c = reshape(b,(4,1))  # RESHAPE FUNCTION: should copy, as view of this
> is impossible (? isn't it ?)
> c
>>> array([[ 6],
>         [ 7],
>         [10],
>         [11]])
> c.flags
>  C_CONTIGUOUS : True
>  F_CONTIGUOUS : False
>  OWNDATA : False
>  WRITEABLE : True
>  ALIGNED : True
>  UPDATEIFCOPY : False
> 
> First question: if c is a copy of b, then why isn't the OWNDATA set to true ?

It looks like creation of c is done in two steps: first a copy is made 
of b, and then c is returned as a view of that copy.  I don't know if 
this is intentional; it sees a bit odd.  I agree with you that I would 
have expected c to own its data rather than refer to a base hidden in 
the shadows.

In [62]:b.base
Out[62]:
array([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]])

In [63]:b
Out[63]:
array([[ 6,  7],
        [10, 11]])

In [64]:c.base
Out[64]:
array([[ 6,  7],
        [10, 11]])

In [65]:c
Out[65]:
array([[ 6],
        [ 7],
        [10],
        [11]])

Eric


More information about the Numpy-discussion mailing list