sum() got an unexpected keyword argument 'keepdims'怎么解决
时间: 2023-10-03 15:03:38 浏览: 132
这个错误是因为 `keepdims` 参数在 Python 2 中是不支持的,而在 Python 3 中支持。如果你的代码运行在 Python 2 中,那么可以尝试将 `keepdims=True` 改为 `keepdims`,即将其作为布尔值传递。如果你的代码运行在 Python 3 中,则可以检查 `sum()` 函数的调用是否正确,并且确保 `keepdims` 参数的值为布尔值。
相关问题
sum() got an unexpected keyword argument 'keepdims'
The error message you are seeing indicates that the `sum()` function is not expecting the `keepdims` keyword argument that you have provided.
The `keepdims` argument was added to the `sum()` function in Python 3.2, so if you are using an earlier version of Python, you may see this error. To fix this issue, you can either upgrade your Python version to 3.2 or later, or you can remove the `keepdims` argument from your `sum()` function call.
If you are using Python 3.2 or later and still seeing this error, double-check that you are not misspelling the argument name or using it in an unsupported way.
TypeError: sum() got an unexpected keyword argument 'keepdims'
这个错误通常是因为您正在使用的Python版本不支持`keepdims`参数。`keepdims`参数是在Python 3.2中引入的,如果您的Python版本低于3.2,则不支持该参数。
解决此问题的方法是升级您的Python版本或删除`keepdims`参数。如果您想保留`keepdims`参数,则可以使用以下代码:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
sum_a = np.sum(a, axis=0, keepdims=True)
print(sum_a)
```
这将使用NumPy库中的`sum`函数来计算数组`a`的列和,并将`keepdims`参数设置为`True`。这将返回一个形状为`(1,2)`的数组,其中包含列和。
阅读全文