7

Im a newbie to Numpy and trying to understand the basic question of what is dimension,

I tried the following commands and trying to understand why the ndim for last 2 arrays are same?

>>> a= array([1,2,3])
>>> a.ndim
1
>>> a= array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> a.ndim
2
>>> a=arange(15).reshape(3,5)
>>> a.ndim
2

>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

My understanding..

Case 1:
    array([[1, 2, 3],
           [4, 5, 6]])

2 elements are present in main lists, so ndim is-2

Case 2:
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])

3 elements are present in the main lists, do ndim is-3

2
  • would be easier to explain if you mentioned what you expected them to be Commented Apr 15, 2013 at 14:52
  • ndim means "number of dimensions". a 2D array has ndim=2, a 3D array has ndim=3, etc. Commented Apr 23, 2013 at 15:11

2 Answers 2

13

The shape of an array is a tuple of its dimensions. An array with one dimension has a shape of (n,). A two dimension array has a shape of (n,m) (like your case 2 and 3) and a three dimension array has a shape of (n,m,k) and so on.

Therefore, whilst the shape of your second and third example are different, the no. dimensions is two in both cases:

>>> a= np.array([[1,2,3],[4,5,6]])
>>> a.shape
(2, 3)

>>> b=np.arange(15).reshape(3,5)
>>> b.shape
(3, 5)

If you wanted to add another dimension to your examples you would have to do something like this:

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

or

np.arange(15).reshape(3,5,1)

You can keep adding dimensions in this way:

One dimension:

>>> a = np.zeros((2))
array([ 0.,  0.])
>>> a.shape
(2,)
>>> a.ndim
1

Two dimensions:

>>> b = np.zeros((2,2))
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> b.shape
(2,2)
>>> b.ndim
2

Three dimensions:

>>> c = np.zeros((2,2,2))
array([[[ 0.,  0.],
        [ 0.,  0.]],

       [[ 0.,  0.],
        [ 0.,  0.]]])
>>> c.shape
(2,2,2)
>>> c.ndim
3

Four dimensions:

>>> d = np.zeros((2,2,2,2))
array([[[[ 0.,  0.],
         [ 0.,  0.]],

        [[ 0.,  0.],
         [ 0.,  0.]]],


       [[[ 0.,  0.],
         [ 0.,  0.]],

        [[ 0.,  0.],
         [ 0.,  0.]]]])
>>> d.shape
(2,2,2,2)
>>> d.ndim
4
Sign up to request clarification or add additional context in comments.

Comments

0

While @atomh33ls has a fantastic written answer, I generated this visual to help communicate the differences between a numpy.ndarray.shape and a numpy.ndarray.ndim.

In this visual, the ndim increases as each new slider moves above 1. This is first seen when m is set to 1 defining a 1-dimensional ndarray with shape (1,).

Note that I use m, n, and k in order to represent the length of each additional dimension.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.