+ 1

ndim in python numpy

Hello, Could you please advice me why: n = np.array([1,4,5,6]) print(n.ndim) Returns: 1 But: n = np.array([[[[1,4,5,6]]]]) print(n.ndim) Returns: 4 What are dimensions in numpy and how are they useful?

25th Jul 2023, 12:45 PM
Ruslan Guliyev
Ruslan Guliyev - avatar
7 Answers
+ 3
ndim returns number of dimensions of array so there first one is single dimension array so returns 1 And second one returns 4 1) [ 1,2,3,4 ] 2) [ [ [ [1, 2,3,4 ] ] ] ] Dimensions includes rows and columns.. There arrays can be single dimensional or multidimensional... edit: hope this helps for more..... https://www.educba.com/multidimensional-array-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
25th Jul 2023, 2:51 PM
Jayakrishna šŸ‡®šŸ‡³
+ 3
First you can check the numpy documentation and read the explanation of the ndarray data type. https://numpy.org/doc/stable/reference/arrays.ndarray.html A one-dimensional array, is your garden variety regular array with a fixed size. You can imagine it as a line section. We can visualize a 2-dimensional array as a grid, like a spreadsheet. The 2 dims are the rows and the columns. And there is a value in each intersection. And a 3 dimensional array would look like Rubik's cube, it has also "depth" but the number of values can extend to any direction. Visualising more than 3 dimensions is difficult for us humans, but in science and in AI research it is not uncommon to work with many dimensions, also this is the case with data analysis. For example in financial analysis, one could compare revenue, cost and profit (first dim = account) compared to previous years (second dim = time) maybe split amond different companies or business unit (third dim = entity) and even compare actual vs budget (4th dim).
25th Jul 2023, 3:43 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Jayakrishna šŸ‡®šŸ‡³ Tibor Santa Thank you for your answers. It is still difficult for me to understand how np.array([[[[1,4,5,6]]]]) is interpreted. I have understood what dimensions are but how the numbers in the array are used for 4 dimensions. Would appreciate your help
25th Jul 2023, 7:30 PM
Ruslan Guliyev
Ruslan Guliyev - avatar
+ 2
Jayakrishna šŸ‡®šŸ‡³ Does it mean that in my 4 dimentional array I have values only for one dimension?
25th Jul 2023, 7:30 PM
Ruslan Guliyev
Ruslan Guliyev - avatar
+ 2
Yes. It is in inner most list...
27th Jul 2023, 3:20 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
Jayakrishna šŸ‡®šŸ‡³ Thank you very much for your help!
29th Jul 2023, 7:38 AM
Ruslan Guliyev
Ruslan Guliyev - avatar
+ 1
You're welcome..
29th Jul 2023, 9:26 AM
Jayakrishna šŸ‡®šŸ‡³