numpy.stack() 函数

此函数沿新轴连接数组序列。 自 NumPy 版本 1.10.0 以来添加了此函数。 需要提供以下参数。

注意 − 此功能在 1.10.0 及以后的版本中可用。

numpy.stack(arrays, axis)

这里,

序号 参数 & 描述
1

arrays

相同形状的数组序列

2

axis

结果数组中输入数组堆叠的轴


示例

import numpy as np 
a = np.array([[1,2],[3,4]]) 

print 'First Array:' 
print a 
print '\n'
b = np.array([[5,6],[7,8]]) 

print 'Second Array:' 
print b 
print '\n'  

print 'Stack the two arrays along axis 0:' 
print np.stack((a,b),0) 
print '\n'  

print 'Stack the two arrays along axis 1:' 
print np.stack((a,b),1)

它应该产生以下输出 −

First array:
[[1 2]
 [3 4]]

Second array:
[[5 6]
 [7 8]]

Stack the two arrays along axis 0:
[[[1 2]
 [3 4]]
 [[5 6]
 [7 8]]]

Stack the two arrays along axis 1:
[[[1 2]
 [5 6]]
 [[3 4]
 [7 8]]]

❮ NumPy 数组操作