Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return
4.1.
Python
5.
Examples
5.1.
Code 1: Shows that array.ravel is equivalent to reshape(-1, order=order)
5.2.
Python
5.3.
Code 2: Showing ordering manipulation
5.4.
Python
6.
Frequently Asked Questions
6.1.
What is the difference between numpy.ravel() and numpy.flatten()?
6.2.
Can numpy.ravel() handle arrays with more than 2 dimensions?
6.3.
What happens if I modify the flattened array returned by numpy.ravel()?
7.
Conclusion
Last Updated: Aug 16, 2024
Easy

numpy.ravel() in Python

Author Rahul Singh
0 upvote

Introduction

NumPy is a powerful Python library used for numerical related analysis and programming. It offers a wide range of tools and functions to work with arrays and matrices efficiently. From so many predefined important function, one of the useful functions in NumPy is numpy.ravel(), which allows you to flatten a multi-dimensional array into a one-dimensional array. 

numpy.ravel() in Python

In this article, we will talk about the numpy.ravel() function, its syntax, parameters, and return values. We will also see code examples that will show the implementation of numpy.ravel() .

Syntax

The syntax of numpy.ravel() function is very simple and easy : 

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


In the above syntax:

- `a` represents the input array that you want to flatten.
 

- `order` is an optional parameter that specifies the order in which the elements of the array should be read. It can take the following values:
 

  - `'C'` (default): The elements are read in row-major order (C-style).
 

  - `'F'`: The elements are read in column-major order (Fortran-style).
 

  - `'A'`: The elements are read in the order they are stored in memory.
 

  - `'K'`: The elements are read in the order they are stored in memory, but only if `a` is Fortran contiguous. Otherwise, it raises an error.

Parameters

The numpy.ravel() function takes two parameters:
 

1. `a` (array_like): This is the input array that you want to flatten. It can be a multi-dimensional array of any shape.
 

2. `order` (str, optional): This parameter specifies the order in which the elements of the array should be read during the flattening process. It can take one of the following values:
 

   - `'C'` (default): The elements are read in row-major order (C-style). This means that the elements are read row by row, from left to right, top to bottom.
 

   - `'F'`: The elements are read in column-major order (Fortran-style). This means that the elements are read column by column, from top to bottom, left to right.
 

   - `'A'`: The elements are read in the order they are stored in memory. If the array is Fortran contiguous, it will be read in Fortran order. If the array is C contiguous, it will be read in C order.
 

   - `'K'`: The elements are read in the order they are stored in memory, but only if `a` is Fortran contiguous. If `a` is not Fortran contiguous, it raises an error.
 

The `order` parameter allows you to control the order in which the elements are flattened, depending on your specific requirements.

Return

The numpy.ravel() function returns a flattened array, which is a one-dimensional array containing all the elements of the input array in the specified order.

The returned array is a view of the original array, meaning that any modifications made to the returned array will also affect the original array. If you want to create a new copy of the flattened array instead of a view, you can use the numpy.ravel().copy() method.

The shape of the returned array is (n,), where n is the total number of elements in the input array.

For example : 

  • Python

Python

import numpy as np



# Create a 2D array

arr = np.array([[1, 2, 3], [4, 5, 6]])



# Flatten the array using numpy.ravel()

flattened_arr = np.ravel(arr)


print(flattened_arr) 

print(flattened_arr.shape)
You can also try this code with Online Python Compiler
Run Code


Output

[1 2 3 4 5 6]
(6,)


In the above example, we create a 2D array `arr` and then use numpy.ravel() to flatten it. The resulting `flattened_arr` is a one-dimensional array containing all the elements of `arr` in row-major order (default). The shape of `flattened_arr` is (6,), indicating that it has 6 elements.

Examples

Code 1: Shows that array.ravel is equivalent to reshape(-1, order=order)

Let's take a look at an example that shows how numpy.ravel() is equivalent to using numpy.reshape() with a shape of (-1,) and the same order parameter.

  • Python

Python

import numpy as np


# Create a 2D array

