I'm currently trying to learn Numpy and Python. Given the following array:
import numpy as np
a = np.array([[1,2],[1,2]])
Is there a function that returns the dimensions of a
(e.g.a is a 2 by 2 array)?
size()
returns 4 and that doesn't help very much.
You can use .shape
You can use
.ndim
for dimension and.shape
to know the exact dimensionYou can change the dimension using
.reshape
functionFirst:
By convention, in Python world, the shortcut for
numpy
isnp
, so: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:
axis/axes
the nth coordinate to index an
array
in Numpy. And multidimensional arrays can have one index per axis.shape
describes how many data (or the range) along each available axis.
The
shape
method requires thata
be a Numpy ndarray. But Numpy can also calculate the shape of iterables of pure python objects:It is
.shape
:Thus:
a.shape
is just a limited version ofnp.info()
. Check this out:Out