为什么在numpy的expand_dims中使用-2索引

问题描述 投票:0回答:1

我正在查看其他人的代码(使用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的一种方法吗?

python numpy reshape
1个回答
0
投票

如果将axis负值传递给numpy.expand_dims,则新轴将以新数组的形状放置在该位置。例如,

© www.soinside.com 2019 - 2024. All rights reserved.