【问题标题】:What is the role of keepdims in Numpy (Python)?keepdims 在 Numpy (Python) 中的作用是什么?
【发布时间】:2017-04-17 01:35:03
【问题描述】:

我在使用np.sum的时候,遇到了一个叫keepdims的参数。查了the docs,还是看不懂keepdims的意思。

keepdims: 布尔型,可选

如果设置为 True,则缩小的轴将作为尺寸为 1 的尺寸留在结果中。使用此选项,结果将根据原始 arr 正确广播。

如果有人能通过一个简单的例子理解这一点,我将不胜感激。

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    keepdims = true;在这种情况下,您的数组(矩阵)的尺寸将被保存。这意味着您得到的结果是针对您尝试实现方法的数组正确“广播”的。

    当你忽略它时,它只是一个没有更多维度的普通数组。

    import numpy as np
    
    x = np.random.rand(4,3)
    
    #Output for below statement: (3,)
    print((np.sum(x, axis=0)).shape)
    
    #Output for below statement: (1, 3)
    print((np.sum(x, axis=0, keepdims=True)).shape)
    

    【讨论】:

      【解决方案2】:

      keepdims = True,用于匹配矩阵的维度。如果我们留下这个 False ,那么它将显示尺寸错误的错误。 计算softmax熵的时候可以看到

      【讨论】:

        【解决方案3】:

        考虑一个小的二维数组:

        In [180]: A=np.arange(12).reshape(3,4)
        In [181]: A
        Out[181]: 
        array([[ 0,  1,  2,  3],
               [ 4,  5,  6,  7],
               [ 8,  9, 10, 11]])
        

        跨行求和;结果是一个 (3,) 数组

        In [182]: A.sum(axis=1)
        Out[182]: array([ 6, 22, 38])
        

        但是将Asum 相加(或除)需要重新整形

        In [183]: A-A.sum(axis=1)
        ...
        ValueError: operands could not be broadcast together with shapes (3,4) (3,) 
        In [184]: A-A.sum(axis=1)[:,None]   # turn sum into (3,1)
        Out[184]: 
        array([[ -6,  -5,  -4,  -3],
               [-18, -17, -16, -15],
               [-30, -29, -28, -27]])
        

        如果我使用keepdims,“结果将正确广播”A

        In [185]: A.sum(axis=1, keepdims=True)   # (3,1) array
        Out[185]: 
        array([[ 6],
               [22],
               [38]])
        In [186]: A-A.sum(axis=1, keepdims=True)
        Out[186]: 
        array([[ -6,  -5,  -4,  -3],
               [-18, -17, -16, -15],
               [-30, -29, -28, -27]])
        

        如果我以另一种方式求和,我不需要keepdims。广播这个总和是自动的:A.sum(axis=0)[None,:]。但是使用keepdims 并没有什么坏处。

        In [190]: A.sum(axis=0)
        Out[190]: array([12, 15, 18, 21])    # (4,)
        In [191]: A-A.sum(axis=0)
        Out[191]: 
        array([[-12, -14, -16, -18],
               [ -8, -10, -12, -14],
               [ -4,  -6,  -8, -10]])
        

        如果您愿意,这些操作可能对np.mean 更有意义,将数组标准化为列或行。无论如何,它可以简化原始数组和总和/平均值之间的进一步数学运算。

        【讨论】:

        • 这是一个不错的答案
        • 在同一个例子中,你能详细说明这两个区别吗? >>> A.sum(axis=0, keepdims=False) 数组([12, 15, 18, 21]) >>> A.sum(axis=0, keepdims=True) 数组([[12, 15, 18, 21]])
        • @MonaJalal。 A 是二维的。第一个和是 1d, (4,)。第二个和是 2d (1,4)。它将轴 0 相加,但将其保留为大小 (1,) 维度,而不是将其挤出。
        【解决方案4】:

        如果对矩阵求和,则可以使用 "keepdims=True" 保留维度 例如:

        import numpy as np
        x  = np.array([[1,2,3],[4,5,6]])
        x.shape
        # (2, 3)
        
        np.sum(x, keepdims=True).shape
        # (1, 1)
        np.sum(x, keepdims=True)
        # array([[21]]) <---the reault is still a 1x1 array
        
        np.sum(x, keepdims=False).shape
        # ()
        np.sum(x, keepdims=False)
        # 21 <--- the result is an integer with no dimesion
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-07
          • 1970-01-01
          • 2017-01-19
          • 1970-01-01
          • 1970-01-01
          • 2016-06-30
          • 2013-10-23
          相关资源
          最近更新 更多