numpy.atleast_3d#

numpy.atleast_3d(*arys)[源码]#

将输入视为至少具有三个维度的数组。

参数:
arys1, arys2, …类数组

一个或多个类数组序列。非数组输入将转换为数组。已具有三个或更多维度的数组将保留。

返回:
res1, res2, …ndarray

一个数组或一个数组元组,每个都具有 a.ndim >= 3。尽可能避免复制,并返回具有三个或更多维度的视图。例如,形状为 (N,) 的一维数组将变为形状为 (1, N, 1) 的视图,而形状为 (M, N) 的二维数组将变为形状为 (M, N, 1) 的视图。

另请参阅

atleast_1d, atleast_2d

示例

>>> import numpy as np
>>> np.atleast_3d(3.0)
array([[[3.]]])
>>> x = np.arange(3.0)
>>> np.atleast_3d(x).shape
(1, 3, 1)
>>> x = np.arange(12.0).reshape(4,3)
>>> np.atleast_3d(x).shape
(4, 3, 1)
>>> np.atleast_3d(x).base is x.base  # x is a reshape, so not base itself
True
>>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):
...     print(arr, arr.shape) 
...
[[[1]
  [2]]] (1, 2, 1)
[[[1]
  [2]]] (1, 2, 1)
[[[1 2]]] (1, 1, 2)