Python

ravel python numpy

As numpy is a python standard library that works with high-level mathematical operations like arrays and matrices with efficient calculation. It has a built-in function for manipulating arrays. This article will have a deep discussion on one of the numpy supported functions that are called NumPy.ravel() function.

It returns a contiguous flatten array which means it changes a two-dimensional array or multidimensional array into a single-dimensional array of the same type as that of the input array. Let’s have an example implementation of this ravel() function how it flattens arrays in different ways.

All the examples execute in the spyder terminal. Note that we have to import the NumPy module to access this ravel() function.

Syntax of numpy.ravel()

The syntax used in numpy.ravel() function is like this:

# Numpy.ravel(z , order=’c’)

Parameter passed of numpy.ravel()

Mainly, there are two parameters passed in this function: ‘ z’ and ‘order’. The ‘z’ is an input array that changes in a contiguous flatten array or in a one-dimensional array. The array elements are in the order specified by the ‘order’ parameter and packed into a one-dimensional array.

This order is an optional parameter that takes three arguments: C, F, and K. If we set an order as C, the array gets flattened in row-major, and by default, the “C” parameter is taken. If we set “F” then the array gets a flattened array in column-major. On the other hand, it is set in the “K” order which flattens the array in the same order as elements occur in memory.

Example 1:

In this example, we have a 2-dimensional array as an input array having different values with a variable assigned with the name ‘arr_1’. Then we will call the arr_1.ravel() function to flatten an array in another variable ‘arr_2’.

We can have input array values and flatten array values on the console screen through print statements.

import numpy as np

arr_1= np.array([[0, -8, 3], [15, 2, 1]])

arr_2=arr_1.ravel()

print("Displaying input array :\n",arr_1)

print("Displaying output array:\n",arr_2)

Finally, we have the converted 2-D array output in the 1-D array below.

Example 2:

Here in the example, we will show that the ravel() function is equal to the reshape() function. Firstly, we created a 2D array from array function np.array() then assigned my_arr.ravel() to a variable ‘output_arr’ that carries the flatten array; after that, we printed both input arrays and output array.

At last we applied my_arr.reshape(-1) in a variable reshape_arr. We have printed the reshape array and ravel() function array.

import numpy as np

my_arr = np.array([[99 ,15 ,56], [7, 63, -54]])

output_arr=my_arr.ravel()

print("Displaying input array :\n ",my_arr)

print("Displaying input array :\n ",output_arr)

reshape_arr = my_arr.reshape(-1)

print("Displaying reshape array : \n", my_arr)

As we have reshaped the array and flattened the array from the ravel() function, the output is shown in the screenshot below.

Example 3:

In this particular example, we are applying the numpy.ravel() function with the order ‘F’ that will set the 2D array in a column-major 1D array. We have a 2D array in a variable ‘F_arr’ and F_arr.ravel() function with the order equals to ‘F’ in another variable represented as F_output that will print the output as column-major.

At last, we have a printed statement of F_arr as input array and F_output array as output array.

import numpy as np

F_arr = np.array([[11 ,32, 23], [-4 ,58 ,88]])

F_output=F_arr.ravel('F')

print("Displaying input array :\n ",F_arr)

print("Displaying output array :\n ",F_output)

The output from the above code in the column-major array is displayed below.

Example 4:

This example uses ‘C’ as an order parameter that will convert the 2D array into a 1D array that is row-major. We have created a 2D array that carries array different values in it represented as ‘x_array’ variable.

After this we have the x_array.ravel() function taking the order parameter as ‘C’ that will give us a 1D array as row-major.

import numpy as np

x_array = np.array([[0 ,4 ,8], [1 ,5 ,9]])

y_array=x_array.ravel('C')

 

print("This is an input array :\n ",x_array)

print("This is an output array :\n ",y_array)

As an order parameter set to ‘C’ that gives us an output array into a flattened array.

Example 5:

In this example, take the order parameter and set it as ‘k’ to know how this order parameter works. For this, we have to take a 2D array and store the value of the array in a variable ‘k_array’ from an array function.

Then calling a k_arr.ravel() function and passing an order set as ‘k’ as an argument that will return a single line row from a 2D array. We can see the input array and result from an array with the order set as ‘k’ through a print statement. We have printed the arrays.

import numpy as np

 

k_array = np.array([[4 , 14 ,44], [5 ,15, 55]])

 

k_output=k_array.ravel('K')

 

print("Here is input array :\n",k_array)

print("Here is output array :\n",k_output)

At last, we have got the output array as a flattened array. We have output the screenshot below from the above code.

Example 6:

The last example is on using reshape with swapping its axes. Let’s implement this example this time; we have set the range of the array to a value 10 from an np.arange() function call. It will help in the reshape() function. As the reshape() function will reshape the number of dimensions without having a change in data.

We have also called swapaxes() function that will interchange axis from 0 to 1, then we have stored np.ravel() function that takes an input array and orders it as ‘C’ in variable ‘p’. We have taken different variables to set order {C, K, F} as p, q, and r. Printing the variable that will give different output for different order sets.

import numpy as np

my_array= np.arange(10).reshape(2,5).swapaxes(0,1)

 

p=np.ravel(my_array, order='C')

q=np.ravel(my_array, order='K')

r=np.ravel(my_array, order='F')

 

print("Output Array :\n",my_array)

print("Output Array of C-order:\n",p)

print("Output Array of K-order:\n",q)

print("Output Array of F_order:\n",r)

We have a different output below on the screen.

Conclusion

We have a deep discussion on all the order parameters of the ravel() function. We have seen how this function returns a flattened array from the two-dimensional array with a different example. Hopefully, these illustrations will assist you in comprehending the concept more perfectly.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content