Combination and segmentation of arrays

Source: Internet
Author: User

Import NumPy as NP

A1=np.arange (+). Reshape (bis)

A2=np.arange (2,34,2). Reshape (bis)

A1

OUT[10]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15]])

A2

OUT[11]:

Array ([[2, 4, 6, 8],

[10, 12, 14, 16],

[18, 20, 22, 24],

[26, 28, 30, 32]])

1. Horizontal combination of arrays

Two functions np.hstack and np.concatenate(Axis=1)

Conditions of Use: 1. The dimensions of the array must be the same; 2. The number of rows must be the same.

Np.hstack ((A1,A2))

OUT[13]:

Array ([[0, 1, 2, 3, 2, 4, 6, 8],

[4, 5, 6, 7, 10, 12, 14, 16],

[8, 9, 10, 11, 18, 20, 22, 24],

[12, 13, 14, 15, 26, 28, 30, 32]]

Np.concatenate ((A1,A2), Axis=1)

OUT[15]:

Array ([[0, 1, 2, 3, 2, 4, 6, 8],

[4, 5, 6, 7, 10, 12, 14, 16],

[8, 9, 10, 11, 18, 20, 22, 24],

[12, 13, 14, 15, 26, 28, 30, 32]]

2. Vertical combination

Two functions np.vstack and np.concatenate (axis=0)

Conditions of Use:

The vertical combination of vstack functions, the number of arrays must be the same, and this rule is suitable for one-dimensional arrays.

Np.vstack ((A1,A2))

OUT[16]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[2, 4, 6, 8],

[10, 12, 14, 16],

[18, 20, 22, 24],

[26, 28, 30, 32]])

Np.concatenate ((A1,A2), axis=0)

OUT[17]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[2, 4, 6, 8],

[10, 12, 14, 16],

[18, 20, 22, 24],

[26, 28, 30, 32]])

3. Combination along the longitudinal axis

Conditions of Use:

The array must be equal to the number of rows in each array by the Dtack function for the vertical docking of the arrays along the axes.

Np.dstack ((A1,A2))

OUT[18]:

Array ([[[[0, 2],

[1, 4],

[2, 6],

[3, 8]],

[4, 10],

[5, 12],

[6, 14],

[7, 16]],

[8, 18],

[9, 20],

[10, 22],

[11, 24]],

[12, 26],

[13, 28],

[14, 30],

[15, 32]])

4. Column Combinations

#通过 the column_stack function to combine two one-dimensional arrays, the process is to first transpose them into rows and then combine them by columns.

T1 = Np.array ([1,2,3,4])

t2 = Np.array ([5,6,7,8])

Np.column_stack ((T1,T2))
OUT[41]:
Array ([[1, 5],
[2, 6],
[3, 7],
[4, 8]])

#通过column_stack函数对两个二维数组进行列向组合, we require that both arrays have the same number of rows.

Np.column_stack ((A1,A2))

OUT[19]:

Array ([[0, 1, 2, 3, 2, 4, 6, 8],

[4, 5, 6, 7, 10, 12, 14, 16],

[8, 9, 10, 11, 18, 20, 22, 24],

[12, 13, 14, 15, 26, 28, 30, 32]]

#通过column_stack对一个一维数组和二维数组进行列向组合, we simply require that the length of a one-dimensional array be the same as the number of rows in a two-dimensional array.

Np.column_stack ((A1,T1))
OUT[42]:
Array ([[0, 1, 2, 3, 1],
[4, 5, 6, 7, 2],
[8, 9, 10, 11, 3],
[12, 13, 14, 15, 4]])

5. Line combinations

Np. Row_stack ((A1,A2))

OUT[20]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[2, 4, 6, 8],

[10, 12, 14, 16],

[18, 20, 22, 24],

[26, 28, 30, 32]])

1. Vertical segmentation

Np.hsplit (a1,4)

OUT[22]:

[Array ([[0],

[4],

[8],

[[1]]), Array (

[5],

[9],

[]]), Array ([[2],

[6],

[10],

[+]]), Array ([[3],

[7],

[11],

[15]])

2. Split horizontally:

Np.vsplit (a1,4)

OUT[24]:

[Array ([[0, 1, 2, 3]]),

Array ([[4, 5, 6, 7]]),

Array ([[8, 9, 10, 11]]),

Array ([[[12, 13, 14, 15]])]

#水平分割不能切一维数组

#通过vspilt函数对二维数组进行分割, the second position of the function is the number of blocks divided into. As long as the number of rows in the array is divisible by the number of blocks divided, we can split the array into a given number of blocks

Combination and segmentation of arrays

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.