Home  >  Article  >  Backend Development  >  How to use np.vstack() and np.hstack() in Python

How to use np.vstack() and np.hstack() in Python

PHPz
PHPzforward
2023-04-18 13:04:032597browse

Here we introduce two methods of splicing arrays:

np.vstack(): stacking in the vertical direction

np.hstack(): tiling in the horizontal direction

import numpy as np
arr1=np.array([1,2,3])
arr2=np.array([4,5,6])
print np.vstack((arr1,arr2))
 
print np.hstack((arr1,arr2))
 
a1=np.array([[1,2],[3,4],[5,6]])
a2=np.array([[7,8],[9,10],[11,12]])
print a1
print a2
print np.hstack((a1,a2))

The results are as follows:

[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[[ 1 2]
[3 4]
[5 6]]
[[ 7 8]
[ 9 10]
[11 12]]
[[ 1 2 7 8 ]
[ 3 4 9 10]
[ 5 6 11 12]]

It needs to be emphasized here that when applying hstack, I was doing assignment1 on cs231n , I always get errors here in hstack! Only then did I realize that my previous studies were very superficial!

(1)np.hstack()

Function prototype: numpy.hstack(tup)

where tup is the sequence of arrays, tup: sequence of ndarrays

The arrays must have the same shape along all but the second axis,except 1-D arrays which can be any length.

Equivalent to: np .concatenate(tup, axis=1)

Example 1:

import numpy as np
brr1=np.array([1,2,3,4,55,6,7,77,8,9,99])
brr1_folds=np.array_split(brr1,3)
print brr1_folds
print brr1_folds[0:2]+brr1_folds[1:3]
print np.hstack((brr1_folds[:2]+brr1_folds[1:3]))
print brr1_folds[0:2]
print brr1_folds[1:3]
#print np.hstack((brr1_folds[0:2],brr1_folds[1:3]))

If the last line is not commented out, an error will occur;

[array([1, 2, 3, 4]), array([55, 6, 7, 77]), array([ 8, 9, 99])]
[array([1, 2, 3, 4]), array ([55, 6, 7, 77]), array([55, 6, 7, 77]), array([ 8, 9, 99])]
[ 1 2 3 4 55 6 7 77 55 6 7 77 8 9 99]
[array([1, 2, 3, 4]), array([55, 6, 7, 77])]
[array([55, 6, 7, 77 ]), array([ 8, 9, 99])]

The reason for the error is that the dimensions of my array are inconsistent. Just change it to , the plus sign is the splicing of list!

Example 2:

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))

The result is: It shows that the one-dimensional array hstack is arbitrary.

[1 2 3 3 4 3 4 5 8 6 6 7]

Example 3:

Indicates that our hstack must have the same second dimension:

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))
print np.hstack(([[1,2,3],[2,3,4]],[[1,2],[2,3]]))

Result:

[1 2 3 3 4 3 4 5 8 6 6 7]
[[1 2 3 1 2]

[2 3 4 2 3]]

If you change the above to the following, an error will be reported! ! !

print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))
print np.hstack(([[1,2,3],[2,3,4]],[[1,2]]))

(2)np.vstack()

Function prototype: numpy.hstack(tup)

tup : sequence of ndarrays

The arrays must have the same shape along all but the first axis.1-D arrays must have the same length.

means that except for the first dimension, we can have different The shape must be the same in other dimensions. One-dimensional arrays must be of the same size.

Example 1:

print np.vstack(([1,2,3],[3,4,3]))
print np.vstack(([1,2,3],[2,3]))

But what you should pay attention to is that the second line is wrong!

Example 2:

print np.vstack(([[1,2,3],[3,4,3]],[[1,3,4],[2,4,5]]))
print np.vstack(([[1,2,3],[3,4,3]],[[3,4],[4,5]]))

The same shows that if the second dimension of our array is different, an error will occur.

print np.vstack(([[1,2,3],[3,4,3]],[[2,4,5]]))
print np.vstack(([[1,2,3],[3,4,3]],[[4,5]]))

Example 3:

We pass in the list:

import numpy as np
arr1=np.array([[1,2],[2,4],[11,33],[2,44],[55,77],[11,22],[55,67],[67,89]])
arr11=np.array([[11,2,3],[22,3,4],[4,5,6]])
arr1_folds=np.array_split(arr1,3)
print arr1_folds
print np.vstack(arr1_folds)

The result:

[array([[ 1, 2] ,
[ 2, 4],
[11, 33]]), array([[ 2, 44],
[55, 77],
[11, 22]]), array([[55, 67],
[67, 89]])]
[[ 1 2]
[ 2 4]
[11 33]
[ 2 44]
[55 77]
[11 22]
[55 67]
[67 89]]

The above is the detailed content of How to use np.vstack() and np.hstack() in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete