Let’s continue to see something for array — split.
As usual, make a array first.
1 | A = np.arange(12).reshape((3,4)) |
Vertical Split
If we wanna split A into two arrays for example array1 is [[0,1],[4,5],[8,9]] and array2 is [[2,3],[6,7],[10,11]]
or into 4 arrays (array1: [[0],[4],[8]]
array2: [[1],[5],[9]]
array3: [[2],[6],[10]]
array4: [[3],[7],[11]]).
As we wanted, it is likely to split it vertically into some pieces.
We have known the axis of vertical is 1 and see the code:
1 | np.split(A,2,axis=1) #into two arrays |
Is it available into one array? Of course, BUT, what’s the meaning…
Another way to present it, a function call hsplit , that is true, horizon split to do that. I am doubt also, but it is same function. Let’s see.
1 | np.hsplit(A,2) |
Exactly same, if someone knows about it, pls make a comment…
For understanding, it likes a knife to cut the middle of the array for left to right.
Horizon Split
No words here, see code instancely
1 | np.split(A,3,axis=0) |
Non Equality Split
Sometimes, we wanna split it into non-equality array. It is also available to do that.
1 | np.array_split(A,3,axis=1) |
Successfully, but does it have some fixed thing inside?
Set a bigger array, let’s try
1 | B = np.arange(24).reshape((4,6)) |
Formally, it is possible to split into 6 arrays, 3 arrays, 2 arrays(equally column). So let’s split it into 4 arrays and 5 arrays
1 | np.array_split(B,4,axis=1) |
Maybe we have see something pop out. Firstly, two element into an element of the array, if it is not enough to have 4 arrays or 5 arrays, decrease the number of the element in the array. Bigger and bigger, it must be like as big as possible to set the first 2 or 3 or 1 array and then decrease to set the number we wanna have.
Error Split
Be carefully, sometimes, we will face some problem, as above, array B can not split into 4 arrays in equally vertical split. Think about why, and try more. You will see. Else, you will see:
ValueError: array split does not result in an equal division
Thanks for your reading, I hope it is useful for you.
End
Illustrator / Cagy
Text / Cagy
Editor / Cagy
Design / Cagy