[Numpy-discussion] Why ndarray provides four ways to flatten?

Yuxiang Wang yw5aj at virginia.edu
Mon Oct 27 21:41:18 EDT 2014


Hi Alexander,

In my opinion - because they don't do the same thing, especially when
you think in terms in lower-level.

ndarray.flat returns an iterator; ndarray.flatten() returns a copy;
ndarray.ravel() only makes copies when necessary; ndarray.reshape() is
more general purpose, even though you can use it to flatten arrays.

They are very distinct in behavior - for example, copies and views may
store in the memory very differently and you would have to pay
attention to the stride size if you are passing them down onto
C/Fortran code. (Correct me if I am wrong please)

-Shawn

On Mon, Oct 27, 2014 at 8:06 PM, Alexander Belopolsky <ndarray at mac.com> wrote:
> Given an n-dim array x, I can do
>
> 1. x.flat
> 2. x.flatten()
> 3. x.ravel()
> 4. x.reshape(-1)
>
> Each of these expressions returns a flat version of x with some variations.
> Why does NumPy implement four different ways to do essentially the same
> thing?
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Yuxiang "Shawn" Wang
Gerling Research Lab
University of Virginia
yw5aj at virginia.edu
+1 (434) 284-0836
https://sites.google.com/a/virginia.edu/yw5aj/



More information about the NumPy-Discussion mailing list