# numpy.exceptions.AxisError: 轴 2 超出维度为 2 的数组范围

- **ID:** `python/numpy-axis-out-of-bounds`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

向 np.sum、np.mean、np.concatenate 等函数传入大于 array.ndim - 1 的 axis 参数。常因假设输入为 3 维。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   np.sum(arr, axis=-1)  # last axis regardless of ndim
   ```
2. **** (85% 成功率)
   ```
   assert arr.ndim >= 3 or use np.sum(arr, axis=tuple(range(arr.ndim)))
   ```

## 无效尝试

- **** — Same AxisError (95% 失败率)
- **** — Changes semantics, not what you want (75% 失败率)
