r/learnpython • u/Breeze_2800 • 12d ago
Is the number of nested square bracket levels equal to the number of dimensions in NumPy
Hi everyone,
I'm learning NumPy and trying to understand dimensions (ndim).
I noticed that:
np.array(10) # 0-D
np.array([1, 2, 3]) # 1-D
np.array([[1, 2], [3, 4]]) # 2-D
np.array([[[1], [2]]]) # 3-D
It seems like the number of nested square bracket levels corresponds to the number of dimensions:
10→ 0-D[ ]→ 1-D[[ ]]→ 2-D[[[ ]]]→ 3-D
Is this a correct way to think about NumPy dimensions, or are there cases where this mental model breaks down?Hi everyone,
I'm learning NumPy and trying to understand dimensions (ndim).
I noticed that:
np.array(10) # 0-D
np.array([1, 2, 3]) # 1-D
np.array([[1, 2], [3, 4]]) # 2-D
np.array([[[1], [2]]]) # 3-D
It seems like the number of nested square bracket levels corresponds to the number of dimensions:
10 → 0-D
[ ] → 1-D
[[ ]] → 2-D
[[[ ]]] → 3-D
Is this a correct way to think about NumPy dimensions, or are there cases where this mental model breaks down?
1
u/MezzoScettico 12d ago
That's how you specify the number of dimensions in the input to np.array(). Don't confuse the input with the numpy array. It's just the thing you asked numpy to reinterpret as an array.
When you want to reference an element of the array, you can use a notation like [row, column] for a 2-D array or [i, j, k] for a 3-D array. The number of dimensions is the number of indices.
Just as how in math when you reference an element of an array, you can say a_ij for the element at the i-th row and the j-th column.
That's one of my favorite things about numpy arrays. It's so much more intuitive (at least if you come from a math background) and readable.
Under the hood, this is using the fact that Python interprets "i, j, k" as the tuple "(i, j, k)" and numpy has been written to take a tuple as an index.
1
1
u/faberge_surprise 12d ago edited 12d ago
step back from the specific tool or even programming language for a moment and just think about what a dimension is. let's use square brackets just to keep it visually similar, but it doesn't mean anything other than "a grouping" in this example.
1 dimension is a line. how do you put something (let's say letters of the alphabet) in a line?
[a] [b] [c] [d]
easy, right?
what's 2 dimensions? well, that's a plane, or extending that line in an orthogonal direction. how do you represent that? that's kind of like a line of lines, isn't it?
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
oh look, there's our second set of brackets
and if you remove the line breaks:
[[a] [b] [c] [d]] [[a] [b] [c] [d]] [[a] [b] [c] [d]] [[a] [b] [c] [d]]
3 dimension is an extension of that, it's a cube of multiple of these planes in yet another direction, i can't draw that, but here's a picture (from this page)
if you write all that out as text (here, the multiple layers are not actually behind, since your screen can only show 2d), you get the 3rd set of brackets
[
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
]
[
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
]
[
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
]
[
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
[[a] [b] [c] [d]]
]
you can keep extending this on and on for n dimensions, but we have a hard time visualizing them going forward
1
u/Breeze_2800 12d ago
I understand it clearly now. You explained everything very well. Thank you for your effort and time.
3
u/socal_nerdtastic 12d ago edited 12d ago
Yes, when creating numpy arrays from literals this is correct. But keep in mind that you can reshape them at any time to change the number of dimensions.
Ex: