In `numpy.sum()` there is parameter called `keepdims`. What does it do?

``````numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)[source]
Sum of array elements over a given axis.

Parameters:
...
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as
dimensions with size one. With this option, the result will broadcast
correctly against the input array.
...
``````

## 最佳答案

@奈 @hpaulj是正确的，您需要进行实验，但是我怀疑您没有意识到某些数组的求和可能会沿轴发生。请遵守以下阅读文档的内容

``````>>> a
array([[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
[1, 0, 0],
[1, 1, 0]])
>>> np.sum(a, keepdims=True)
array([])
>>> np.sum(a, keepdims=False)
6
>>> np.sum(a, axis=1, keepdims=True)
array([,
,
,
,
])
>>> np.sum(a, axis=1, keepdims=False)
array([0, 1, 2, 1, 2])
>>> np.sum(a, axis=0, keepdims=True)
array([[2, 4, 0]])
>>> np.sum(a, axis=0, keepdims=False)
array([2, 4, 0])
``````

You will notice that if you don't specify an axis (1st two examples), the numerical result is the same, but the `keepdims = True` returned a `2D` array with the number 6, whereas, the second incarnation returned a scalar. Similarly, when summing along `axis 1` (across rows), a `2D` array is returned again when `keepdims = True`. The last example, along `axis 0` (down columns), shows a similar characteristic... dimensions are kept when `keepdims = True`.
Studying axes and their properties is critical to a full understanding of the power of NumPy when dealing with multidimensional data.

公众号 关注公众号订阅更多技术干货！