Skip to content

Latest commit

 

History

History
92 lines (58 loc) · 1.99 KB

np126_0359.md

File metadata and controls

92 lines (58 loc) · 1.99 KB

numpy.stack

原文:numpy.org/doc/1.26/reference/generated/numpy.stack.html

numpy.stack(arrays, axis=0, out=None, *, dtype=None, casting='same_kind')

沿着新轴连接数组序列。

axis参数指定结果维度中新轴的索引。例如,如果axis=0,它将是第一个维度,如果axis=-1,它将是最后一个维度。

新版本 1.10.0 中新增。

参数:

arrays数组序列

每个数组必须具有相同的形状。

axisint,可选

结果数组中的轴,沿着其中堆叠输入数组。

outndarray,可选

如果提供,目标位置放置结果。形状必须正确,与如果未指定 out 参数,则 stack 将返回的形状匹配。

dtypestr 或 dtype

如果提供,目标数组将具有此 dtype。不能与out同时提供。

新版本 1.24 中新增。

casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’},可选

控制可能发生的数据转换类型。默认为‘same_kind’。

新版本 1.24 中新增。

返回:

stackedndarray

堆叠数组比输入数组多一个维度。

另请参阅

concatenate

沿现有轴连接数组序列。

block

从嵌套块列表中组装 nd-array。

split

将数组拆分为多个相等大小的子数组列表。

示例

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
>>> np.stack(arrays, axis=0).shape
(10, 3, 4) 
>>> np.stack(arrays, axis=1).shape
(3, 10, 4) 
>>> np.stack(arrays, axis=2).shape
(3, 4, 10) 
>>> a = np.array([1, 2, 3])
>>> b = np.array([4, 5, 6])
>>> np.stack((a, b))
array([[1, 2, 3],
 [4, 5, 6]]) 
>>> np.stack((a, b), axis=-1)
array([[1, 4],
 [2, 5],
 [3, 6]])