Python numpy implements array merge instances (vstack and hstack) and numpyvstack

Source: Internet
Author: User

Python numpy implements array merge instances (vstack and hstack) and numpyvstack

Several Arrays can be combined along different axes for simple usage of vstack and hstack,

>>> a = np.floor(10*np.random.random((2,2)))>>> aarray([[ 8., 8.],    [ 0., 0.]])>>> b = np.floor(10*np.random.random((2,2)))>>> barray([[ 1., 8.],    [ 0., 4.]])>>> np.vstack((a,b))array([[ 8., 8.],    [ 0., 0.],    [ 1., 8.],    [ 0., 4.]])>>> np.hstack((a,b))array([[ 8., 8., 1., 8.],    [ 0., 0., 0., 4.]])

The column_stack function converts a 1D array into a 2D array, which is equivalent to vertically arranging 1D arrays.

>>> from numpy import newaxis>>> np.column_stack((a,b))  # With 2D arraysarray([[ 8., 8., 1., 8.],    [ 0., 0., 0., 4.]])>>> a = np.array([4.,2.])>>> b = np.array([2.,8.])>>> a[:,newaxis] # This allows to have a 2D columns vectorarray([[ 4.],    [ 2.]])>>> np.column_stack((a[:,newaxis],b[:,newaxis]))array([[ 4., 2.],    [ 2., 8.]])>>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is differentarray([[ 4.],    [ 2.],    [ 2.],    [ 8.]])

For multi-dimensional arrays, hstack follows the second axis and vstack follows the first axis.

Summary

The above is all the content of the Python numpy Implementation of array merge instances in this article, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.