我正在查看其他人的代码(使用numpy 1.16.3),发现了一条令人困惑的行,其中以-2
作为expand_dims
中的扩展轴:
result = [np.expand_dims(v != 0, -2) for v in vecs]
查看文档,我发现此注释:
Note
Previous to NumPy 1.13.0, neither axis < -a.ndim - 1 nor axis > a.ndim raised errors or put the new axis where documented. Those axis values are now deprecated and will raise an AxisError in the future.
这使我认为-2
只会使expand_dims
不执行任何操作。是这样吗如果是这样,为什么还要在代码中使用它呢?这只是包装逻辑评估v != 0
的一种方法吗?
如果将axis
负值传递给numpy.expand_dims
,则新轴将以新数组的形状放置在该位置。例如,