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!