python
type_error
ai_generated
true
numpy.exceptions.AxisError: 轴 2 超出维度为 2 的数组范围
numpy.exceptions.AxisError: axis 2 is out of bounds for array of dimension 2
ID: python/numpy-axis-out-of-bounds
80%修复率
88%置信度
0证据数
2024-03-04首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
向 np.sum、np.mean、np.concatenate 等函数传入大于 array.ndim - 1 的 axis 参数。常因假设输入为 3 维。
English
Passing an axis argument larger than array.ndim - 1 to functions like np.sum, np.mean, np.concatenate. Often from assuming 3-D input.
解决方案
-
95% 成功率
np.sum(arr, axis=-1) # last axis regardless of ndim
-
85% 成功率
assert arr.ndim >= 3 or use np.sum(arr, axis=tuple(range(arr.ndim)))
无效尝试
常见但无效的做法:
-
95% 失败
Same AxisError
-
75% 失败
Changes semantics, not what you want