[python] numpy library array splicing np.concatenate official document details and examples

In practice, we often encounter the problem of array splicing. concatenate based on numpy library is a very useful array operation function.

1,concatenate((a1, a2, … ), axis=0) details of official documents

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, you need to specify the splicing direction, which is axis = 0 by default, that is, to splice the array objects of axis 0 longitudinally (longitudinal splicing follows axis=1 direction); note: generally, axis = 0 is to operate the array of this axis, and the operation direction is another axis, that is 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. The same shape here can meet the requirement of the same shape between arrays in the axis direction of splicing

If the array object is spliced with axis= 1, the direction is the horizontal 0 axis, a is a 2 * 2-dimensional array, axis= 0 is 2, b is a 1 * 2-dimensional array, axis= 0 is 1, and the shapes of the two are different, 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

By transposing b, we get that 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]])

Keywords: IPython

Added by Christian B. on Tue, 05 May 2020 23:47:35 +0300