关于stack和hstack的区别

import numpy as np
a=np.array([[1,2,3,21],
   [4,5,6,22]])
b=np.array([[7,8,9,22],
   [10,11,12,23]])
d=np.stack((a,b),axis=1)
print(d.shape)
print(d)
g = d.reshape((1,2,e.shape[-1]))
print(g.shape)
print(g)
print('----------------------')
e = np.hstack((a,b))
print(e.shape)
print(e)
f = e.reshape((1,2,e.shape[-1]))
print(f.shape)
print(f)

输出:

(2, 2, 4)
[[[ 1  2  3 21]
  [ 7  8  9 22]]

 [[ 4  5  6 22]
  [10 11 12 23]]]
(1, 2, 8)
[[[ 1  2  3 21  7  8  9 22]
  [ 4  5  6 22 10 11 12 23]]]
----------------------
(2, 8)
[[ 1  2  3 21  7  8  9 22]
 [ 4  5  6 22 10 11 12 23]]
(1, 2, 8)
[[[ 1  2  3 21  7  8  9 22]
  [ 4  5  6 22 10 11 12 23]]]

之前以为,stack和hstack对两个array处理后,再reshape会不一样,实验发现是一致的。

你可能感兴趣的:(Python学习)