7 Examples to Know About Numpy Ravel Function

Introduction

Python has many ways to restructure the array according to the person’s needs. But, there are some cases when we need a one-dimensional array rather than a two-dimensional array. In this type of problem, the numpy library provides a function by which we can convert a two-dimensional array into a one-dimensional, i.e., numpy.ndarray.ravel() The travel() function in the numpy library is one of the most essential and commonly used functions that helps unravel the data the user has presented.

What is the Numpy ravel() Method?

Numpy.ndarray.ravel() is used when returning a contiguous flattened array. It means a 1-D array with all the input elements and with the same type as it.

Syntax of Numpy Ravel

numpy.ravel(order = 'C')

Parameters

order: {‘C,’ ‘F,’ ‘A,’ ‘K’} – These are optional functions in the input. All the input array elements are read according to the index order. Firstly, If we use order= ‘C,’ it means the index order will be row-major. The order in memory is slow at the first index and fastest at the last index.

Secondly, If we use order = ‘F,’ it means the index order will be column-major. It is also known as ‘Fortran-style order,’ which changes the fastest at the first index and slowest at the last index. Remember, C and F do not consider the memory layout of the array and only refer to the order of axis indexing.

Thirdly, If we use order = ‘A,’ it means reading and writing the array elements in Fortran-style index order. If an array is Fortran contiguous in memory, it will take C-like order.

Fourthly, If we use order = ‘K,’ it will read the elements in the order they occur in memory. Except for reversing the data. By default, the order is always set to ‘C.’

Return value of Numpy Ravel

An array with the same type as the input array with the order as per your requirement.

Examples of Numpy Ravel Method

Let us understand the numpy ravel() function of the numpy module in detail with the help of examples:

1. Using a 2-D array in Numpy Ravel() Function

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.ravel()  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 4 7 2 5 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function. At last, we have printed the value of the output array.

2. To show that numpy.ravel() is equivalent to reshape

In this example, we will import the numpy module as an alias name np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function to convert a 2-D array into a 1-D array. and at last, we will apply the array.reshape() function.

#python program using a 2-d array
#Import numpy library 

import numpy as np 
 
arr = np.array([[1,4,7], [2,5,8]]) 
 
output=arr.ravel()  

print("Input Array : ",arr)
print("Output Array : ",output)

arr1 = arr.reshape(-1)
print("Reshape output : ", arr1)

Output:

Input Array :  [[1 4 7]
 [2 5 8]]
Output Array :  [1 4 7 2 5 8]
Reshape output :  [1 4 7 2 5 8]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function. Fourthly, we have printed the value of the output array. Fifthly, we have applied arr. reshape(-1) function and store the value into new arr1. Finally, we printed the output of the reshape function and saw that numpy.ravel() is equivalent to an array.reshape().

3. Using Order = ‘F’ As A Parameter in Numpy Ravel() Function

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function with the order = ‘F’ to convert a 2-D array into a 1-D array.

#python program using a 2-d array
#Import numpy library
#using order = 'F' 

import numpy as np 
 
arr = np.array([[1,2,3], [4,5,6]]) 
 
output=arr.ravel('F')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Output Array :  [1 4 2 5 3 6]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function. Fourthly, we have used the order ‘F’ to print the output as column-major. At last, we have printed the value of the output array.

4. Using Order = ‘C’ As A Parameter in Numpy Ravel() Function

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function with the order = ‘C’ to convert a 2-D array into a 1-D array.

#Import numpy library
#using order = 'C' 

import numpy as np 
 
arr = np.array([[1,2,3], [4,5,6]]) 
 
output=arr.ravel('C')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Output Array :  [1 2 3 4 5 6]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function’s value. Fourthly, we have used the order ‘C’ to print the output as row-major. At last, we have printed the value of the output array.

5. Using Order = ‘A’ As A Parameter in Numpy Ravel() Function

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function with the order = ‘A’ to convert a 2-D array into a 1-D array.

#Import numpy library
#using order = 'A' 

import numpy as np 
 
arr = np.array([[1,2,3], [4,5,6]]) 
 
