繁体   English   中英

在二维中扩展 numpy 数组的最简单方法是什么?

[英]What's the simplest way to extend a numpy array in 2 dimensions?

我有一个看起来像这样的二维数组:

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 的vstackcolumn_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' 是整数参数,您可以将其替换为值。 “元组”只是括号中的一对数字。

例如

>>> 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]])

如果你想在新位置有一个常数值,你可以使用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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM