12

I'm a beginner to numpy with no experience in matrices. I understand basic 1d and 2d arrays but I'm having trouble visualizing a 3d numpy array like the one below. How do the following python lists form a 3d array with height, length and width? Which are the rows and columns?

b = np.array([[[1, 2, 3],[4, 5, 6]],
          [[7, 8, 9],[10, 11, 12]]])
3
  • 2
    once you enter 3D, the concept of rows and columns doesn't make much sense. You can refer this answer for building an intuition for higher dimensional arrays. stackoverflow.com/questions/22320534/… Commented Jan 11, 2018 at 6:33
  • Thanks for the reference. I understood the concept of axes in numpy. But in the 3d array above, why does the array consist of two lists each consisting of two lists again? How does that represent 3d? Commented Jan 11, 2018 at 6:42
  • I visualize 3d as planes or sheets of paper stacked on top of each other. Or a 3d chess board or a Qubic 3d tictactoe (1960s vintage game). But I've worked with multidimensional arrays long enough that I don't try to visualize them. They are their own 'reality'. Commented Jan 11, 2018 at 7:11

1 Answer 1

32

The anatomy of an ndarray in NumPy looks like this red cube below: (source: Physics Dept, Cornell Uni)

anatomy of nd array


Once you leave the 2D space and enter 3D or higher dimensional spaces, the concept of rows and columns doesn't make much sense anymore. But still you can intuitively understand 3D arrays. For instance, considering your example:

In [41]: b
Out[41]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In [42]: b.shape
Out[42]: (2, 2, 3)

Here the shape of b is (2, 2, 3). You can think about it like, we've two (2x3) matrices stacked to form a 3D array. To access the first matrix you index into the array b like b[0] and to access the second matrix, you index into the array b like b[1].

# gives you the 2D array (i.e. matrix) at position `0`
In [43]: b[0]
Out[43]: 
array([[1, 2, 3],
       [4, 5, 6]])


# gives you the 2D array (i.e. matrix) at position 1
In [44]: b[1]
Out[44]: 
array([[ 7,  8,  9],
       [10, 11, 12]])

However, if you enter 4D space or higher, it will be very hard to make any sense out of the arrays itself since we humans have hard time visualizing 4D and more dimensions. So, one would rather just consider the ndarray.shape attribute and work with it.


More information about how we build higher dimensional arrays using (nested) lists:

For 1D arrays, the array constructor needs a sequence (tuple, list, etc) but conventionally list is used.

In [51]: oneD = np.array([1, 2, 3,])    
In [52]: oneD.shape
Out[52]: (3,)

For 2D arrays, it's list of lists but can also be tuple of lists or tuple of tuples etc:

In [53]: twoD = np.array([[1, 2, 3], [4, 5, 6]])
In [54]: twoD.shape
Out[54]: (2, 3)

For 3D arrays, it's list of lists of lists:

In [55]: threeD = np.array([[[1, 2, 3], [2, 3, 4]], [[5, 6, 7], [6, 7, 8]]])

In [56]: threeD.shape
Out[56]: (2, 2, 3)

P.S. Internally, the ndarray is stored in a memory block as shown in the below picture. (source: Enthought)

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

@VaishnavMK I added little more information about how we build arrays using nested lists.
The increasing nesting logic clears up a lot of things. I was mainly confusing between dimensions of an array (spatial) with dimensions of a matrix. Silly.
Really lovely graphic (and great explanation)! Beats when I manually wrote an ascii diagram to explain it.

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.