Separate matrix columns into feature groups
Feature engineering
NumPy · Function
Splits an array into multiple horizontal sub-arrays. It uses axis 1 for multidimensional arrays and axis 0 for one-dimensional arrays.
Use np.hsplit to divide an array horizontally into a list of sub-arrays. For arrays with at least two dimensions it splits the second axis; for one-dimensional input it splits axis 0, so the operation remains useful for both vectors and matrices.
numpy.hsplit(ary, indices_or_sections)| Name | Type | Description |
|---|---|---|
ary | array_like | The array to split horizontally. |
indices_or_sections | int or 1-D array | An integer number of equal sections, or an array of split positions along the selected axis. |
A list of sub-arrays.
np.split with axis 1.import numpy as np
x = np.arange(12).reshape(3, 4)
left, right = np.hsplit(x, 2)
print(left)
print(right)Splits a 3-by-4 array into two 3-by-2 arrays by dividing its columns.
Feature engineering
Batch preparation
Structured arrays
np.hsplit do?np.hsplit(ary, indices_or_sections) returns a list of sub-arrays formed by horizontal splitting. It corresponds to np.split on axis 1 for multidimensional input, while one-dimensional input uses axis 0.
Follow-up: How would you split at particular column boundaries? Pass an array-like sequence of split indices instead of an integer section count.
np.hsplit use?For a two-dimensional array, horizontal splitting operates on the second axis, which is the columns axis. The rows remain in each result, while columns are distributed among the returned arrays.
Follow-up: What changes for a three-dimensional array? The same second-axis rule applies, so each result retains the other axes and receives a portion of axis 1.
The second argument can be an integer or split positions. An integer requests that many equal sections; positions specify boundaries along the selected axis and can produce differently sized pieces.
Follow-up: What should be checked before using an integer? Verify that the axis length is divisible by the requested number of sections.
numpy.splitnumpy.vsplitnumpy.array_split