+ 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?
7 Respostas
+ 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/
+ 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).
+ 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
+ 2
Jayakrishna đźđł Does it mean that in my 4 dimentional array I have values only for one dimension?
+ 2
Yes.
It is in inner most list...
+ 1
Jayakrishna đźđł Thank you very much for your help!
+ 1
You're welcome..