ma.
expand_dims
展开数组的形状。
插入将出现在 axis 在展开的数组形状中的位置。
输入数组。
在展开轴中放置新轴的位置。
1.13.0 版后已移除: 通过一个轴 axis > a.ndim 将被视为 axis == a.ndim ,然后经过 axis < -a.ndim - 1 将被视为 axis == 0 . 不赞成这种行为。
axis > a.ndim
axis == a.ndim
axis < -a.ndim - 1
axis == 0
在 1.18.0 版更改: 现在支持一组轴。现在禁止使用上述超出范围的轴,并提高 AxisError .
观 a 随着维数的增加。
参见
squeeze
反操作,移除单体尺寸
reshape
插入、删除和组合尺寸,并调整现有尺寸的大小
doc.indexing
atleast_1d
atleast_2d
atleast_3d
实例
>>> x = np.array([1, 2]) >>> x.shape (2,)
以下等价于 x[np.newaxis, :] 或 x[np.newaxis] :
x[np.newaxis, :]
x[np.newaxis]
>>> y = np.expand_dims(x, axis=0) >>> y array([[1, 2]]) >>> y.shape (1, 2)
以下等价于 x[:, np.newaxis] :
x[:, np.newaxis]
>>> y = np.expand_dims(x, axis=1) >>> y array([[1], [2]]) >>> y.shape (2, 1)
axis 也可以是元组:
axis
>>> y = np.expand_dims(x, axis=(0, 1)) >>> y array([[[1, 2]]])
>>> y = np.expand_dims(x, axis=(2, 0)) >>> y array([[[1], [2]]])
注意一些例子可能会用到 None 而不是 np.newaxis . 这些是相同的对象:
None
np.newaxis
>>> np.newaxis is None True