python – Numpy array dimensions

The Question :

390 people think this question is useful

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.

The Question Comments :
  • A piece of advice: your “dimensions” are called the shape, in NumPy. What NumPy calls the dimension is 2, in your case (ndim). It’s useful to know the usual NumPy terminology: this makes reading the docs easier!

The Answer 1

527 people think this answer is useful

It is .shape:

Tuple of array dimensions.


>>> a.shape
(2, 2)

The Answer 2

71 people think this answer is useful


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]])


In Numpy, dimension, axis/axes, shape are related and sometimes similar concepts:


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


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)


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

The Answer 3

47 people think this answer is useful
import numpy as np   
>>> np.shape(a)

Also works if the input is not a numpy array but a list of lists

>>> a = [[1,2],[1,2]]
>>> np.shape(a)

Or a tuple of tuples

>>> a = ((1,2),(1,2))
>>> np.shape(a)

The Answer 4

18 people think this answer is useful

You can 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

The Answer 5

10 people think this answer is useful

You can use .ndim for dimension and .shape to know the exact dimension

var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])

# displays 2

# display 6, 2

You can change the dimension using .reshape function

var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]]).reshape(3,4)

#display 2

#display 3, 4

The Answer 6

7 people think this answer is useful

The shape method requires that a be a Numpy ndarray. But Numpy can also calculate the shape of iterables of pure python objects:


The Answer 7

2 people think this answer is useful

a.shape is just a limited version of Check this out:

import numpy as np
a = np.array([[1,2],[1,2]])


class:  ndarray
shape:  (2, 2)
strides:  (8, 4)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  False
data pointer: 0x27509cf0560
byteorder:  little
byteswap:  False
type: int32

The Answer 8

0 people think this answer is useful
rows = a.shape[0] # 2 
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4

Add a Comment