Python 在 numpy.sum() 中有一个名为“keepdims”的参数。它有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39441517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:16:51  来源:igfitidea点击:

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

pythonnumpysum

提问by Ney J Torres

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

numpy.sum()那里有一个参数叫做keepdims. 它有什么作用?

As you can see here in the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html

正如您在文档中所见:http: //docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html

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.
...

回答by

@Ney @hpaulj is correct, you need to experiment, but I suspect you don't realize that summation for some arrays can occur along axes. Observe the following which reading the documentation

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

>>> a
array([[0, 0, 0],
       [0, 1, 0],
       [0, 2, 0],
       [1, 0, 0],
       [1, 1, 0]])
>>> np.sum(a, keepdims=True)
array([[6]])
>>> np.sum(a, keepdims=False)
6
>>> np.sum(a, axis=1, keepdims=True)
array([[0],
       [1],
       [2],
       [1],
       [2]])
>>> 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 = Truereturned a 2Darray with the number 6, whereas, the second incarnation returned a scalar. Similarly, when summing along axis 1(across rows), a 2Darray 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.

您会注意到,如果您不指定轴(第一个两个示例),则数值结果相同,但keepdims = True返回一个2D数字为 6的数组,而第二个化身返回一个标量。类似地,当沿axis 1(跨行)求和时,当 时2D再次返回数组keepdims = True。最后一个例子,沿着axis 0(下列),显示了一个类似的特征......尺寸在 时保持不变keepdims = True
研究轴及其属性对于充分理解 NumPy 在处理多维数据时的强大功能至关重要。

回答by Benjamin Crouzier

An example showing keepdimsin action when working with higher dimensional arrays. Let's see how the shape of the array changes as we do different reductions:

keepdims使用高维数组时的实际操作示例。让我们看看数组的形状如何随着我们进行不同的归约而改变:

import numpy as np
a = np.random.rand(2,3,4)
a.shape
# => (2, 3, 4)
# Note: axis=0 refers to the first dimension of size 2
#       axis=1 refers to the second dimension of size 3
#       axis=2 refers to the third dimension of size 4

a.sum(axis=0).shape
# => (3, 4)
# Simple sum over the first dimension, we "lose" that dimension 
# because we did an aggregation (sum) over it

a.sum(axis=0, keepdims=True).shape
# => (1, 3, 4)
# Same sum over the first dimension, but instead of "loosing" that 
# dimension, it becomes 1.

a.sum(axis=(0,2)).shape
# => (3,)
# Here we "lose" two dimensions

a.sum(axis=(0,2), keepdims=True).shape
# => (1, 3, 1)
# Here the two dimensions become 1 respectively