The dictionary meaning for ravel is
to become unwoven, untwisted, or unwound
We tend to use unravel in same way
to separate or undo the texture of : UNRAVEL
https://www.merriam-webster.com/dictionary/ravel
In numpy flatten does the same thing, except it always makes a copy. ravel is more like reshape(-1), returning a view where possible
It's use for computational arrays may trace back to apl in the 1960s.
https://aplwiki.com/wiki/Ravel.
Answer from hpaulj on Stack OverflowVideos
The dictionary meaning for ravel is
to become unwoven, untwisted, or unwound
We tend to use unravel in same way
to separate or undo the texture of : UNRAVEL
https://www.merriam-webster.com/dictionary/ravel
In numpy flatten does the same thing, except it always makes a copy. ravel is more like reshape(-1), returning a view where possible
It's use for computational arrays may trace back to apl in the 1960s.
https://aplwiki.com/wiki/Ravel.
ravel means the same as unravel - to become unwoven, untwisted, or unwound
As for numpy ravel - a 1-D array, containing the elements of the input, is returned. So if you provide a 2D array to ravel, it will be unwoven, untwisted or unwound, to become a 1D array.
x = np.array([[1, 2, 3],
[4, 5, 6]])
np.ravel(x)
OUTPUT:
array([1, 2, 3, 4, 5, 6])
The current API is that:
flattenalways returns a copy.ravelreturns a contiguous view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel will often be faster since no memory is copied, but you have to be more careful about modifying the array it returns.reshape((-1,))gets a view whenever the strides of the array allow it even if that means you don't always get a contiguous array.
As explained here a key difference is that:
flattenis a method of an ndarray object and hence can only be called for true numpy arrays.ravelis a library-level function and hence can be called on any object that can successfully be parsed.
For example ravel will work on a list of ndarrays, while flatten is not available for that type of object.
@IanH also points out important differences with memory handling in his answer.