Tag Archives: np.concatenate

[Python] numpy library array splicing np.concatenate Detailed explanation and examples of official documents

In practice, we often encounter array splicers, and concatenate is a very useful array manipulation function based on the Numpy library.
1, Concatenate (A1, A2…) Axis =0) official document details

concatenate(...)
    concatenate((a1, a2, ...), axis=0)

    Join a sequence of arrays along an existing axis.

    Parameters
    ----------
    a1, a2, ... : sequence of array_like
        The arrays must have the same shape, except in the dimension
        corresponding to `axis` (the first, by default).
    axis : int, optional
        The axis along which the arrays will be joined.  Default is 0.

    Returns
    -------
    res : ndarray
        The concatenated array.

    See Also
    --------
    ma.concatenate : Concatenate function that preserves input masks.
    array_split : Split an array into multiple sub-arrays of equal or
                  near-equal size.
    split : Split array into a list of multiple sub-arrays of equal size.
    hsplit : Split array into multiple sub-arrays horizontally (column wise)
    vsplit : Split array into multiple sub-arrays vertically (row wise)
    dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
    stack : Stack a sequence of arrays along a new axis.
    hstack : Stack arrays in sequence horizontally (column wise)
    vstack : Stack arrays in sequence vertically (row wise)
    dstack : Stack arrays in sequence depth wise (along third dimension)

2. Parameters
The parameter passed in must be a tuple or list of multiple arrays. In addition, the direction of splicing should be specified. The default is Axis = 0, which means longitudinal splicing of array objects on axis 0 (longitudinal splicing along axis= 1). Note: Generally, Axis = 0 means to operate on the array of this axis, and the operation direction is another axis, namely Axis =1.

In [23]: a = np.array([[1, 2], [3, 4]])

In [24]: b = np.array([[5, 6]])

In [25]: np.concatenate((a, b), axis=0)
Out[25]:
array([[1, 2],
       [3, 4],
       [5, 6]])

The incoming array must have the same shape, and the same shape here is sufficient for the same shape between the arrays on the axis axis in the splicing direction
If the array object is splicing axis= 1, the direction is horizontal axis 0, a is a 2*2 dimensional array, Axis = 0 is 2, B is a 1*2 dimensional array, axis= 0 is 1, the shapes of the two are different, then an error will be reported

In [27]: np.concatenate((a,b),axis = 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-aa1228decc36> in <module>()
----> 1 np.concatenate((a,b),axis = 1)

ValueError: all the input array dimensions except for the concatenation axis must match exactly

Transpose B, and b is a 2*1 dimensional array:

In [28]: np.concatenate((a,b.T),axis = 1)
Out[28]:
array([[1, 2, 5],
       [3, 4, 6]])