Follow with me for a second:
a = np.zeros((4, 4, 3), 'int')
for n in range(4):
for m in range(4):
a[n, m] = n + m
Then we print(a.shape)
and get (4, 4, 3)
Finally, we print(a)
[[[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]]
[[1 1 1]
[2 2 2]
[3 3 3]
[4 4 4]]
[[2 2 2]
[3 3 3]
[4 4 4]
[5 5 5]]
[[3 3 3]
[4 4 4]
[5 5 5]
[6 6 6]]]
This looks confusing. What is going on?
Let's reformat this by hand by pasting into Sublime and rearranging:
[
[ [0 0 0] [1 1 1] [2 2 2] [3 3 3] ]
[ [1 1 1] [2 2 2] [3 3 3] [4 4 4] ]
[ [2 2 2] [3 3 3] [4 4 4] [5 5 5] ]
[ [3 3 3] [4 4 4] [5 5 5] [6 6 6] ]
]
First annoying thing to notice, is that the outermost [ ]
should be IGNORED.
As in:
[ # <- get rid of this
[ [0 0 0] [1 1 1] [2 2 2] [3 3 3] ]
[ [1 1 1] [2 2 2] [3 3 3] [4 4 4] ]
[ [2 2 2] [3 3 3] [4 4 4] [5 5 5] ]
[ [3 3 3] [4 4 4] [5 5 5] [6 6 6] ]
] # <- get rid of this
QUESTION:
Why did the printout give me this? I didn't specify the shape to be (1, 4, 4, 3), but looking at it clearly, that's what it's showing me. Am I right to consider this confusing and unnecessary? Sure, it's ONE array and maybe deserved to be presented as such but this is not helpful when it comes time to read the output of the array on the computer screen.
Second thing:
Let me just run thru these dimensions, one at a time, in my head and by hand. Again, please bear with me.
First axis / dimension / index, which is (4, )
:
4 containers
Second axis / dimension / index, which is ( , 4, )
::
4 containers nested within the prior dimension's containers:
Third axis / dimension / index, which is ( , , 3)
:
3 containers nested within the prior dimension's containers:
There are numbers in this innermost container of containers:
AND BOOM. Thanks for reading this far. But there was a setup / trick up there when I wrote '3 containers nested'...
QUESTION Why isn't the final above image correct? Because what python shows me instead is this:
What happened to the innermost brackets?
It's like a prank that numpy is pulling on us -- "You eyes need to focus on [ ]
for the first two dimensions...but after that, you need to forget about those brackets and instead now you are looking at some datum instead of a container."
My question is also a proposal that numpy's printouts are changed. This feels inconsistent and I just don't see the reason for it. I'm hoping someone can make a good argument on why both of these inconsistencies are actually consistent.