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

Sal*_*ley 45 python arrays math numpy

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

XX
xx
Run Code Online (Sandbox Code Playgroud)

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

xxy
xxy
yyy
Run Code Online (Sandbox Code Playgroud)

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

xxaxx
xxaxx
aaaaa
xxaxx
xxaxx
Run Code Online (Sandbox Code Playgroud)

tom*_*dee 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]])
Run Code Online (Sandbox Code Playgroud)

而对于第二个问题

    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]])
Run Code Online (Sandbox Code Playgroud)

  • `np.append`是`numpy`中滥用最多的函数之一.大多数情况下,人们认为它就像列表追加一样.如果你必须连接数组,学会直接使用`np.concatenate`(即使它对尺寸和形状很挑剔). (3认同)

rro*_*ndd 36

一个有用的替代回答第一个问题,用实例从 tomeedee的答案,是使用numpy的的 vstack column_stack 方法:

给定矩阵p,

>>> import numpy as np
>>> p = np.array([ [1,2] , [3,4] ])
Run Code Online (Sandbox Code Playgroud)

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

>>> 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]])
Run Code Online (Sandbox Code Playgroud)

这些方法在实践中可能比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
Run Code Online (Sandbox Code Playgroud)

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

给定矩阵p,

>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
Run Code Online (Sandbox Code Playgroud)

假设我们要删除第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]])
Run Code Online (Sandbox Code Playgroud)

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

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

此技术也可以扩展为删除行和列的集合,因此如果我们想要删除行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]])
Run Code Online (Sandbox Code Playgroud)


Bjö*_*örn 7

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

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

导致:

array([[1, 2, 0],
       [3, 4, 0]])
Run Code Online (Sandbox Code Playgroud)

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

至于第二个问题,delete之前有人建议:

p = np.delete(p, 2, axis=1)
Run Code Online (Sandbox Code Playgroud)

这会再次恢复原始数组:

array([[1, 2],
       [3, 4]])
Run Code Online (Sandbox Code Playgroud)


ube*_*kel 6

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

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
Run Code Online (Sandbox Code Playgroud)

以下是数组:

p

   array([[1, 2],
       [3, 4]])
Run Code Online (Sandbox Code Playgroud)

g

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

以及g分配后的结果:

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


Jas*_*son 5

您可以使用:

>>> np.concatenate([array1, array2, ...]) 
Run Code Online (Sandbox Code Playgroud)

例如

>>> 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]]
Run Code Online (Sandbox Code Playgroud)

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

有时,如果 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]])
Run Code Online (Sandbox Code Playgroud)