[Numpy-discussion] Inplace reshape

Pierre GM pgmdevlist at gmail.com
Mon Apr 23 13:44:56 EDT 2007


On Monday 23 April 2007 13:36:26 Christopher Barker wrote:
> Gael Varoquaux wrote:
> > Unless I miss something obvious "a.reshape()" doesn't modify a, which is
> > somewhat missleading, IMHO.
>
> quite correct. .reshape() creates a new array that shared data with the
> original:

Mmh. My understanding is that .reshape() creates a *view* of the original 
array, so no data is actually copied. The only thing that changes is how the 
data is read. That means that modifying the reshaped version will modify the 
original.

So:

>>>import numpy as N
>>>a=N.zeros((3,2))
array([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])
>>>b=a.reshape(6,)
>>>b
array([ 0.,  0.,  0.,  0.,  0.,  0.])

>>>b += 1
>>>b
array([ 1.,  1.,  1.,  1.,  1.,  1.])
>>>a
array([[ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.]])



More information about the NumPy-Discussion mailing list