NumPy ndarray.ravel()
The numpy.ndarray.ravel()
method returns a contiguous flattened array.
It creates a one-dimensional array containing the same elements as the original array in a specified order.
Syntax
ndarray.ravel([order])
Parameters
Parameter | Type | Description |
---|---|---|
order | {‘C’, ‘F’, ‘A’, ‘K’}, optional | The order in which elements are read: 'C' – C-style row-major order (default). 'F' – Fortran-style column-major order. 'A' – ‘F’ if the array is Fortran-contiguous, else ‘C’. 'K' – Elements in the order they appear in memory. |
Return Value
Returns a one-dimensional view of the original array with elements in the specified order.
Examples
1. Flattening an Array Using ravel()
This example demonstrates how ravel()
converts a multi-dimensional array into a one-dimensional array.
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Flattening the array using ravel()
flattened_arr = arr.ravel()
print("Original Array:\n", arr)
print("\nFlattened Array:\n", flattened_arr)
Output:
Original Array:
[[1 2 3]
[4 5 6]]
Flattened Array:
[1 2 3 4 5 6]
The method returns a one-dimensional array with the elements in row-major (C-style) order.
2. Using Different Orders in ravel()
Here, we use different order parameters to control how the elements are flattened.
import numpy as np
# Creating a 3x3 array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Flattening using row-major order ('C')
ravel_c = arr.ravel(order='C')
# Flattening using column-major order ('F')
ravel_f = arr.ravel(order='F')
print("Original Array:\n", arr)
print("\nFlattened using C-order:\n", ravel_c)
print("\nFlattened using F-order:\n", ravel_f)
Output:
Original Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Flattened using C-order:
[1 2 3 4 5 6 7 8 9]
Flattened using F-order:
[1 4 7 2 5 8 3 6 9]
With order='C'
, the elements are read row-wise, while order='F'
reads elements column-wise.
3. Using ravel() on Fortran-Contiguous Arrays
If the array is stored in Fortran-contiguous order, using order='A'
ensures elements are flattened accordingly.
import numpy as np
# Creating a Fortran-contiguous array
arr = np.asfortranarray([[1, 2],
[3, 4]])
# Flattening using 'A' order
ravel_a = arr.ravel(order='A')
print("Original Array:\n", arr)
print("\nFlattened using A-order:\n", ravel_a)
Output:
Original Array:
[[1 2]
[3 4]]
Flattened using A-order:
[1 3 2 4]
Since the array is Fortran-contiguous, order='A'
follows column-major order.
4. Effect of ravel() on Memory
Unlike flatten()
, which returns a copy, ravel()
returns a view of the array whenever possible.
import numpy as np
# Creating an array
arr = np.array([[10, 20, 30],
[40, 50, 60]])
# Getting a ravelled view
ravel_view = arr.ravel()
# Modifying the view
ravel_view[0] = 99
print("Modified Ravel View:\n", ravel_view)
print("\nOriginal Array After Modification:\n", arr)
Output:
Modified Ravel View:
[99 20 30 40 50 60]
Original Array After Modification:
[[99 20 30]
[40 50 60]]
Since ravel()
returns a view (not a copy), modifying the flattened array also modifies the original array.