[Numpy-discussion] Why ndarray provides four ways to flatten?
Sebastian Berg
sebastian at sipsolutions.net
Wed Oct 29 05:11:04 EDT 2014
On Di, 2014-10-28 at 20:37 -0400, Alexander Belopolsky wrote:
>
> On Tue, Oct 28, 2014 at 1:42 PM, Stephan Hoyer <shoyer at gmail.com>
> wrote:
> .flat lets you iterate over all elements of a N-dimensional
> array as if it was 1D, without ever needing to make a copy of
> the array. In contrast, ravel() and reshape(-1) cannot always
> avoid a copy, because they need to return another ndarray.
>
>
> In some cases ravel() returns a copy where a view can be easily
> constructed. For example,
>
Yeah, but we just changed that for 1.10, not ravel can even get further
if you use order='K'.
- Sebastian
>
> >>> x = np.arange(10)
> >>> y = x[::2]
> >>> y.ravel().flags['OWNDATA']
> True
>
>
> Interestingly, in the same case reshape(-1) returns a view:
>
>
> >>> y.reshape(-1).flags['OWNDATA']
> False
>
>
> (This suggests at least a documentation bug - numpy.ravel
> documentation says that it is equivalent to reshape(-1).)
>
>
> It is only in situations like this
>
>
> >>> a = np.arange(16).reshape((4,4))
> >>> a[1::2,1::2].ravel()
> array([ 5, 7, 13, 15])
>
>
> where flat view cannot be an ndarray, but .flat can still return
> something that is at least duck-typing compatible with ndarray (if not
> an ndarray subclass) and behaves as a view into original data.
>
>
> My preferred design would be for x.flat to return a flat view into x.
> This would be consistent with the way .T and .real attributes are
> defined and close enough to .imag. An obvious way to obtain a flat
> copy would be x.flat.copy(). Once we have this, ravel() and flatten()
> can be deprecated and reshape(-1) discouraged.
>
>
> I think this would be backward compatible except for rather
> questionable situations like this:
>
>
> >>> i = x.flat
> >>> list(i)
> [0, 1, 2, 3, 4, 0, 6, 7, 8, 9]
> >>> list(i)
> []
> >>> np.array(i)
> array([0, 1, 2, 3, 4, 0, 6, 7, 8, 9])
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141029/b7a660e3/attachment.sig>
More information about the NumPy-Discussion
mailing list