I am struggling to understand two things from below matrix (numpy arrays):
How can I deduce from the np.stack(cart_indexing, axis=1) function that there are 5 dimensions? I am struggling to conceptually understand the (5, 2, 5) part. I see it as (rows, column numbers, dimensions).
What does axis = -1 really mean? How to understand it?
x = np.linspace(start=-10, stop=0, num=5, endpoint=True)
y = np.linspace(start=1, stop=10, num=5)
cart_indexing = np.meshgrid(x, y, indexing="xy") # cartesian indexing
>> [array([[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ],
[-10. , -7.5, -5. , -2.5, 0. ]]),
array([[ 1. , 1. , 1. , 1. , 1. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ],
[ 7.75, 7.75, 7.75, 7.75, 7.75],
[10. , 10. , 10. , 10. , 10. ]])]
np.stack(cart_indexing, axis=0)
>> array([[[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ],
[-10. , -7.5 , -5. , -2.5 , 0. ]],
[[ 1. , 1. , 1. , 1. , 1. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ],
[ 7.75, 7.75, 7.75, 7.75, 7.75],
[ 10. , 10. , 10. , 10. , 10. ]]])
np.stack(cart_indexing, axis=1)
>> array([[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 1. , 1. , 1. , 1. , 1. ]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 3.25, 3.25, 3.25, 3.25, 3.25]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 5.5 , 5.5 , 5.5 , 5.5 , 5.5 ]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 7.75, 7.75, 7.75, 7.75, 7.75]],
[[-10. , -7.5 , -5. , -2.5 , 0. ],
[ 10. , 10. , 10. , 10. , 10. ]]])
np.stack(cart_indexing, axis=1).shape
>> (5, 2, 5)
np.stack(cart_indexing, axis=-1)
>> array([[[-10. , 1. ],
[ -7.5 , 1. ],
[ -5. , 1. ],
[ -2.5 , 1. ],
[ 0. , 1. ]],
[[-10. , 3.25],
[ -7.5 , 3.25],
[ -5. , 3.25],
[ -2.5 , 3.25],
[ 0. , 3.25]],
[[-10. , 5.5 ],
[ -7.5 , 5.5 ],
[ -5. , 5.5 ],
[ -2.5 , 5.5 ],
[ 0. , 5.5 ]],
[[-10. , 7.75],
[ -7.5 , 7.75],
[ -5. , 7.75],
[ -2.5 , 7.75],
[ 0. , 7.75]],
[[-10. , 10. ],
[ -7.5 , 10. ],
[ -5. , 10. ],
[ -2.5 , 10. ],
[ 0. , 10. ]]])
np.stack(cart_indexing, axis=-1).shape
>> (5, 5, 2)
CodePudding user response:
It's not clear what you mean by
there are 5 dimensions
None of your arrays have 5 dimensions. You start with a list of 2 arrays with 2 dimensions;
for i in cart_indexing:
print(f"Shape:{i.shape}; Dimensions:{i.ndim}")
Shape:(5, 5); Dimensions:2
Shape:(5, 5); Dimensions:2
Notice here how you have 5 and 5 and 2.
Then, the axis parameter in your stack comes into play:
for i in range(3):
print(f"Stacked on axis {i} my array has {np.stack(cart_indexing, axis=i).ndim} dimensions and a shape of {np.stack(cart_indexing, axis=i).shape}")
Stacked on axis 0 my array has 3 dimensions and a shape of (2, 5, 5) #the 2 is in the (axis=)0th position
Stacked on axis 1 my array has 3 dimensions and a shape of (5, 2, 5) #the 2 is in the (axis=)1st position
Stacked on axis 2 my array has 3 dimensions and a shape of (5, 5, 2) #the 2 is in the (axis=)2nd position
Put another way, stacking adds a dimension along which the arrays are stacked. The axis parameter determines which dimension is created during stacking/along which dimension they are stacked
What does axis = -1 really mean?
Why does print("Hello world"[-1])
print "d"?
Or, in other words, if we want to count our dimensions from last to first:
for i in range(-3,0):
print(f"Stacked on axis {i} my array has {np.stack(cart_indexing, axis=i).ndim} dimensions and a shape of {np.stack(cart_indexing, axis=i).shape}")
Stacked on axis -3 my array has 3 dimensions and a shape of (2, 5, 5) #dimension that is third from last
Stacked on axis -2 my array has 3 dimensions and a shape of (5, 2, 5) #dimension that is second from last
Stacked on axis -1 my array has 3 dimensions and a shape of (5, 5, 2) #last dimesnion