Ravel Function of Python Numpy Explained [3 Examples]

ravel function in python numpy library parameters explained with code examples

In this article, we’ll dig deep into the theory and practical implementation of the ravel function of Python Numpy library. From explaining the parameters of the ravel() function to its proper implementation using code examples, everything will be discussed step by step.

What is Ravel Function in Python Numpy?

ravel function in python numpy library parameters explained with code examples

python numpy ravel function python code examples

In our previous article, we learned how to convert a 1D array to 2D and a 2D array to 3D using reshape Numpy function. But the ravel function returns a contiguous flattened array. It converts a multi-dimensional array to a single array. The resulting 1D array will have all the items that were present in the multi-dimensional array. All the items of the 1D array generated by the Numpy ravel function will be set to be lined up in a single row.

Advantage of ravel() Function (Memory Efficient)

The ravel function is more memory efficient. Unlike similar functions like flatten(), the ravel function returns a flattened view of the original array data and always tries to avoid the creation of a new array in memory, if it’s really possible. Whereas flatten function creates a copy of the data every time.

Syntax

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

Parameters

The ravel function has 2 parameters.

  1. The first parameter ‘a‘ specifies the array that will be converted to a 1D/single array.
  2. The second parameter ‘order‘ is optional. It specifies the order of reading the array elements that have been given as input to the ravel function. The order parameter takes two values. ‘F‘ for Fortran-style (column-major) and ‘C‘ for C-style (row-major). The default value provided to the parameter ‘order‘ is ‘C‘.

Implementing ravel() Numpy Function (Examples)

Example 1: Convert 2D Array to 1D Array using ravel() Function

import numpy as np

ar_2D = np.array([[8, 2, 4], [9, 10, 265]])

arrFlattened = np.ravel(ar_2D)

print(arrFlattened)

Output

[  8   2   4   9  10 265]

In this code example, we’ve:

  • Imported the library of Numpy so we can use its ravel function.
  • Then we created a 2D Numpy array using the array function of NumPy.
  • After that, we called the ravel() function and passed it the 2D array as an argument to it. Also, we’ve stored the flattened array inside and variable and finally display it.
  • The output shows a single array that includes all the items of the specified 2D array.

Example 2: Changing Flattened Array Items Data

print('Before modification')
print(arrFlattened)

arrFlattened[3]=5008       // modifying the item at index 4 as index start from 0

print('After modification')
print(arrFlattened) 

print('After modification 2D array shows the same modifications')
print(ar_2D)

Output

Before modification
[  8   2   4   9  10 265]
After modification
[   8    2    4 5008   10  265]
After modification 2D array shows the same modifications
[[   8    2    4]
 [5008   10  265]]

We’ve used the same example 1 code in this section as well. We can see that the flattened array can be modified using the indexing method. The modified array can be seen in the above output section. Even the 2D array is also modified as seen in the output block. The reason is that the ravel Numpy functions return a view of the original array.

Example 3: Changing order of Array (order Parameter)

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

flattenedArrC= np.ravel(twoDArr,order='C')

flattenedArrF= np.ravel(twoDArr,order='F')

print('Order C')
print(flattenedArrC)

print('Order F')
print(flattenedArrF)

Output

Order C
[ 1  2  3  4  5  6  7  8  9 10]
Order F
[ 1  6  2  7  3  8  4  9  5 10]

In this code example, we’ve applied the order ‘C‘ C-style (row-major) and ‘F‘ Fortran-style (column-major). In order F, we can see that the items are taken column-wise while in order C, the items are specified row-wise. The default value assigned to the order parameter is ‘C‘.

Conclusion

In conclusion, we’ve learned about the theoretical concept and practical usage of the ravel function of Python Numpy. Proper code examples were used in this article to make you better understand how to use this function.

Thank you for reading this article.

Leave a Comment

Your email address will not be published. Required fields are marked *