Hi, I thought I remembered there was a way to reshape inplace an array, but neither google, nor greping my mailbox brings anything out. Am I wrong, or is there indeed a way to reshape inplace an array ? Cheers, Gaël
On 23/04/07, Gael Varoquaux
Hi,
I thought I remembered there was a way to reshape inplace an array, but neither google, nor greping my mailbox brings anything out. Am I wrong, or is there indeed a way to reshape inplace an array ?
Sometimes it's just impossible to reshape an array without copying. But reshape() (either the method or the function) copies only the array descriptor if that's possible. So you'll have a new ndarray object, but the data will not be copied. Assigning to the shape attribute might do what you want (if it triggers a reshape() internally, which I think it does). I don't think it's possible to *rearrange* the data in memory in place. I can't even think of an algorithm to do that, off the top of my head. Anne
On 4/23/07, Gael Varoquaux
Hi,
I thought I remembered there was a way to reshape inplace an array, but neither google, nor greping my mailbox brings anything out. Am I wrong, or is there indeed a way to reshape inplace an array ?
Just set the shape of the array: somearray.shape = newshape Should do the trick regards, tim Cheers,
Gaël _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
 //=][=\\ tim.hochberg@ieee.org
On Mon, Apr 23, 2007 at 10:20:43AM 0700, Timothy Hochberg wrote:
Just set the shape of the array:
somearray.shape = newshape
Of course ! Thanks On Mon, Apr 23, 2007 at 12:20:51PM 0500, Robert Kern wrote:
.reshape()
Unless I miss something obvious "a.reshape()" doesn't modify a, which is somewhat missleading, IMHO. Cheers, Gaël
Gael Varoquaux wrote:
On Mon, Apr 23, 2007 at 12:20:51PM 0500, Robert Kern wrote:
.reshape()
Unless I miss something obvious "a.reshape()" doesn't modify a, which is somewhat missleading, IMHO.
Nope, you caught me in a moment of stupidity.  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
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:
import numpy a = numpy.zeros((2,3))
help(a.reshape) Help on builtin function reshape:
reshape(...) a.reshape(d1, d2, ..., dn, order='c') Return a new array from this one. The new array must have the same number of elements as self. Also always returns a view or raises a ValueError if that is impossible.;
a array([[ 0., 0., 0.], [ 0., 0., 0.]])
b = a.reshape((6,)) a array([[ 0., 0., 0.], [ 0., 0., 0.]])
so a hasn't changed.
b array([ 0., 0., 0., 0., 0., 0.])
but b is a different shape.
b[1] = 5 a array([[ 0., 5., 0.], [ 0., 0., 0.]])
b and a share data. if you want to change the shape of a:
a.shape = (6,) a array([ 0., 5., 0., 0., 0., 0.])
Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
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.]])
On 4/23/07, Christopher Barker
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:
Sometimes it do, sometimes it don't: In [8]: x = ones((8,8)) In [9]: y = x[:3,:6] In [10]: z = y.reshape(2,9) In [11]: z[...] = 0 In [12]: x Out[12]: array([[ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1., 1., 1., 1.]]) In [13]: z Out[13]: array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0.] As Ann pointed out, sometimes reusing the same array isn't possible. This makes the use of reshaped arrays as lvalues subject to subtle errors, so caution is advised. Chuck
On 4/23/07, Christopher Barker
reshape(...) a.reshape(d1, d2, ..., dn, order='c')
Return a new array from this one. The new array must have the same
number of elements as self. Also always returns a view or raises a ValueError if that is impossible.;
Here's a better doc string that explains "This will be a new view object if possible; otherwise, it will return a copy."
numpy.reshape? Type: function Base Class:
String Form: Namespace: Interactive File: /usr/local/lib/python2.4/sitepackages/numpy/core/fromnumeric.py Definition: numpy.reshape(a, newshape, order='C') Docstring: Return an array that uses the data of the given array, but with a new shape.
:Parameters:  `a` : array  `newshape` : shape tuple or int The new shape should be compatible with the original shape. If an integer, then the result will be a 1D array of that length.  `order` : 'C' or 'FORTRAN', optional (default='C') Whether the array data should be viewed as in C (rowmajor) order or FORTRAN (columnmajor) order. :Returns:  `reshaped_array` : array This will be a new view object if possible; otherwise, it will return a copy. :See also: numpy.ndarray.reshape() is the equivalent method.
On 4/23/07, Keith Goodman
On 4/23/07, Christopher Barker
wrote: reshape(...) a.reshape(d1, d2, ..., dn, order='c')
Return a new array from this one. The new array must have the same
number of elements as self. Also always returns a view or raises a ValueError if that is impossible.;
Here's a better doc string that explains "This will be a new view object if possible; otherwise, it will return a copy."
numpy.reshape? Type: function Base Class:
String Form: Namespace: Interactive File: /usr/local/lib/python2.4/sitepackages/numpy/core/fromnumeric.py Definition: numpy.reshape(a, newshape, order='C') Docstring: Return an array that uses the data of the given array, but with a new shape. :Parameters:  `a` : array  `newshape` : shape tuple or int The new shape should be compatible with the original shape. If an integer, then the result will be a 1D array of that length.  `order` : 'C' or 'FORTRAN', optional (default='C') Whether the array data should be viewed as in C (rowmajor) order or FORTRAN (columnmajor) order.
:Returns:  `reshaped_array` : array This will be a new view object if possible; otherwise, it will return a copy.
:See also: numpy.ndarray.reshape() is the equivalent method.
I think that it should raise an error, or warn, if it needs to make a copy, but that isn't the tradition. Reshape does raise an error if the product of the dimensions isn't the same for both the original and the reshaped array. Chuck
Charles R Harris wrote:
Here's a better doc string that explains "This will be a new view object if possible; otherwise, it will return a copy."
Does this exist somewhere, or are you contributing it now?
I think that it should raise an error, or warn, if it needs to make a copy,
I totally agree, and that behavior matches the current ('1.0.2') docstring.
but that isn't the tradition.
so sad  could this be deprecated. I know we had a pretty length discussion about this issue in another context. In general, it's a bad idea for methods to sometimes return copies and sometimes views  it's begging for subtle bugs! oh well, either the code or the docstring should change, in any case. Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
On 4/23/07, Christopher Barker
Charles R Harris wrote:
Here's a better doc string that explains "This will be a new view object if possible; otherwise, it will return a copy."
Does this exist somewhere, or are you contributing it now?
At the moment numpy.reshape and array.reshape have different doc strings (I'm using numpy 1.0.2.dev3546). The one I pasted is from numpy.reshape.
Keith Goodman wrote:
At the moment numpy.reshape and array.reshape have different doc strings (I'm using numpy 1.0.2.dev3546). The one I pasted is from numpy.reshape.
And I see from there: :See also: numpy.ndarray.reshape() is the equivalent method. so it looks like they are the same thing. On the other hand, I *think* that some of the ndarray methods are slightly different, but the functions have been retained with the old functionality for backward compatibility reasons. However, it looks like ndarray.reshape() is behaving like the docstring for numpy.reshape(), so there is a bug of some sort (probably a doc bug). if numpy.reshape() is delegating to ndarray.reshape() couldn't they share docstrings somehow? Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
Christopher Barker wrote:
if numpy.reshape() is delegating to ndarray.reshape() couldn't they share docstrings somehow?
Yes, of course. I got bored halfway through the conversions and didn't get to doing the methods. If you want to speed up the process, please submit a patch.  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
Gael Varoquaux wrote:
Hi,
I thought I remembered there was a way to reshape inplace an array, but neither google, nor greping my mailbox brings anything out. Am I wrong, or is there indeed a way to reshape inplace an array ?
.reshape()  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
participants (8)

Anne Archibald

Charles R Harris

Christopher Barker

Gael Varoquaux

Keith Goodman

Pierre GM

Robert Kern

Timothy Hochberg