[Numpy-discussion] fancy view question

Robert Kern robert.kern at gmail.com
Tue Feb 17 10:21:49 EST 2009


On Tue, Feb 17, 2009 at 09:15,  <josef.pktd at gmail.com> wrote:

> I'm still learning about views:
>
>>>> b = np.repeat(np.repeat(a, 2, axis=0), 2, axis=1)
>>>> b.flags
>  C_CONTIGUOUS : True
>  F_CONTIGUOUS : False
>  OWNDATA : True
>  WRITEABLE : True
>  ALIGNED : True
>  UPDATEIFCOPY : False
>
> Does OWNDATA : True  mean it made a copy? Or is there another way to
> check whether it's a view or a copy?
> zoom has OWNDATA : False
>
>>>> c = zoom(a, 3,2)
>>>> c.flags
>  C_CONTIGUOUS : True
>  F_CONTIGUOUS : False
>  OWNDATA : False
>  WRITEABLE : True
>  ALIGNED : True
>  UPDATEIFCOPY : False

zoom() does not return a view of the input. It can't. That kind of
view *cannot* be created in the shape/strides memory model. The
.reshape() has to make a copy.

In [10]: x = array([[1,2],[3,4]])

In [11]: np.lib.stride_tricks.as_strided(x, (2, 2, 2, 2), (8, 0, 4,
0)).reshape((4, 4))
Out[11]:
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])

In [12]: y = _

In [13]: x[0,0] = 10

In [14]: y
Out[14]:
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list