【问题标题】:What's the simplest way to extend a numpy array in 2 dimensions?在二维中扩展 numpy 数组的最简单方法是什么?
【发布时间】:2010-10-27 00:32:31
【问题描述】:

我有一个如下所示的二维数组:

XX
xx

添加额外行和列的最有效方法是什么:

xxy
xxy
yyy

对于加分,我还希望能够剔除单行和单列,例如,在下面的矩阵中,我希望能够剔除所有的 a,只留下 x - 特别是我正在尝试同时删除第 n 行和第 n 列 - 我希望能够尽快执行此操作:

xxaxx
xxaxx
aaaaa
xxaxx
xxaxx

【问题讨论】:

    标签: python arrays math numpy


    【解决方案1】:

    我能想到的最短的代码行是第一个问题。

    >>> import numpy as np
    >>> p = np.array([[1,2],[3,4]])
    
    >>> p = np.append(p, [[5,6]], 0)
    >>> p = np.append(p, [[7],[8],[9]],1)
    
    >>> p
    array([[1, 2, 7],
       [3, 4, 8],
       [5, 6, 9]])
    

    第二个问题

        p = np.array(range(20))
    >>> p.shape = (4,5)
    >>> p
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19]])
    >>> n = 2
    >>> p = np.append(p[:n],p[n+1:],0)
    >>> p = np.append(p[...,:n],p[...,n+1:],1)
    >>> p
    array([[ 0,  1,  3,  4],
           [ 5,  6,  8,  9],
           [15, 16, 18, 19]])
    

    【讨论】:

    • np.appendnumpy 中被滥用最多的函数之一。大多数情况下,人们认为它就像列表附加一样。如果一定要加入数组,学会直接使用np.concatenate(即使对尺寸和形状很挑剔)。
    • appendaxis 只是:np.concatenate((arr, values), axis=axis)。没有axis,它首先分解输入,但使用axis,它直接调用concatenate
    【解决方案2】:

    第一个问题的有用替代答案,使用来自 tomeedee 答案的示例,将使用numpy的 vstack column_stack 方法:

    给定一个矩阵 p,

    >>> import numpy as np
    >>> p = np.array([ [1,2] , [3,4] ])
    

    增广矩阵可以通过以下方式生成:

    >>> p = np.vstack( [ p , [5 , 6] ] )
    >>> p = np.column_stack( [ p , [ 7 , 8 , 9 ] ] )
    >>> p
    array([[1, 2, 7],
           [3, 4, 8],
           [5, 6, 9]])
    

    这些方法在实践中可能比 np.append() 方便,因为它们允许将一维数组附加到矩阵而无需任何修改,这与以下情况相反:

    >>> p = np.array([ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
    >>> p = np.append( p , [ 7 , 8 , 9 ] , 1 )
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 3234, in append
        return concatenate((arr, values), axis=axis)
    ValueError: arrays must have same number of dimensions
    

    回答第二个问题,删除行和列的好方法是使用逻辑数组索引,如下所示:

    给定一个矩阵 p,

    >>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
    

    假设我们要删除第 1 行和第 2 列:

    >>> r , c = 1 , 2
    >>> p = p [ np.arange( p.shape[0] ) != r , : ] 
    >>> p = p [ : , np.arange( p.shape[1] ) != c ]
    >>> p
    array([[ 0,  1,  3,  4],
           [10, 11, 13, 14],
           [15, 16, 18, 19]])
    

    注意 - 对于改革后的 Matlab 用户 - 如果您想在单行中执行这些操作,您需要索引两次:

    >>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )    
    >>> p = p [ np.arange( p.shape[0] ) != r , : ] [ : , np.arange( p.shape[1] ) != c ]
    

    这种技术也可以扩展为删除行和列的sets,所以如果我们想删除第 0 行和第 2 行以及第 1、2 和 3 列,我们可以使用 numpy 的 setdiff1d 生成所需逻辑索引的函数:

    >>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
    >>> r = [ 0 , 2 ]
    >>> c = [ 1 , 2 , 3 ]
    >>> p = p [ np.setdiff1d( np.arange( p.shape[0] ), r ) , : ] 
    >>> p = p [ : , np.setdiff1d( np.arange( p.shape[1] ) , c ) ]
    >>> p
    array([[ 5,  9],
           [15, 19]])
    

    【讨论】:

      【解决方案3】:

      第一个问题的答案:

      使用 numpy.append。

      http://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html#numpy.append

      第二个问题的答案:

      使用 numpy.delete

      http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

      【讨论】:

        【解决方案4】:

        我发现通过分配更大的矩阵来“扩展”要容易得多。例如

        import numpy as np
        p = np.array([[1,2], [3,4]])
        g = np.array(range(20))
        g.shape = (4,5)
        g[0:2, 0:2] = p
        

        这里是数组:

        p

           array([[1, 2],
               [3, 4]])
        

        g:

        array([[ 0,  1,  2,  3,  4],
               [ 5,  6,  7,  8,  9],
               [10, 11, 12, 13, 14],
               [15, 16, 17, 18, 19]])
        

        以及赋值后得到的g

           array([[ 1,  2,  2,  3,  4],
               [ 3,  4,  7,  8,  9],
               [10, 11, 12, 13, 14],
               [15, 16, 17, 18, 19]])
        

        【讨论】:

          【解决方案5】:

          也许你需要这个。

          >>> x = np.array([11,22])
          >>> y = np.array([18,7,6])
          >>> z = np.array([1,3,5])
          >>> np.concatenate((x,y,z))
          array([11, 22, 18,  7,  6,  1,  3,  5])
          

          【讨论】:

            【解决方案6】:

            第一个问题的另一个优雅解决方案可能是insert 命令:

            p = np.array([[1,2],[3,4]])
            p = np.insert(p, 2, values=0, axis=1) # insert values before column 2
            

            导致:

            array([[1, 2, 0],
                   [3, 4, 0]])
            

            insert 可能比append 慢,但允许您轻松地用一个值填充整个行/列。

            至于第二个问题delete之前已经推荐过:

            p = np.delete(p, 2, axis=1)
            

            又恢复了原来的数组:

            array([[1, 2],
                   [3, 4]])
            

            【讨论】:

              【解决方案7】:

              你可以使用:

              >>> np.concatenate([array1, array2, ...]) 
              

              例如

              >>> import numpy as np
              >>> a = [[1, 2, 3],[10, 20, 30]]
              >>> b = [[100,200,300]]
              >>> a = np.array(a) # not necessary, but numpy objects prefered to built-in
              >>> b = np.array(b) # "^
              >>> a
              array([[ 1,  2,  3],
                     [10, 20, 30]])
              >>> b
              array([[100, 200, 300]])
              >>> c = np.concatenate([a,b])
              >>> c
              array([[  1,   2,   3],
                     [ 10,  20,  30],
                     [100, 200, 300]])
              >>> print c
              [[  1   2   3]
               [ 10  20  30]
               [100 200 300]]
              

              ~-+-~-+-~-+-~

              有时,如果 numpy 数组对象的 shape 属性值不完整,您会遇到麻烦。通过将元组分配给形状属性来解决此问题:(array_length, element_length)。

              注意:这里,'array_length' 和 'element_length' 是整数参数,您可以将它们替换为 for。 “元组”只是括号中的一对数字。

              例如

              >>> import numpy as np
              >>> a = np.array([[1,2,3],[10,20,30]])
              >>> b = np.array([100,200,300]) # initialize b with incorrect dimensions
              >>> a.shape
              (2, 3)
              >>> b.shape
              (3,)
              >>> c = np.concatenate([a,b])
              
              Traceback (most recent call last):
                File "<pyshell#191>", line 1, in <module>
                  c = np.concatenate([a,b])
              ValueError: all the input arrays must have same number of dimensions
              >>> b.shape = (1,3)
              >>> c = np.concatenate([a,b])
              >>> c
              array([[  1,   2,   3],
                     [ 10,  20,  30],
                     [100, 200, 300]])
              

              【讨论】:

                【解决方案8】:

                如果你想在新的位置有一个常数值,你可以使用np.pad

                https://numpy.org/doc/stable/reference/generated/numpy.pad.html

                >>> p = np.array([[1,2],[3,4]])
                >>> np.pad(p, ((0,1),(0,1))) # Lengthen each dimension by 1.
                array([[1, 2, 0],
                       [3, 4, 0],
                       [0, 0, 0]])
                

                【讨论】:

                  猜你喜欢
                  • 2011-07-07
                  • 2013-12-22
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-08-19
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多