我有一个二维数组,它看起来像这样:
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()会更加方便,这些方法允许将1D数组附加到矩阵中而无需进行任何修改,与下面的情况形成对比:
>>> 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]])
您可以使用:
>>> np.concatenate([array1, array2, ...])
e.g.
>>> 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数组对象的形状属性只初始化部分值,就会遇到麻烦。这个问题可以通过将元组(数组长度,元素长度)赋值给形状属性来解决。
注意:这里,“array_length”和“element_length”是整数参数,你需要用实际数值替换它们。“元组”就是用括号括起来的一对数字。
例如:
>>> 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]])
我发现通过在一个更大的矩阵中进行赋值来"扩展"更加容易。例如:
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.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]])
第一个问题的答案:
使用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
也许你需要这个。
>>> 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])
np.append
是numpy
中被滥用最多的函数之一。大多数人错误地认为它就像列表的append
方法一样。如果您必须连接数组,请直接学习使用np.concatenate
(即使它对维度和形状很挑剔)。 - hpauljaxis
参数的append
相当于np.concatenate((arr, values), axis=axis)
。如果没有axis
参数,则首先对输入执行ravel操作,但是通过指定axis
参数可以直接调用concatenate
函数。 - hpaulj