numpy.exceptions.AxisError#

exception exceptions.AxisError(axis, ndim=None, msg_prefix=None)[源代码]#

提供的轴无效。

每当指定一个大于数组维数的 axis 参数时,都会引发此异常。为了与针对旧版 numpy 代码的兼容性(这些代码在此情况下会引发 ValueErrorIndexError 的混合),此异常会同时继承这两个类,以确保 except ValueErrorexcept IndexError 语句继续捕获 AxisError

参数:
axisint 或 str

越界轴或自定义异常消息。如果提供了轴,则还应指定 ndim

ndimint, optional

数组的维度数。

msg_prefixstr, optional

异常消息的前缀。

示例

>>> import numpy as np
>>> array_1d = np.arange(10)
>>> np.cumsum(array_1d, axis=1)
Traceback (most recent call last):
  ...
numpy.exceptions.AxisError: axis 1 is out of bounds for array of dimension 1

负轴被保留

>>> np.cumsum(array_1d, axis=-2)
Traceback (most recent call last):
  ...
numpy.exceptions.AxisError: axis -2 is out of bounds for array of dimension 1

类构造函数通常将轴和数组的维度作为参数

>>> print(np.exceptions.AxisError(2, 1, msg_prefix='error'))
error: axis 2 is out of bounds for array of dimension 1

或者,可以传递自定义异常消息

>>> print(np.exceptions.AxisError('Custom error message'))
Custom error message
属性:
axisint, optional

越界轴或 None(如果提供了自定义异常消息)。这应该是用户传递的轴,在任何用于解析负索引的归一化之前。

新功能,版本 1.22。

ndimint, optional

数组维度数或 None(如果提供了自定义异常消息)。

新功能,版本 1.22。