return ufunc.reduce(obj, axis, dtype, out, **passkwargs) TypeError: __int__ returned non-int (type NoneType)
时间: 2024-03-10 11:50:49 浏览: 166
这个错误通常是由于在使用NumPy中的某些函数时,数组中的元素类型不正确导致的。在这种情况下,NumPy期望ndarray中的所有元素都是整数类型,但实际上有一个或多个元素不是整数类型(可能是NoneType)。
要解决这个问题,你可以使用NumPy的astype()函数将数组中的所有元素转换为整数类型。例如,如果你有一个名为arr的数组,你可以使用以下代码将其转换为整数类型:
```
arr = arr.astype(int)
```
这将把arr中的所有元素转换为整数类型。如果数组中有NoneType元素,它们将被转换为0。
另外,你还可以检查你的代码,确保在使用NumPy函数时传递给它们的参数具有正确的类型,这也可以避免出现这个错误。
相关问题
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-68998da09081> in <module> 25 26 # 计算评估指标 ---> 27 true_positive = np.sum(target_labels & x) <__array_function__ internals> in sum(*args, **kwargs) D:\anaconda\envs\pytorch3.6\lib\site-packages\numpy\core\fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial, where) 2240 2241 return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims, -> 2242 initial=initial, where=where) 2243 2244 D:\anaconda\envs\pytorch3.6\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 83 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs) 84 else: ---> 85 return reduction(axis=axis, out=out, **passkwargs) 86 87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) TypeError: sum() received an invalid combination of arguments - got (axis=NoneType, out=NoneType, ), but expected one of: * (*, torch.dtype dtype) didn't match because some of the keywords were incorrect: axis, out * (tuple of names dim, bool keepdim, *, torch.dtype dtype) * (tuple of ints dim, bool keepdim, *, torch.dtype dtype)
这个错误是因为在计算 `true_positive` 时使用了 `np.sum()` 函数,但是函数的参数传递错误。`np.sum()` 函数不接受 `axis` 和 `out` 参数为 `NoneType`。要解决这个问题,你可以将 `target_labels` 和 `x` 转换为 numpy 数组,然后使用逻辑与运算符 `&` 计算它们的交集。例如,你可以尝试以下代码:
```python
import numpy as np
# 将 target_labels 和 x 转换为 numpy 数组
target_labels = np.array(target_labels)
x = np.array(x)
# 计算 true_positive
true_positive = np.sum(target_labels & x)
```
阅读全文
相关推荐
















