432

How do I get the dimensions of an array? For instance, this is 2x2:

a = np.array([[1, 2], [3, 4]])
cottontail
  • 10,268
  • 18
  • 50
  • 51
morgan freeman
  • 6,041
  • 3
  • 25
  • 32
  • 37
    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! – Eric O. Lebigot Jun 17 '10 at 14:40

10 Answers10

575

Use .shape to obtain a tuple of array dimensions:

>>> a.shape
(2, 2)
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 30
    Note: `shape` might be more accurately described as an *attribute* than as a *function*, since it is not invoked using function-call syntax. – Brent Bradburn Apr 26 '12 at 02:00
  • 19
    @nobar actually it is a _property_ (which is both an attribute and a function, really) – wim Jul 29 '14 at 12:53
  • 1
    @wim more specifically [property is a class](https://docs.python.org/3/library/functions.html#property). In the case of class properties (a property you put in your class), they are objects of type property exposed as an attribute of the class. An attribute, in python, [is the name following the dot](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces). – Pedro Rodrigues Nov 21 '19 at 21:28
  • 2
    If you really want to nitpick, it's a descriptor. Although `property` itself is a class, `ndarray.shape` is not a class, it's an instance of the property type. – wim Nov 21 '19 at 21:40
89

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
YaOzI
  • 16,128
  • 9
  • 76
  • 72
48
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)
OMRY VOLK
  • 1,429
  • 11
  • 24
  • `np.shape` first turns its argument into an array if it doesn't have the shape attribute, That's why it works on the list and tuple examples. – hpaulj Sep 20 '18 at 14:32
21

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
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Rhuan Caetano
  • 285
  • 3
  • 9
17

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

>>> var.ndim
2

>>> varshape
(2, 6) 

You can change the dimension using .reshape function:

>>> var_ = var.reshape(3, 4)

>>> var_.ndim
2

>>> var_.shape
(3, 4)
Ivan
  • 34,531
  • 8
  • 55
  • 100
Daksh Gupta
  • 7,554
  • 2
  • 25
  • 36
8

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

np.shape([[1,2],[1,2]])
aph
  • 1,765
  • 2
  • 19
  • 34
3

a.shape is just a limited version of np.info(). Check this out:

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

Out

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
prosti
  • 42,291
  • 14
  • 186
  • 151
1
rows = a.shape[0] # 2 
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4
0

Execute below code block in python notebook.

import numpy as np
a = np.array([[1,2],[1,2]])
print(a.shape)
print(type(a.shape))
print(a.shape[0])

output

(2, 2)

<class 'tuple'>

2

then you realized that a.shape is a tuple. so you can get any dimension's size by a.shape[index of dimention]

Pasindu Perera
  • 489
  • 3
  • 8
0

Since the dimensions of a numpy array is stored as the shape attribute, getattr() can also be used.

arr = np.arange(8).reshape(2,2,2)
getattr(arr, 'shape')   # (2, 2, 2)

It's useful if you need to get it along with a dynamic list of other properties. An example could be

properties = ['shape', 'ndim', 'size']
d = {prop: getattr(arr, prop) for prop in properties}
# {'shape': (2, 2, 2), 'ndim': 3, 'size': 8}
cottontail
  • 10,268
  • 18
  • 50
  • 51