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

问题描述 投票:43回答:7

我有一个看起来像这样的2d数组:

XX
xx

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

xxy
xxy
yyy

对于奖励积分,我也希望能够淘汰单行和列,所以例如在下面的矩阵中我希望能够淘汰所有的只留下x的 - 特别是我是试图同时删除第n行和第n列 - 我希望能够尽快完成此操作:

xxaxx
xxaxx
aaaaa
xxaxx
xxaxx
python arrays math numpy
7个回答
51
投票

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

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

35
投票

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

7
投票

第一个问题的另一个优雅解决方案可能是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]])

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

2
投票

2
投票

您可以使用:

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

EG

>>> 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中的值。 '元组'只是括号中的一对数字。

EG

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

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])
© www.soinside.com 2019 - 2024. All rights reserved.