1

I am new to NumPy. I'm having trouble figuring out how to look at a multi-dimensional array and just "know" it's shape. For example:

# a 3D array (two stacked 2D arrays)
c = np.array( [[[  0,  1,  2],               
                [ 10, 12, 13]],
               [[100,101,102],
                [110,112,113]]] )
c.shape # (2, 2, 3)

To figure out the shape in my head I've been starting with the innermost entity (a 3 element array) then work outwards (there are 2 of the 3 element arrays) and there are 2 of those matrices, so (2, 2, 3).

Is that how you do it?

4
  • 1
    I usually start backwards, so if c.shape is (2, 2, 3) I know there are two elements in the first dimension, 2 in the next, and 3 in the last. I don't frequently find myself just looking at an array, the nested brackets are hard to judge and for large arrays (more than a few dozen elements) the data isn't all displayed on screen anyhow. Commented Jul 17, 2017 at 14:57
  • Thank you @user2699 - that makes total sense to me. The example I used came directly from the official NumPy tutorial so there really wasn't much context. Commented Jul 17, 2017 at 14:59
  • 1
    You're welcome. A good read is the broadcasting rules explaining how numpy sees array shape for different operations: docs.scipy.org/doc/numpy/user/basics.broadcasting.html Commented Jul 17, 2017 at 15:02
  • 1
    You showed us a nested list. To predict the resulting shape I have to count '[' and elements. print(c) provides more help with indentation and blank lines. Commented Jul 17, 2017 at 15:56

1 Answer 1

1

Think of it as a cube or an excel spreadsheet. Worksheet 1 has 2 rows and 3 columns. Worksheet 2 also has 2 rows and 3 columns. Squash them together and you have a 2x3x2 3d matrix.

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

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.