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.
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.
Videos
Hi, I heard that if the values in the result were contiguous in the original array, ravel does not produce a copy of the underlying values. What does that mean? How do I know if the values in the result were contagious in the original array not? In practice, is it recommended to use flattern or ravel all the time?