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...