我有一个看起来像这样的二维数组:
XX
xx
添加额外行和列的最有效方法是什么:
xxy
xxy
yyy
对于加分,我还希望能够剔除单行和单列,因此例如在下面的矩阵中,我希望能够剔除所有的 a,只留下 x - 特别是我试图同时删除第 n 行和第 n 列 - 我希望能够尽快做到这一点:
xxaxx
xxaxx
aaaaa
xxaxx
xxaxx
    我能想到的最短的代码行是第一个问题。
>>> 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]])
    第一个问题的有用替代答案,使用来自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 ]
这种技术也可以扩展到删除行和列的集合,所以如果我们想删除第 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]])
    第一个问题的另一个优雅的解决方案可能是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]])
    我发现通过分配更大的矩阵来“扩展”要容易得多。例如
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]])
    您可以使用:
>>> 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]])
    回答第一个问题:
使用 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
如果您想在新位置中使用恒定值,您可以使用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]])
    也许你需要这个。
>>> 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])