[Numpy-discussion] reshape and ravel methods of arrays now return views or raise error

Zachary Pincus zpincus at stanford.edu
Tue Mar 28 17:36:11 CST 2006


Uh oh,

Just updated to new numpy so that dot() wouldn't be broken. What fun  
to play with the new order/ravel/reshape stuff.

Unfortunately, the ravel/reshape methods seem to not be quite worked  
out properly yet. Basically (and as expected) the ravel method  
doesn't work anymore on fortran-strided arrays. But since fortran- 
strided arrays happen *all-the-time* (e.g. after a transpose  
operation), things get confusing fast.

In fact, absolutely no reshape operation (other than the identity  
reshape) will work on fortran-strided arrays (see below). Also, the  
errors could be more helpful, and anyway ravel shouldn't fail with an  
error about reshape fortran-strided.

This can't be the optimal situation, since it makes the method  
interface next-to-useless (because exceptions might be thrown at any  
moment) unless a *lot* of care is taken to properly sanitize the  
arrays that are used (in which case any speed gains are probably lost).

In [1]: import numpy
In [2]: numpy.__version__
Out[2]: '0.9.7.2291'
In [3]: a = numpy.array([[1,2],[3,4],[5,6]])
In [4]: b = a.transpose()
In [5]: a.ravel()
Out[5]: array([1, 2, 3, 4, 5, 6])
In [6]: b.ravel()
ValueError: cannot return a view from reshape.
In [7]: b.shape
Out[7]: (2, 3)
In [8]: b.reshape((1,6))
ValueError: cannot return a view from reshape.
In [9]: b.reshape((6,1))
ValueError: cannot return a view from reshape.
In [10]: b.reshape((3,2))
ValueError: cannot return a view from reshape.

This state of affairs seems pretty hostile to both advanced users and  
basic ones. Now I need to go through all of my code and eliminate the  
use of array methods, since about half of the time I'll be calling  
things like ravel on arrays that have been transposed. What a waste  
-- is there something better I can do or something better numpy can do?

At the very least, a strong convention really needs to be established  
and made clear about what the methods do (never return copies) and  
what the functions do (sometimes return copies? hardly a strong  
convention), and when to choose one or the other.

Zach





More information about the Numpy-discussion mailing list