简单的说,numpy.newaxis https://numpy.org/devdocs/reference/constants.html#numpy.newaxis习惯于增加维度现有数组的多一个维度,使用时once. Thus,
-
1D数组将变成2D array
-
2D数组将变成3D array
-
3D数组将变成4D array
-
4D数组将变成5D array
等等..
这是一个视觉插图,描绘了晋升一维数组到二维数组。
场景一1: np.newaxis https://numpy.org/devdocs/reference/constants.html#numpy.newaxis当你想要的时候可能会派上用场明确地将一维数组转换为行向量 or a 列向量,如上图所示。
Example:
# 1D array
In [7]: arr = np.arange(4)
In [8]: arr.shape
Out[8]: (4,)
# make it as row vector by inserting an axis along first dimension
In [9]: row_vec = arr[np.newaxis, :] # arr[None, :]
In [10]: row_vec.shape
Out[10]: (1, 4)
# make it as column vector by inserting an axis along second dimension
In [11]: col_vec = arr[:, np.newaxis] # arr[:, None]
In [12]: col_vec.shape
Out[12]: (4, 1)
场景2: 当我们想要利用numpy 广播 https://numpy.org/doc/stable/user/basics.broadcasting.html作为某些操作的一部分,例如在执行时addition一些数组。
Example:
假设您要添加以下两个数组:
x1 = np.array([1, 2, 3, 4, 5])
x2 = np.array([5, 4, 3])
如果你尝试像这样添加这些,NumPy 将引发以下内容ValueError
:
ValueError: operands could not be broadcast together with shapes (5,) (3,)
在这种情况下,您可以使用np.newaxis https://numpy.org/devdocs/reference/constants.html#numpy.newaxis增加其中一个数组的维度,以便 NumPy 可以播送 https://numpy.org/doc/stable/user/basics.broadcasting.html.
In [2]: x1_new = x1[:, np.newaxis] # x1[:, None]
# now, the shape of x1_new is (5, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5]])
现在,添加:
In [3]: x1_new + x2
Out[3]:
array([[ 6, 5, 4],
[ 7, 6, 5],
[ 8, 7, 6],
[ 9, 8, 7],
[10, 9, 8]])
或者,您也可以将新轴添加到数组中x2
:
In [6]: x2_new = x2[:, np.newaxis] # x2[:, None]
In [7]: x2_new # shape is (3, 1)
Out[7]:
array([[5],
[4],
[3]])
现在,添加:
In [8]: x1 + x2_new
Out[8]:
array([[ 6, 7, 8, 9, 10],
[ 5, 6, 7, 8, 9],
[ 4, 5, 6, 7, 8]])
Note:观察到我们在两种情况下得到相同的结果(但一种是另一种的转置)。
场景3:这与场景 1 类似。但是,你可以使用np.newaxis https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis不止一次promote数组到更高的维度。对于高阶数组有时需要这样的操作(即张量).
Example:
In [124]: arr = np.arange(5*5).reshape(5,5)
In [125]: arr.shape
Out[125]: (5, 5)
# promoting 2D array to a 5D array
In [126]: arr_5D = arr[np.newaxis, ..., np.newaxis, np.newaxis] # arr[None, ..., None, None]
In [127]: arr_5D.shape
Out[127]: (1, 5, 5, 1, 1)
作为替代方案,您可以使用numpy.expand_dims https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html具有直观的axis
kwarg.
# adding new axes at 1st, 4th, and last dimension of the resulting array
In [131]: newaxes = (0, 3, -1)
In [132]: arr_5D = np.expand_dims(arr, axis=newaxes)
In [133]: arr_5D.shape
Out[133]: (1, 5, 5, 1, 1)
更多背景np.newaxis https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis vs np.重塑 https://numpy.org/doc/stable/reference/generated/numpy.reshape.html
newaxis https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis也称为伪索引,允许将轴临时添加到多数组中。
np.newaxis https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis使用切片运算符重新创建数组,同时numpy.reshape https://numpy.org/doc/stable/reference/generated/numpy.reshape.html将数组重塑为所需的布局(假设尺寸匹配;这是must for a reshape https://numpy.org/doc/stable/reference/generated/numpy.reshape.html即将发生)。
Example
In [13]: A = np.ones((3,4,5,6))
In [14]: B = np.ones((4,6))
In [15]: (A + B[:, np.newaxis, :]).shape # B[:, None, :]
Out[15]: (3, 4, 5, 6)
在上面的示例中,我们在第一个轴和第二个轴之间插入了一个临时轴B
(使用广播)。此处使用填充缺失的轴np.newaxis https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis使广播 https://numpy.org/doc/stable/user/basics.broadcasting.html操作工作。
一般提示: 也可以用None
代替np.newaxis https://numpy.org/devdocs/reference/constants.html#numpy.newaxis;这些实际上是相同的对象 https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html.
In [13]: np.newaxis is None
Out[13]: True
附:另请参阅这个很棒的答案:newaxis 与 reshape 添加尺寸 https://stackoverflow.com/a/28385957