Trending September 2023 # Working Of Numpy Unravel_Index() Function # Suggested October 2023 # Top 13 Popular | Uyenanhthammy.com

# Trending September 2023 # Working Of Numpy Unravel_Index() Function # Suggested October 2023 # Top 13 Popular

You are reading the article Working Of Numpy Unravel_Index() Function updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working Of Numpy Unravel_Index() Function

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Working of NumPy unravel_index() Function

Syntax:

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.

Examples

In the below section let us consider an example of the unravel_index() function of the Numpy module.

Example #1

Code:

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)

Output:

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.

Now we will see below the exact working of how the unravel_ index() arranges the indices of the given array.

Example #2

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,

[ 0, 1, 2, 3, 4, 5, 6, 7, 8,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 ] ,

[15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27, 28, 29]]

[[ 0, 1, 2, 3, 4], [5, 6, 7, 8,] , [10, 11, 12, 13, 14],[15, 16,, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27,, 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]]

[[0, 6, 12, 18, 24] [1, 7, 13, 19, 25] [2, 8, 14, 20, 26] [3,, 15, 21, 27] [4, 10, 16, 22,] [5, 11,, 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])).

Conclusion

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.

Recommended Articles

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 –

You're reading Working Of Numpy Unravel_Index() Function

Update the detailed information about Working Of Numpy Unravel_Index() Function on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!