"Python" numpy Library Array stitching np.concatenate official documents and examples

Source: Internet
Author: User

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

1, Concatenate ((A1, a2, ...), axis=0) 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 "the", by default).  Axis:int, optional the axis along which the arrays would be joined.

    Default is 0.

    Returns-------Res:ndarray the concatenated array.
    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 parameter

The parameter passed in must be a tuple or list of more than one array

In addition, you need to specify the direction of stitching, the default is axis = 0, which means that the 0-axis array object is vertically spliced (vertical stitching along the axis= 1 direction); Note: General axis = 0, which is the axis of the array to operate, the operation direction is another axis, namely Axis=1.

in [[]: A = Np.array ([[[1, 2], [3, 4]]) in

[]: b = Np.array ([[5, 6]]) in

[]: Np.concatenate ((A, B), axis=0) 
  OUT[25]:
Array ([[1, 2],
       [3, 4],
       [5, 6]])
Array passed in must have the same shape, where the same shape can be satisfies the same shape as the array on the axis axis of the stitching direction

If the array object is axis= 1-axis stitching, the direction is horizontal 0 axis, A is a 2*2-dimensional array, axis= 0 axis is 2,b is a 1*2-dimensional array, axis= 0 is 1, the two shapes are unequal, then the error

In [to]: Np.concatenate ((a,b), Axis = 1)
---------------------------------------------------------------------- -----
valueerror                                traceback (most recent) <ipython-input-27-aa1228decc36> in
<module > ()
----> 1 np.concatenate ((a,b), Axis = 1)

Valueerror:all the input array dimensions except for the Concate Nation axis must match exactly

Transpose B, get B as 2*1 dimension array:

in [[]: Np.concatenate (a,b.t), Axis = 1)
out[28]:
Array ([[1, 2, 5],
       [3, 4, 6]])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.