Updated April 13, 2023
Introduction to NumPy unravel_index
In Python, NumPy provides a function unravel_index() function to make flatten indexed array into a tuple of elements or coordinates of each item of the multidimensional arrays which gives us the row and column coordinates together in the means of the output of this function, which in general gives us the idea of where the items of the elements are present with the exact position of row and column. In general, we can define the unravel_index() a function of NumPy to result in the element’s coordinates of the given array which converts any flatten index to get a tuple which consists of coordinates array.
Working of NumPy unravel_index() Function
In this article, we will discuss the unravel_index() method provided by the NumPy module in Python programming language for converting the array of flat indices into coordinates consisting in a tuple holding the coordinates of each element of the array. In general, the unravel_index() method takes in the array indices the shape or size of the array along with the specification of the row or column-wise arrangement of elements in the array. There is another function that does the opposite of this function known as ravel_multi_index which converts the multidimensional array to a dimensional array which takes both input and output as indices. In the below section let us see the syntax and example for the unravel_index() function.
import numpy numpy.unravel_index(ind_arr, shp_arr, order);
In the above syntax, we can see this function takes 3 parameters which are
- Ind_arr – In this parameter, we specify the indices of the array elements which is an integer value which is already flattened values of size of array’s dimensional.
- Shp_arr – This parameter to specify the size or shape of the array to store the unraveling indices obtained after applying the function.
- Order- This parameter is optional and specifies the ordering of the elements in row-wise then specify it as “C” style if column-wise then specify “F” (Fortan-style). By default, if we do not specify the order it will take C style.
In the below section let us consider an example of the unravel_index() function of the Numpy module.
import numpy as ed print("Program to demonstrate unravel_index() function") print("\n") print("The elements of the array are:") a = [9, 17, 28] print(a) print("\n") print("The size of the array it should be used for conversion is ") b= (6,5) print(b) print("\n") res = ed.unravel_index(a, b) print("The output of the unravel_index() is") print(res)
In the above program, we can see have first imported NumPy module to use the unravel_index() function. Then we are declaring few items of the array which is stored in the variable “a” to get their coordinates of rows and columns together in the form of a tuple and then we are declaring the size of the array stored in the variable “b” to be converted from flattened array. In the above program we then call the function and pass these both variables “a = [9, 17, 28]” and “b = (6,5)” to get the output as shown in the above program. We get the coordinates of element “9” as (1,4), for element “17” as (3,2), and element “28” as (5,3). So the output would result in the combination of these coordinates with rows and columns such as the tuple = (array ([1, 3, 5]), array ([4, 2, 3])). In this program we are specifying the order so by default it will take “C” style, therefore the elements here are placed row-wise when we see an array in (6,5) size. So to specify the order in the “F” style parameter the above program needs to be modified when we are calling the function such as
res = ed.unravel_index(a, b, order = 'F') print(res)
So the above line of the above code needs to be modified in the above program and the output will be as shown in the below screenshot.
In the above output, we can we get an output as a tuple of arrays such as (array([3, 5, 4]), array([1, 2, 4])) where the output is different from the above because here we have changed the order as “F”. Therefore in this it will arrange the elements of an array in (6, 5) column-wise.
Now we will see below the exact working of how the unravel_ index() arranges the indices of the given array.
The elements of the array are [9, 17, 28] and the size of the array to be converted is (6, 5) which specifies the dimension of the target array from which we need to fetch the indices.
So if the array is flattened which means if no dimension is mentioned then it would like below:[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
And now we want to convert this above flatten array to (6, 5) dimensional array so this will look like below:[[ 0, 1, 2, 3, 4], [5, 6, 7, 8, 9 ] , [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27, 28, 29]]
So the bold numbers are the indices of the array to unflattened the above-flattened array. So in the above array indices of the array are placed row-wise when “C” style having 6 rows starting from (0, 1, 2, 3, 4, 5) rows and 5 columns as (0, 1, 2, 3, 4). So the coordinates of the given indices elements of the above (6, 5) dimensional array are (1,4) for 9, (3, 2) for 17, (5,3) for 28 and therefore the output array of tuple would be (array ([1, 3, 5]), array ([4, 2, 3])).
Suppose the order is “F” then the above (6, 5) array would be arranged in column-wise as shown below:[[0, 6, 12, 18, 24] [1, 7, 13, 19, 25] [2, 8, 14, 20, 26] [3, 9, 15, 21, 27] [4, 10, 16, 22, 28] [5, 11, 17, 23, 29]]
So the above array is in Order “F” style so the bold numbers are the indices elements of the array and the coordinates are (3,1) for 9, (5,2) for 17 and (4,4) for 28 and output results as (array([3, 5, 4]), array([1, 2, 4])).
In this article, we conclude that the NumPy module of Python provides a function for converting the flattened array of indices as elements to the tuple of arrays contains the coordinates of these indices placed in the given dimensions of the array. In this article, we saw how to use the unravel_index() function to unflatten the flattened array of indices with an example for both order “C” row-wise and “F” column-wise with program and demonstration.
This is a guide to NumPy unravel_index. Here we also discuss the introduction and Working of NumPy unravel_index() function with Examples. You may also have a look at the following articles to learn more –