Concatenation of one-dimensional arrays and two-dimensional arrays and addition of rows to two-dimensional arrays

Source: Internet
Author: User

Concatenation of one-dimensional arrays and two-dimensional arrays

NumPy has a lot of splicing functions. such as Hstack and Vstack. There are many such summary posts on the Internet. But the condition that two arrays can be spliced is the same dimension that satisfies two arrays. So when two-dimensional arrays and one-dimensional arrays are spliced, it is necessary to use Newaxis to convert one-dimensional arrays into two-dimensional arrays, that is, shape from (3,) to (3,1).

A = Np.array ([n/a]); b = Np.array ([[[1],[2],[3]]); #将一维数组a转化为二维数组a = A[:,np.newaxis];c = Np.concatenate ((b,a), Axis=1) Print c.shape//output as (3,2)

Two-dimensional arrays add rows

>>> A=np.array ([[2,4,3],[2,4,5]]) >>> B=np.array ([[8,7,6],[7,9,0]]) >>> C = np.concatenate ((b,a), axis=0) >>> CArray ([[8, 7, 6], [7, 9, 0], [2, 4, 3]       ,       [2, 4, 5]]) >>> C = np.concatenate (b), axis=0) >>> CArray ([[2, 4, 3], [2, 4, 5], [8, 7, 6]       ,       [7, 9, 0]])


Concatenation of one-dimensional arrays and two-dimensional arrays and addition of rows to two-dimensional 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.