output=arr.ravel('A')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Output Array :  [1 2 3 4 5 6]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function. Fourthly, we have used the order ‘A’ to print the output as row-major. At last, we have printed the value of the output array.

6. Using Order = ‘K’ As A Parameter in Numpy Ravel() Function

In this example, we will be importing the numpy module as np. Then, we will take the input array as a 2-D array. Finally, we will apply the numpy ravel() function with the order = ‘K’ to convert a 2-D array into a 1-D array.

#Import numpy library
#using order = 'K' 

import numpy as np 
 
arr = np.array([[1,2,3], [4,5,6]]) 
 
output=arr.ravel('K')  

print("Input Array : ",arr)
print("Output Array : ",output)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Output Array :  [1 2 3 4 5 6]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have created a 2-D array using the array function. Thirdly, we have created the variable as output and assigned it the ravel() function. Fourthly, we have used the order ‘K’ to print the output as row-major. At last, we have printed the value of the output array.

7. Using reshape() with swapping axis

In this example, we will be importing the numpy module as np. Then, we will arrange an array with the help of the reshape function and swap axes function.

#import numpy library

import numpy as np
x = np.arange(18).reshape(3,3,2).swapaxes(1,2)   

y=np.ravel(x, order='C')  
z=np.ravel(x, order='K')  
m=np.ravel(x, order='A')  

print(x)
print(y)
print(z)
print(m)

Output:

[[[ 0  2  4]
  [ 1  3  5]]

 [[ 6  8 10]
  [ 7  9 11]]

 [[12 14 16]
  [13 15 17]]]
[ 0  2  4  1  3  5  6  8 10  7  9 11 12 14 16 13 15 17]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17]
[ 0  2  4  1  3  5  6  8 10  7  9 11 12 14 16 13 15 17]

Explanation:

First, we have imported the numpy module with the alias name np. Secondly, we have arranged an array till 18 to help reshape() function and swapaxes() function. Thirdly, we have applied the ravel function with all the orders and stored them in the new variable. At last, we have printed all the variables and seen the difference in the output.

Difference between flatten() and Numpy ravel()

Both functions are used to convert a multi-dimensional array into a one-dimensional array. But, there are some differences through which they differ in themselves.

Numpy Ravel()

  • The Numpy Ravel function always returns the reference of the original array.
  • It is a library-level function.
  • Ravel is faster than flatten().
  • If the value is modified, the original array will also get affected.

Numpy Flatten()

  • Flatten method() always returns a copy of the original array.
  • It is a function of ndarray object.
  • It is slower than ravel as it takes more memory.
  • If the value is modified, the original array will not be affected.
#flatten and ravel function in python example

import numpy as np

arr = np.array([[1,2,3], [4,5,6]])
print("Input Array : ",arr)
print("Dimension of array : ",(arr.ndim))
print("\n")

arr1 = arr.ravel()
print("Ravel output : ",arr1)
arr1[0] = 50
print("updated ravel array : ",arr1)
print("Input array : ",arr)
print("\n")

arr2 = arr.flatten()
print("Flatten output : ",arr2)
arr2[0] = 100
print("Updated flatten array : ",arr2)
print("Input array : ",arr)

Output:

Input Array :  [[1 2 3]
 [4 5 6]]
Dimension of array :  2


Ravel output :  [1 2 3 4 5 6]
updated ravel array :  [50  2  3  4  5  6]
Input array :  [[50  2  3]
 [ 4  5  6]]


Flatten output :  [50  2  3  4  5  6]
Updated flatten array :  [100   2   3   4   5   6]
Input array :  [[50  2  3]
 [ 4  5  6]]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we have taken the input array and printed the input array and the array’s dimension. Thirdly, we have applied the ravel() function and printed the ravel() function’s output. We then updated the value of arr2[0] = 50 and printed the ravel output and the input array again. So we can easily see the changes. The same was done with the flatten() function, which updated the value, but the input array didn’t change.

Conclusion

In this tutorial, we have discussed the ravel() function of the numpy module. All the parameters and their values are explained in detail with the help of examples. Examples will help you to understand the concept more accurately. You can use the order per your output one-dimensional array requirement.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments