numpy.ravel

numpy.ravel(a, order='C')

Return a flattened array.

A 1-d array, containing the elements of the input, is returned. A copy is made only if needed.

Parameters:

a : array_like

Input array. The elements in a are read in the order specified by order, and packed as a 1-dimensional array.

order : {‘C’,’F’}, optional

The elements of a are read in this order. It can be either ‘C’ for row-major order, or F for column-major order. By default, row-major order is used.

Returns:

1d_array : ndarray

Output of the same dtype as a, and of shape (a.size(),) (or (np.prod(a.shape),)).

See also

ndarray.flat
1-D iterator over an array.
ndarray.flatten
1-D array copy of the elements of an array in row-major order.

Notes

In row-major order, the row index varies the slowest, and the column index the quickest. This can be generalised to multiple dimensions, where row-major order implies that the index along the first axis varies slowest, and the index along the last quickest. The opposite holds for Fortran-, or column-major, mode.

Examples

If an array is in C-order (default), then ravel is equivalent to reshape(-1):

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print x.reshape(-1)
[1  2  3  4  5  6]
>>> print np.ravel(x)
[1  2  3  4  5  6]

When flattening using Fortran-order, however, we see

>>> print np.ravel(x, order='F')
[1 4 2 5 3 6]

Previous topic

numpy.reshape

Next topic

numpy.ndarray.flat

This Page

Quick search