0

I started looking into Numpy using a 'Python for data analysis'. Why is the array dimension for arr2d is "2", instead of "3". Also why is the dimension for arr3d "3", instead of "2".

I thought the dimension of the array is based on the number of rows? Or this doesn't apply to higher dimensional and multidimensional arrays?

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

arr2d.shape

Output: (3, 3)

arr2d.ndim

Output: 2

arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

arr3d.shape

Output: (2, 2, 3)

arr3d.ndim

Output: 3

  • 2
    You're thinking of the dimensionality of the space the data points are drawn from, not the dimensionality of the array itself. – user2357112 Feb 15 '21 at 15:12
  • 1
    Also, a 3D array doesn't have rows. That's a 2D concept. – user2357112 Feb 15 '21 at 15:13
  • @user2357112supportsMonica could you please explain further ? –  Feb 15 '21 at 15:14
  • 1
    The number of dimensions is equal to the number of values in `shape`, which you can see in the outputs. `arr2d` contains 3 * 3 = 6 numbers, `arr3d` contains 2 * 2 * 3 = 12 numbers. – mportes Feb 15 '21 at 15:19
  • @myrmica Thanks for the explanation, I now understand what you're saying. You get the dimension based on the shape. if the shape of an array n is ```(4,4)```, then it's dimension is '2' and if an array b has a shape of ```(2,3,4)```, then it's dimension is '3'. –  Feb 15 '21 at 15:38
  • 1
    A general analogy might be: `np.array([1,2,3])` is like a line and has one dimension; `np.array([[1,2,3],[4,5,6]])` is like a plane and has two dimensions; `np.array([[[1,2],[3,4]],[[5,6],[7,8]]])` is like a cube and has three dimension. – wwii Feb 15 '21 at 15:41
  • 1
    Maybe a visual representation would be helpful. I have added a basic intuitive answer to your question (with some advanced knowledge around strides if you are interested). – Akshay Sehgal Feb 15 '21 at 17:28

2 Answers2

0

well see basically the dimension of the array is not based on the number of rows basically it is based on the brackets i.e [] that you entered in np.array() method

see

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

in arr2d there are 2 brackets([[]]) or there are 2 opening brackets([[) or its has 2 closing brackets(]]) so its an 2D array of (3,3) i.e 3 rows and 3 columns

similarly

arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

in arr3d there are 3 brackets([[[]]]) or there are 3 opening brackets([[[) or or its has 3 closing brackets(]]]) so its an 3D array of (2,2,3) i.e its has 2 arrays of 2 rows and 3 columns

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
0

Numpy stores its ndarrays as contiguous blocks of memory. Each element is stored in a sequential manner every n bytes after the previous.

(images referenced from this excellent SO post)

So if your 3D array looks like this -

np.arange(0,16).reshape(2,2,4)

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

enter image description here

Then in memory its stores as -

enter image description here

When retrieving an element (or a block of elements), NumPy calculates how many strides (of 8 bytes each) it needs to traverse to get the next element in that direction/axis. So, for the above example, for axis=2 it has to traverse 8 bytes (depending on the datatype) but for axis=1 it has to traverse 8*4 bytes, and axis=0 it needs 8*8 bytes.

With this in mind, let's understand what dimensions are in numpy.

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr2d.shape, arr3d.shape)
(3, 3) (2, 2, 3)

These can be considered a 2D matrix and a 3D tensor respectively. Here is an intuitive diagram to show how this would look like.

enter image description here

A 1D numpy array with (ndims=1) is a vector, 2D is a matrix, and 3D is a rank 2 tensor which can be imagined as a cube. The number of values it can store is equal to - array.shape[0] * array.shape[1] * array.shape[2] which in your second case is 2*2*3.

Vector (n,)       -> (axis0,)                #elements
Matrix (m,n)      -> (axis0, axis1)          #rows, columns
Tensor2 (l,m,n)   -> (axis0, axis1, axis2)
Tensor3 (l,m,n,o) -> (axis0, axis1, axis2, axis3)
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51