gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:23:21 24 4
gpt4 key购买 nike

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

我知道我可以使用 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]])]

关于python - 如何按索引拆分数组,其中拆分的子数组包含拆分点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com