NumPy split divides an array into sections along a chosen axis

Dividing an array into sections begins with a question about boundaries. Do you want a fixed number of equally sized pieces, or pieces that end at specific positions? NumPy split supports both forms. Academia Master Trader explains the distinction in English for readers in Andorra. The examples use constructed arrays so that section sizes, entry order and axis choices can be checked directly.

An integer specifies the number of sections

After import numpy as np, define x = np.array([2,4,6,8,10,12]). The call parts = np.split(x,3) produces three arrays: [2,4], [6,8] and [10,12]. The integer three asks for three equal sections. It does not ask for sections containing three entries each.

The input length is six, and six divides equally into three sections of length two. The default axis is zero. For this one dimensional array, that axis follows the vector's single dimension. The returned object is a list of arrays, so parts[0] selects the first section and parts[2] selects the third.

NumPy split

A sequence specifies boundary positions

The expression np.split(x,[1,4]) returns [2], [4,6,8] and [10,12]. Positions one and four mark boundaries, using zero based indexing. The corresponding slices are x[:1], x[1:4] and x[4:]. The boundary entry belongs to the next section, rather than to the section that ends there.

Here, the sections have different lengths because explicit positions define the boundaries. Two boundary positions produce three sections. Use a sorted sequence of integer positions when this is the intended form. Before running the operation, write the implied slices to make the boundary interpretation clear, particularly when values happen to resemble their own positions.

Equal sections need equal division

With the six entry vector, np.split(x,4) cannot make four equal sections and raises ValueError. Split does not quietly distribute extra entries among the pieces when its second argument is an integer. The integer form and the boundary sequence form therefore express different requirements, not interchangeable ways of requesting approximate chunk sizes.

NumPy also provides a related function named array split for equal or near equal sections when division is uneven. Choose that behavior deliberately if the task allows section lengths to differ. Do not describe an uneven division as an equal one merely because every input entry appears somewhere in the result.

The chosen axis determines the direction

Define m = np.array([[1,2,3,4],[5,6,7,8]]), which has shape (2,4). The call np.split(m,2,axis=0) returns two arrays with shape (1,4). The first contains the first row, and the second contains the second row. Both retain two dimensions.

With np.split(m,2,axis=1), the results are [[1,2],[5,6]] and [[3,4],[7,8]]. Each has shape (2,2). The operation divides the column dimension while keeping both rows in each section. Axis choice describes a dimension of the input, so check that dimension's length before deciding whether equal division is possible.

Sections can share the original data

The returned sections are views into the original array. They should not be treated as independent copies. For example, after creating parts from x as above, assigning parts[0][0] = 99 also changes the first entry of x to ninety nine. Use a copy of a section when independent storage is required for later changes.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the selected axis, its length and whether the second argument names sections or boundaries. Then inspect the resulting shapes and consider whether shared storage is suitable for the next operation.