>>> A = np.matrix(np.zeros(2, 3)))
>>> A.shape
(2, 3)
>>> A
matrix([[ 0., 0., 0.],
[ 0., 0., 0.]])
Does the matrix A
have two rows with three zeros or two columns with three zeros?
Shape of an Array The shape is a tuple of integers. These numbers denote the lengths of the corresponding array dimension. In other words: The "shape" of an array is a tuple with the number of elements per axis (dimension). In our example, the shape is equal to (6, 3), i.e. we have 6 lines and 3 columns.
The shape of a simple Python tuple or list can be obtained with the built-in len() function. len() will return an integer that describes the number of objects in the tuple or list.
Python numpy shape 0 shape is a tuple that always gives dimensions of the array. The shape is a tuple that gives you an indication of the no. of dimensions in the array. The shape function for numpy arrays returns the dimensions of the array.
numpy reports the shape of 3D arrays in the order layers, rows, columns.
A.shape
will return a tuple (m, n), where m is the number of rows, and n is the number of columns.
rows
, columns
are just the names we give, by convention, to the 2 dimensions of a matrix
(or more generally a 2d numpy array).
np.matrix
is, by definition, 2d, so this convention is useful. But np.array
may have 0, 1, 2 or more dimensions. For that these 2 names are less useful. For example if 1d, does it have rows or columns? If 3d, what do we call the last dimension, depth? or maybe the first is pages?
So don't put too much emphasis on the names. Most numpy
functions ask you to specify the 'axis', by number, 0, 1, 2 etc., not by name.
There may be further confusion if you load data from a csv file, and get a 1d array (one 'row' per line of the file) of dtype fields. Are fields the same as columns? Sort of, but not quite.
The matrix in question has 2 rows and 3 columns (it is of dimension 2x3), where each of the matrix elements is of value zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With