NumPy ravel()

The ravel() method flattens a NumPy array without changing its data.

Example

import numpy as np

array1 = np.array([[0, 1], [2, 3]])

# flatten an array array2 = np.ravel(array1)
print(array2) # Output : [0 1 2 3]

ravel() Syntax

The syntax of ravel() is:

numpy.ravel(array, order)

ravel() Arguments

The ravel() method takes two arguments:

  • array - an original array that is to be flattened
  • order (optional) - specifies the order in which the array elements are flattened

ravel() Return Value

The ravel() method returns the flattened array.


Example 1: Flatten a Multidimensional Array

import numpy as np

array1 = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])

# flatten the array array2 = np.ravel(array1)
print(array2)

Output

 [0 1 2 3 4 5 6 7]

Using Optional Order Argument in ravel()

The order argument specifies the order in which the array elements are flattened.

The order can be:

  • 'C' - flattens the elements row-wise (in C-style order)
  • 'F' - flattens the elements column-wise (in Fortran-style order)
  • 'A' - tries to preserve the original array's order, otherwise defaults to C-order.
  • 'K' - flattens the elements in the order they occur in memory, and uses C-order by default.
import numpy as np

array1 = np.array([[[0, 1], [2, 3]],
                   [[4, 5], [6, 7]]])

# flatten the array row-wise array2 = np.ravel(array1, order = 'C')
print("C:",array2)
# flatten the array column-wise array3 = np.ravel(array1, order = 'F')
print("F:",array3)
# flatten the array to original array's order array4 = np.ravel(array1, order = 'A')
print("A:",array2)
# flatten the array in the order they occur in memory) array5 = np.ravel(array1, order = 'K')
print("K:",array3)

Output

C: [0 1 2 3 4 5 6 7]
F: [0 4 2 6 1 5 3 7]
A: [0 1 2 3 4 5 6 7]
K: [0 4 2 6 1 5 3 7]

Difference Between Flatten and Ravel

The key differences between flatten() and ravel() are

  • flatten() is an ndarray object method whereas ravel() is a library-level function. For example,
import numpy as np

array1 = np.array([[1, 2], [3, 4]])

# flatten is used as an array object array2 = array1.flatten() # ravel is used as a library function array3 = np.ravel(array1)
print(array2) print(array3)

Output

[1 2 3 4]
[1 2 3 4]
  • ravel() works with a list of arrays, but flatten() doesn't.
import numpy as np

# create a list of arrays
array1 = [np.array([1, 2]), np.array([3, 4])]

# use ravel with a list of arrays raveled_arr_list = np.ravel(array1)
print(raveled_arr_list)
# using flatten with a list of arrays # raises an error flattened_arr_list = array1.flatten()
print(flattened_arr_list)

Output

[1 2 3 4]
Traceback (most recent call last):
  File "<string>", line 13, in <module>
ERROR!
AttributeError: 'list' object has no attribute 'flatten'
  • flatten() always returns a copy of the original array whereas ravel() makes a copy only when necessary.

To learn more, visit NumPy flatten().

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community