arr = np.array([[1, 2, 3], [4, 5, 6]])


# Flatten the array using numpy.ravel()

flattened_arr_ravel = np.ravel(arr, order='C')


# Flatten the array using numpy.reshape()

flattened_arr_reshape = arr.reshape(-1, order='C')


print(flattened_arr_ravel) 

print(flattened_arr_reshape) 


print(np.array_equal(flattened_arr_ravel, flattened_arr_reshape)) 
You can also try this code with Online Python Compiler
Run Code


Output

[1 2 3 4 5 6]
[1 2 3 4 5 6]
True


In this example, we create a 2D array `arr`. We then flatten the array using both numpy.ravel() and numpy.reshape().
 

- For numpy.ravel(), we pass the `arr` and specify the `order` parameter as `'C'` to flatten the array in row-major order.

- For numpy.reshape(), we use `arr.reshape(-1, order='C')`. The `-1` in the shape indicates that the dimension should be calculated automatically based on the size of the array and the other dimensions. We also specify the `order` parameter as `'C'` to flatten the array in row-major order.


We print both `flattened_arr_ravel` and `flattened_arr_reshape` to see the flattened arrays. As you can see, both methods produce the same flattened array `[1 2 3 4 5 6]`.


Finally, we use numpy.array_equal() to compare `flattened_arr_ravel` and `flattened_arr_reshape`. It returns `True`, confirming that the arrays are equal and that numpy.ravel() is indeed equivalent to numpy.reshape() with a shape of (-1,) and the same order parameter.

Code 2: Showing ordering manipulation

In this code, we'll see how the `order` parameter in numpy.ravel() affects the flattening of a multi-dimensional array.

  • Python

Python

import numpy as np

# Create a 2D array

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Flatten the array using numpy.ravel() with default order ('C')

flattened_arr_c = np.ravel(arr, order='C')

# Flatten the array using numpy.ravel() with Fortran order ('F')

flattened_arr_f = np.ravel(arr, order='F')

print("Original array:")

print(arr)

print("\nFlattened array with default order ('C'):")

print(flattened_arr_c)

print("\nFlattened array with Fortran order ('F'):")

print(flattened_arr_f)
You can also try this code with Online Python Compiler
Run Code


Output

Original array:
[[1 2 3]
 [4 5 6]]
Flattened array with default order ('C'):
[1 2 3 4 5 6]
Flattened array with Fortran order ('F'):
[1 4 2 5 3 6]


In this example, we create a 2D array `arr`. We then use numpy.ravel() to flatten the array with different ordering options.

First, we flatten the array using the default order (`'C'`), which reads the elements in row-major order. The resulting `flattened_arr_c` is `[1 2 3 4 5 6]`, where the elements are read row by row.


Next, we flatten the array using Fortran order (`'F'`). The resulting `flattened_arr_f` is `[1 4 2 5 3 6]`, where the elements are read column by column.


We print the original array `arr` and the flattened arrays `flattened_arr_c` and `flattened_arr_f` to see the difference in the ordering of the elements.

Frequently Asked Questions

What is the difference between numpy.ravel() and numpy.flatten()?

Both functions flatten a multi-dimensional array, but numpy.ravel() returns a view of the original array, while numpy.flatten() returns a new copy of the flattened array.

Can numpy.ravel() handle arrays with more than 2 dimensions?

Yes, numpy.ravel() can flatten arrays with any number of dimensions into a one-dimensional array.

What happens if I modify the flattened array returned by numpy.ravel()?

Since numpy.ravel() returns a view of the original array, any modifications made to the flattened array will also affect the original array.

Conclusion

In this article, we have learned about the numpy.ravel() function in Python. We discussed its syntax, parameters, and return value. We also looked at the different type of examples to show how to use numpy.ravel() to flatten multi-dimensional arrays and how the order parameter affects the flattening process. numpy.ravel() is a convenient way to convert a multi-dimensional array into a one-dimensional array, which can be useful in various scenarios when working with NumPy arrays.

You can also check out our other blogs on Code360.

Live masterclass