Numpy array dimensions
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:21 Accepted answer (Score 571)
00:35 Answer 2 (Score 86)
02:01 Answer 3 (Score 48)
02:23 Answer 4 (Score 21)
02:37 Thank you
--
Full question
https://stackoverflow.com/questions/3061...
Accepted answer links:
[.shape]: http://docs.scipy.org/doc/numpy/referenc...
Answer 2 links:
[numpy doc]: https://docs.scipy.org/doc/numpy/user/qu...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #numpy #dimensions
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:21 Accepted answer (Score 571)
00:35 Answer 2 (Score 86)
02:01 Answer 3 (Score 48)
02:23 Answer 4 (Score 21)
02:37 Thank you
--
Full question
https://stackoverflow.com/questions/3061...
Accepted answer links:
[.shape]: http://docs.scipy.org/doc/numpy/referenc...
Answer 2 links:
[numpy doc]: https://docs.scipy.org/doc/numpy/user/qu...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #arrays #numpy #dimensions
#avk47
ACCEPTED ANSWER
Score 583
Use .shape to obtain a tuple of array dimensions:
>>> a.shape
(2, 2)
ANSWER 2
Score 93
First:
By convention, in Python world, the shortcut for numpy is np, so:
In [1]: import numpy as np
In [2]: a = np.array([[1,2],[3,4]])
Second:
In Numpy, dimension, axis/axes, shape are related and sometimes similar concepts:
dimension
In Mathematics/Physics, dimension or dimensionality is informally defined as the minimum number of coordinates needed to specify any point within a space. But in Numpy, according to the numpy doc, it's the same as axis/axes:
In Numpy dimensions are called axes. The number of axes is rank.
In [3]: a.ndim # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2
axis/axes
the nth coordinate to index an array in Numpy. And multidimensional arrays can have one index per axis.
In [4]: a[1,0] # to index `a`, we specific 1 at the first axis and 0 at the second axis.
Out[4]: 3 # which results in 3 (locate at the row 1 and column 0, 0-based index)
shape
describes how many data (or the range) along each available axis.
In [5]: a.shape
Out[5]: (2, 2) # both the first and second axis have 2 (columns/rows/pages/blocks/...) data
ANSWER 3
Score 50
import numpy as np
>>> np.shape(a)
(2,2)
Also works if the input is not a numpy array but a list of lists
>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)
Or a tuple of tuples
>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)
ANSWER 4
Score 22
Use .shape:
In: a = np.array([[1,2,3],[4,5,6]])
In: a.shape
Out: (2, 3)
In: a.shape[0] # x axis
Out: 2
In: a.shape[1] # y axis
Out: 3