Numpy&Pandas 005: Split

Let’s continue to see something for array — split.

As usual, make a array first.

1
2
3
4
5
A = np.arange(12).reshape((3,4))

#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
np.split(A,2,axis=1)	#into two arrays

#[array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]

np.split(A,4,axis=1) #into four arrays

#[array([[0],
# [4],
# [8]]), array([[1],
# [5],
# [9]]), array([[ 2],
# [ 6],
# [10]]), array([[ 3],
# [ 7],
# [11]])]

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
2
3
4
5
6
7
np.hsplit(A,2)

#[array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]

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
2
3
4
5
6
7
np.split(A,3,axis=0)

#[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]

np.vsplit(A,3)

#[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]

Non Equality Split

Sometimes, we wanna split it into non-equality array. It is also available to do that.

1
2
3
4
5
6
7
8
9
np.array_split(A,3,axis=1)

#[array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2],
# [ 6],
# [10]]), array([[ 3],
# [ 7],
# [11]])]

Successfully, but does it have some fixed thing inside?

Set a bigger array, let’s try

1
2
3
4
5
6
B = np.arange(24).reshape((4,6))

#array([[ 0, 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10, 11],
# [12, 13, 14, 15, 16, 17],
# [18, 19, 20, 21, 22, 23]])

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
np.array_split(B,4,axis=1)

#[array([[ 0, 1],
# [ 6, 7],
# [12, 13],
# [18, 19]]), array([[ 2, 3],
# [ 8, 9],
# [14, 15],
# [20, 21]]), array([[ 4],
# [10],
# [16],
# [22]]), array([[ 5],
# [11],
# [17],
# [23]])]

np.array_split(B,5,axis=1)

#[array([[ 0, 1],
# [ 6, 7],
# [12, 13],
# [18, 19]]), array([[ 2],
# [ 8],
# [14],
# [20]]), array([[ 3],
# [ 9],
# [15],
# [21]]), array([[ 4],
# [10],
# [16],
# [22]]), array([[ 5],
# [11],
# [17],
# [23]])]

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