Arrays can also be split into multiple arrays along the horizontal, vertical, and depth axes using the np.hsplit()
, np.vsplit()
, and np.dsplit()
functions. We will only look at the np.hsplit()
function as the others work similarly.
The np.hsplit()
function takes the array to split as a parameter, and either a scalar value to specify the number of arrays to be returned, or a list of column indexes to split the array upon.
If splitting into a number of arrays, each array returned will have the same count of columns. The source array must have a number of columns that is a multiple of the specified value.
To demonstrate this, we will use the following array with four columns and three rows:
In [70]: # sample array a = np.arange(12).reshape(3, 4) a Out[70]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
We can split this into four arrays, each representing the values in a specific column:
In [71]: # horiz split the 2-d array into 4 array columns np.hsplit(a, 4) Out[71]: [array([[0], [4], [8]]), array([[1], [5], [9]]), array([[ 2], [ 6], [10]]), array([[ 3], [ 7], [11]])]
Using a value of 2
returns two matrices with two columns each:
In [72]: # horiz split into two array columns np.hsplit(a, 2) Out[72]: [array([[0, 1], [4, 5], [8, 9]]), array([[ 2, 3], [ 6, 7], [10, 11]])]
Also, the following code splits an array along specific columns:
In [73]: # split at columns 1 and 3 np.hsplit(a, [1, 3]) Out[73]: [array([[0], [4], [8]]), array([[ 1, 2], [ 5, 6], [ 9, 10]]), array([[ 3], [ 7], [11]])]
The np.split()
function performs an identical task when using axis=1
:
In [74]: # along the rows np.split(a, 2, axis = 1) Out[74]: [array([[0, 1], [4, 5], [8, 9]]), array([[ 2, 3], [ 6, 7], [10, 11]])]
Vertical splitting works similarly to horizontal splitting, except against the vertical axis, which can be seen here:
In [75]: # new array for examples a = np.arange(12).reshape(4, 3) a Out[75]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])
We can split this by 4
and get the four arrays representing the rows:
In [76]: # split into four rows of arrays np.vsplit(a, 4) Out[76]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]]), array([[ 9, 10, 11]])]
Alternately, splitting by 2
, retrieving two arrays of two rows each:
In [77]: # into two rows of arrays np.vsplit(a, 2) Out[77]: [array([[0, 1, 2], [3, 4, 5]]), array([[ 6, 7, 8], [ 9, 10, 11]])]
Splitting can also be performed on specific rows:
In [78]: # split along axis=0 # row 0 of original is row 0 of new array # rows 1 and 2 of original are row 1 np.vsplit(a, [1, 3]) Out[78]: [array([[0, 1, 2]]), array([[3, 4, 5], [6, 7, 8]]), array([[ 9, 10, 11]])]
Likewise, the split command does the same when specifying axis=0
:
In [79]: # split can specify axis np.split(a, 2, axis = 0) Out[79]: [array([[0, 1, 2], [3, 4, 5]]), array([[ 6, 7, 8], [ 9, 10, 11]])]
Depth splitting splits three-dimensional arrays. To demonstrate this, we will use the following three-dimensional array:
In [80]: # 3-d array c = np.arange(27).reshape(3, 3, 3) c Out[80]: 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], [24, 25, 26]]])
This array can be depth split by 3
:
In [81]: # split into 3 np.dsplit(c, 3) Out[81]: [array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]])]
3.144.37.12