Python array concatenation, combination, connection

Source: Internet
Author: User

Transferred from: Https://www.douban.com/note/518335786/?type=like

============ changing the dimension of an array ==================
Known reshape function can have a one-dimensional array to form a multidimensional array
Ravel function to flatten an array
B.ravel ()
The flatten () function can also achieve the same function
Difference: Ravel only provides view views, while flatten allocates memory storage

Reshaping: Setting a dimension with a meta-ancestor
>>> b.shape= (4,2,3)
>>> b
Array ([[0, 1, 2],
[3, 4, 5],

[6, 7, 8],
[9, 10, 11],

[12, 13, 14],
[15, 16, 17],

[18, 19, 20],
[21, 22, 23]])




Transpose:
>>> b
Array ([0, 1],
[2, 3])
>>> B.transpose ()
Array ([0, 2],
[1, 3])

A combination of ============= arrays ==============
>>> A
Array ([0, 1, 2],
[3, 4, 5],
[6, 7, 8])
>>> B = a*2
>>> b
Array ([0, 2, 4],
[6, 8, 10],
[12, 14, 16])

1. Horizontal Combination
>>> Np.hstack ((b))
Array ([0, 1, 2, 0, 2, 4],
[3, 4, 5, 6, 8, 10],
[6, 7, 8, 12, 14, 16]
>>> Np.concatenate ((A, B), Axis=1)
Array ([0, 1, 2, 0, 2, 4],
[3, 4, 5, 6, 8, 10],
[6, 7, 8, 12, 14, 16]

2. Vertical combination
>>> Np.vstack ((b))
Array ([0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 2, 4],
[6, 8, 10],
[12, 14, 16])
>>> Np.concatenate ((A, B), axis=0)
Array ([0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 2, 4],
[6, 8, 10],
[12, 14, 16])

3. Depth combination: Along the longitudinal axis direction
>>> Np.dstack ((b))
Array ([[0, 0],
[1, 2],
[2, 4],

[3, 6],
[4, 8],
[5, 10],

[6, 12],
[7, 14],
[8, 16]])

4. Column Combination Column_stack ()
One-dimensional arrays: combination by column direction
Two-dimensional arrays: Same as Hstack

5. Line Combination Row_stack ()
Think of arrays: Group by row direction
Two-dimensional arrays: Same as Vstack

6.== to compare two arrays
>>> a==b
Array ([True, False, false],
[False, False, false],
[False, False, false], Dtype=bool)
#True那个因为都是0啊

================== the segmentation of an array ===============
>>> A
Array ([0, 1, 2],
[3, 4, 5],
[6, 7, 8])
>>> B = a*2
>>> b
Array ([0, 2, 4],
[6, 8, 10],
[12, 14, 16])

1. Horizontal segmentation (not vertical division??? )
>>> Np.hsplit (a,3)
[Array ([0],
[3],
[6]),
Array ([1],
[4],
[7]),
Array ([2],
[5],
[8]]
Split (A,3,axis=1) to achieve the same goal

2. Vertical segmentation
>>> Np.vsplit (a,3)
[Array ([0, 1, 2]), Array ([3, 4, 5]), Array ([6, 7, 8])]

Split (a,3,axis=0) to achieve the same goal

3. Deep segmentation
A three-dimensional array:::
>>> d = np.arange (reshape) (3,3,3)
>>> D
Array ([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],

[9, 10, 11],
[12, 13, 14],
[15, 16, 17],

[18, 19, 20],
[21, 22, 23],
[24, 25, 26]])

Deep segmentation (i.e. split in the direction of depth)
Note: Dsplite only works on arrays of more than 3 dimensions
Raise ValueError (' dsplit works on arrays of 3 or more dimensions ')
Valueerror:dsplit only works on arrays of 3 or more dimensions

>>> Np.dsplit (d,3)
[Array ([[0],
[3],
[6],

[9],
[12],
[15],

[18],
[21],
[[[1]]), Array (
[4],
[7],

[10],
[13],
[16],

[19],
[22],
[+]]), Array ([[2],
[5],
[8],

[11],
[14],
[17],

[20],
[23],
[26]])

=================== the properties of an array =================
>>> A.shape #数组维度
(3, 3)
>>> A.dtype #元素类型
Dtype (' int32 ')
>>> a.size #数组元素个数
9
>>> a.itemsize #元素占用字节数
4
>>> a.nbytes #整个数组占用存储空间 =itemsize*size
36
>>> A.T #转置 =transpose
Array ([0, 3, 6],
[1, 4, 7],
[2, 5, 8])

Flat property
......

Python array concatenation, combination, connection

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.