首页 > 解决方案 > Python numpy 在矩阵的开头和结尾插入列

问题描述

我需要将边界附加到我的矩阵,它只是在矩阵的开头和最后一列和行的末尾重复第一列和行。

我有这个 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]]

如您所见,它已添加0 1 2 3 4为第一行和15 16 17 18 19最后一行。

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

temp1 = np.arange(4).reshape(4,1)
temp2 = np.arange(4, 8, 1).reshape(4,1)
matrix_t = np.insert(matrix, [0, 5], [temp1, temp2], axis=1)

然后我得到了这个错误:

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

当我这样做时,它工作得很好:

matrix_t = np.insert(matrix, [0, 5], temp1, axis=1)

[[ 0  0  1  2  3  4  0]                                                                                                                       
 [ 1  5  6  7  8  9  1]                                                                                                                       
 [ 2 10 11 12 13 14  2]                                                                                                                       
 [ 3 15 16 17 18 19  3]]

我错过了什么?

标签: pythonarraysnumpymatrix

解决方案


insert文档:

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

values规范意味着这两个匹配:

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

values不是list; 它是array_like,这意味着insert将从该输入创建一个数组。

当我们尝试添加新列时:

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) 插槽中:

In [52]: np.ones((4,7),int)[:,[0,5]]                                                           
Out[52]: 
array([[1, 1],
       [1, 1],
       [1, 1],
       [1, 1]])

如果我们在轴 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为了更简单的符号)轻松完成:

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

np.concatenate([arr[[0]],arr,arr[[-1]]])是相同的,其中arr[[0]]是 (1,5) 形。

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

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

推荐阅读