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

2024-04-23 07:58:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我错过了什么


Tags: inpynpline矩阵matrixfileinsert
1条回答
网友
1楼 · 发布于 2024-04-23 07:58:56

{}文档:

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

相关问题 更多 >