Numpy Ravel implementation in Python with Examples

Numpy Ravel Implementation in Python with Examples

Do you want to make any dimensional NumPy array to a single dimension? If yes then NumPy has a function to do so. And it is NumPy.ravel(). It returns a contiguous flattened array. In this post, you will learn how to apply the NumPy ravel method using various examples.

But before going to the demonstration part let’s know the syntax of it.

The syntax for the Numpy Ravel Function

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

Here are the explanation of the parameters.

a: Your input array.

order: Choose any one from the {‘C’,’F’, ‘A’, ‘K’} and it is optional.

You can learn more about it in Numpy Ravel Documentation. Let’s go to the implementation part of the ravel method.

Step by Step to Implement Numpy Ravel Function

Step 1: Import the NumPy library.

In my examples, I am using only the NumPy module. So let’s import them.

import numpy as np

Step 2:  Create a Two Dimensional Array

Let’s create a two-dimensional array for implementing the ravel() examples. You can create a NumPy array using the numpy.array() method. Execute the following code to create it.

array_2d = np.array([[1,2,3],[4,5,6],[7,8,9]])


Sample 2D Array for Numpy Ravel Method
Sample 2D Array for Numpy Ravel Method

Step 3: Apply these Examples.

Now for deep understanding, I have compiled great examples on it. Just follow each and every example.

Example 1: Simple Use of Numpy Ravel() method.

If you want to just flatten the NumPy array then simply pass that array inside the raven() method. By default, it accepts the “C” order. It means to index the elements row-wise.



Applying ravel() with order C
Applying ravel() with order C

Example 2: Return the flattened array using Order F

If you pass the order = “F” as an argument of ravel() then it will flatten the array column-wise. It means elements of the first column, then the second column, and so on.



Applying ravel() with order F
Applying ravel() with order F

Example 3: Preserving the ordering of the array.

If you use the order=”A” argument, then it will preserve the order of it. For example, suppose I transpose the original array. Then if I apply the ravel() method then it will output the original array.  Just run the lines of code to see it.



Applying ravel() with order A
Applying ravel() with order A

That’s all for now. Many readers has asked me to explain the difference between the numpy.flatten() and numpy.raven() method. Both the method returns the same list but there is one major difference. The flatten() method return the flatten array as a copy whereas ravel() method returns the view of the original array. If you modify the values of the returned array in ravel() it will also modify the original array. But in this case, it will not happen in flatten() method.

I hope you have understood the usage of the numpy ravel() function. If you have any queries then you contact us for more queries.



Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner