NumPy(Numerical Python的缩写)是一个开源的Python科学计算库。使用NumPy,就可以很自然地使用数组和矩阵。NumPy包含很多实用的数学函数,涵盖线性代数运算、傅里叶变换和随机数生成等功能。本文主要介绍一下NumPy中atleast_3d方法的使用。

numpy.atleast_3d

numpy.atleast_3d(*arys)      [source]

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

参数 :

arys1, arys2, … :array_like

一个或多个类似数组的序列。 非数组输入将转换为数组。

 保留已经具有三个或三个以上维度的数组。

返回值 :

res1, res2, … :ndarray

一个数组或数组列表,每个数组均带有a.ndim>= 3。 

尽可能避免复制,并返回三个或更多尺寸的视图。 

例如,形状为(N,)的一维数组变成形状为(1,N,1)的视图,

而形状为(M,N)变成形状为(M,N,1)的视图。

例子

>>> 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) \n...
[[[1]
  [2]]] (1, 2, 1)
[[[1]
  [2]]] (1, 2, 1)
[[[1 2]]] (1, 1, 2)

推荐文档

相关文档

大家感兴趣的内容

随机列表