Python numpy在矩阵的开头和结尾插入列

Python numpy在矩阵的开头和结尾插入列,python,arrays,numpy,matrix,Python,Arrays,Numpy,Matrix,我需要在我的矩阵中添加边界,它只是重复矩阵开头的第一列和第一行,最后一列和最后一行 我有这个PoC: matrix = np.arange(20).reshape(4,5) [[ 0 1 2 3 4] [ 5 6 7 8 9]

我需要在我的矩阵中添加边界,它只是重复矩阵开头的第一列和第一行,最后一列和最后一行

我有这个PoC:

matrix = np.arange(20).reshape(4,5)

[[ 0  1  2  3  4]                                                                                                                             
 [ 5  6  7  8  9]                                                                                                                             
 [10 11 12 13 14]                                                                                                                             
 [15 16 17 18 19]]
当我像这样在顶部和底部插入行时,效果很好

shape = matrix.shape (4,5)
matrix_t = np.insert(matrix, [0, shape[0]], [matrix[0], matrix[shape[0]-1]], axis=0)

[[ 0  1  2  3  4]                                                                                                                             
 [ 0  1  2  3  4]                                                                                                                             
 [ 5  6  7  8  9]                                                                                                                             
 [10 11 12 13 14]                                                                                                                             
 [15 16 17 18 19]                                                                                                                             
 [15 16 17 18 19]]
如您所见,它将
01234
添加为第一行,将
15171819
添加为最后一行

现在我想做同样的事情,只是在左侧和右侧附加列。将上面的代码稍微简化一点,我就是这样做的(需要重塑以创建列向量)

然后我得到了这个错误:

Traceback (most recent call last):                                                                                                            
  File "main.py", line 33, in <module>                                                                                                        
    matrix_t = np.insert(matrix, [0, 5], [temp1, temp2], axis=1)                                                                              
  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 3496, in insert                                                      
    new[slobj] = values                                                                                                                       
ValueError: total size of new array must be unchanged

我缺少什么?

插入
文档:

values : array_like
    Values to insert into `arr`. If the type of `values` is different
    from that of `arr`, `values` is converted to the type of `arr`.
    `values` should be shaped so that ``arr[...,obj,...] = values``
    is legal.
开始阵列:

In [40]: arr = np.arange(20).reshape(4,5)  
添加新行:

In [42]: np.insert(arr, [0, 4], [arr[0], arr[-1]], axis=0)                                     
Out[42]: 
array([[ 0,  1,  2,  3,  4],
       [ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [15, 16, 17, 18, 19]])
规范意味着这两个匹配:

In [48]: np.array([arr[0], arr[-1]])                                                           
Out[48]: 
array([[ 0,  1,  2,  3,  4],
       [15, 16, 17, 18, 19]])
In [49]: Out[42][[0,4],:]                                                                      
Out[49]: 
array([[ 0,  1,  2,  3,  4],
       [15, 16, 17, 18, 19]])
不是
列表
;它类似于
,意味着插入将从该输入创建一个数组

当我们尝试添加新列时:

In [50]: temp1 = np.arange(4).reshape(4,1) 
    ...: temp2 = np.arange(4, 8, 1).reshape(4,1) 
    ...: np.insert(arr, [0, 5], [temp1, temp2], axis=1)                                        
---------------------------------------------------------------------------
...
ValueError: shape mismatch: value array of shape (2,4,1) could not be broadcast to indexing result of shape (2,4)
一个不同的信息,但同样的问题。查看值列表的数组版本:

In [51]: np.array([temp1, temp2])                                                              
Out[51]: 
array([[[0],
        [1],
        [2],
        [3]],

       [[4],
        [5],
        [6],
        [7]]])
这就是(2,4,1)数组。它试图将其放入(2,4)槽中:

如果我们在轴1上连接temp,以形成(2,4)阵列,则插入工作:

In [53]: np.concatenate([temp1,temp2], axis=1)                                                 
Out[53]: 
array([[0, 4],
       [1, 5],
       [2, 6],
       [3, 7]])

In [54]: np.insert(arr, [0, 5], Out[53], axis=1)                                               
Out[54]: 
array([[ 0,  0,  1,  2,  3,  4,  4],
       [ 1,  5,  6,  7,  8,  9,  5],
       [ 2, 10, 11, 12, 13, 14,  6],
       [ 3, 15, 16, 17, 18, 19,  7]])
np.insert
是通用的,试图处理很多情况,因此理解输入可能很困难

===

您的第一次插入同样可以通过索引或连接来轻松完成(
vstack
更简单的表示法):

np.连接([arr[[0]],arr,arr[-1]])
是相同的,其中
arr[[0]]
是(1,5)形状

带有列连接(
temp1
的列插入已具有(4,1)形状):


哇,我想你对python很了解:)谢谢你的解释,我现在明白了。我是python界的新手。也许如果我的错误信息是那样的具体,我会找到答案。顺便说一句,这个带-1索引的语法很酷,我忘了。
In [51]: np.array([temp1, temp2])                                                              
Out[51]: 
array([[[0],
        [1],
        [2],
        [3]],

       [[4],
        [5],
        [6],
        [7]]])
In [52]: np.ones((4,7),int)[:,[0,5]]                                                           
Out[52]: 
array([[1, 1],
       [1, 1],
       [1, 1],
       [1, 1]])
In [53]: np.concatenate([temp1,temp2], axis=1)                                                 
Out[53]: 
array([[0, 4],
       [1, 5],
       [2, 6],
       [3, 7]])

In [54]: np.insert(arr, [0, 5], Out[53], axis=1)                                               
Out[54]: 
array([[ 0,  0,  1,  2,  3,  4,  4],
       [ 1,  5,  6,  7,  8,  9,  5],
       [ 2, 10, 11, 12, 13, 14,  6],
       [ 3, 15, 16, 17, 18, 19,  7]])
In [56]: arr[[0]+[0,1,2,3]+[3]]                                                                
Out[56]: 
array([[ 0,  1,  2,  3,  4],
       [ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [15, 16, 17, 18, 19]])
In [57]: np.vstack([arr[0],arr,arr[-1]])                                                       
Out[57]: 
array([[ 0,  1,  2,  3,  4],
       [ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [15, 16, 17, 18, 19]])
In [58]: np.concatenate([temp1, arr, temp2], axis=1)                                           
Out[58]: 
array([[ 0,  0,  1,  2,  3,  4,  4],
       [ 1,  5,  6,  7,  8,  9,  5],
       [ 2, 10, 11, 12, 13, 14,  6],
       [ 3, 15, 16, 17, 18, 19,  7]])