I can’t understand sense of axis parameter. I will explain the problem.
code:
np.split(b1,[1,3],axis=0)
results:
[array([[1, 2, 3, 4]]),
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]]),
array([[13, 14, 15, 16],
[17, 18, 19, 20]])]
code:
np.split(b1,[1,3],axis=1)
results:
[array([[ 1],
[ 5],
[ 9],
[13],
[17]]),
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15],
[18, 19]]),
array([[ 4],
[ 8],
[12],
[16],
[20]])]
When i saw this thought "okay if axis=0 it means operate for lines if axis=1 it means operate for column"
Then code:
v1=np.array([[26.50940987, 22.97569521, 22.85657049, 9.0536725 ],
[25.98586589, 29.063503 , 16.32117152, 53.37755382],
[23.57720158, 34.10171715, 24.7272822 , 26.84495276],
[19.56007871, 43.61342762, 45.83942094, 16.36201912]])
v1=np.sort(v1,axis=0)
print(v1)
results:
array([[19.56007871, 22.97569521, 16.32117152, 9.0536725 ],
[23.57720158, 29.063503 , 22.85657049, 16.36201912],
[25.98586589, 34.10171715, 24.7272822 , 26.84495276],
[26.50940987, 43.61342762, 45.83942094, 53.37755382]])
it sorted on column? when it axis=1 it sorted in line.
Why this is happenning? What is the thing i can’t understand?
>Solution :
With just 2 dimensions, the "word description" of what the axis parameter does can be ambiguous – at least in English. Do we sum on or over an axis. Sometimes it helps to look at 1d, and then skip to 3d (or more). Then it’s more obvious which axis is gone, and which one(s) remain.
np.sum also takes an axis; experiment with keepdims and a tuple of axes.
With the split of a (5,4) array, axis 0 makes (1,4),(2,4),(2,4); while axis 1 is (5,1),(5,2),(5,1). The axis says which dimension is split. That seems fairly clear to me, but may that’s because I’ve used it enough.
With the sort, axis 0 sorts rows, but each column individually. ‘sort on column’ is an ambiguous descriptor.
To summarize this – there is not one description that applies equally well across all cases. –
Here’s your array with sum:
In [213]: arr = np.arange(1,21).reshape(5,4)
In [214]: arr
Out[214]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]])
In [215]: arr.sum(axis=0)
Out[215]: array([45, 50, 55, 60])
In [216]: arr.sum(axis=0, keepdims=True) # (4,) result
Out[216]: array([[45, 50, 55, 60]]) # (1,4) result
In [217]: arr.sum(axis=1, keepdims=True)
Out[217]:
array([[10],
[26],
[42],
[58],
[74]]) # (1,5) result
Make a 3d array:
In [218]: arr.reshape(5,2,2).sum(axis=0, keepdims=True)
Out[218]:
array([[[45, 50],
[55, 60]]]) # (1,2,2)
In [219]: arr.reshape(5,2,2).sum(axis=(1,2), keepdims=True)
Out[219]:
array([[[10]],
[[26]],
[[42]],
[[58]],
[[74]]]) # (5,1,1)
Back to split with axis 0, for 3d array. Same 1,2,2 split of the size5 axis.
In [223]: [i.shape for i in np.split(arr.reshape(5,2,2),[1,3],axis=0)]
Out[223]: [(1, 2, 2), (2, 2, 2), (2, 2, 2)]