Skip to main content
 首页 » 编程设计

python之如何按索引拆分数组,其中拆分的子数组包含拆分点

2024年12月31日7Renyi-Fan

我有一个包含值的二维数组和一个带有索引值的一维数组,我想在其中拆分二维矩阵,其中拆分的子数组包括“拆分点”。

我知道我可以使用 numpy.split 函数按索引拆分,我知道我可以使用 stride_tricks 拆分数组以创建连续的重叠子集 View 。

但是 stride_ticks 似乎只适用于我们想要将数组拆分为大小相等的子数组的情况。

最小示例,我可以执行以下操作:

>>> import numpy as np 
>>> array = np.random.randint(0,10, (10,2)) 
>>> indices = np.array([2,3,8]) 
>>> array 
array([[8, 1], 
       [1, 0], 
       [2, 0], 
       [8, 8], 
       [1, 6], 
       [7, 8], 
       [4, 4], 
       [9, 4], 
       [6, 7], 
       [6, 4]]) 
 
>>> split_array = np.split(array, indices, axis=0) 
>>> split_array 
[array([[8, 1], 
        [1, 0]]),  
 
 array([[2, 0]]),  
 
 array([[8, 8], 
        [1, 6], 
        [7, 8], 
        [4, 4], 
        [9, 4]]),  
 
 array([[6, 7], 
        [6, 4]])] 

但我只是在 split 函数中寻找一个选项,我可以在其中定义 include_split_point=True,这会给我这样的结果:

[array([[8, 1], 
        [1, 0], 
        [2, 0]]),  
 
 array([[2, 0], 
        [8, 8]]),  
 
 array([[8, 8], 
        [1, 6], 
        [7, 8], 
        [4, 4], 
        [9, 4], 
        [6, 7]]),  
 
 array([[6, 7], 
        [6, 4]])] 

请您参考如下方法:

创建一个索引元素重复的新数组

new_indices = np.zeros(array.shape[0], dtype = int) 
new_indices[indices] = 1 
new_indices += 1 
new_array = np.repeat(array, new_indices, axis = 0) 

更新索引以说明更改的数组

indices = indices + np.arange(1, len(indices)+1) 

照常使用索引拆分

np.split(new_array, indices, axis = 0) 

输出:

[array([[8, 1], 
        [1, 0], 
        [2, 0]]),  
 array([[2, 0], 
        [8, 8]]),  
 array([[8, 8], 
        [1, 6], 
        [7, 8], 
        [4, 4], 
        [9, 4], 
        [6, 7]]),  
 array([[6, 7], 
        [6, 4]])]