python - How to split array by indices where the splitted sub-arrays include the split point
问题描述
I have a 2D array containing values and a 1D array with index values where I would like to split the 2D matrix, where the splitted sub-arrays include the 'split-point'.
I know I can use the numpy.split
function to split by indices and I know I can use stride_tricks
to split an array for creating consecutive overlapping subset-views.
But it seems the stride_ticks
only applies if we want to split an array into equal sized sub-arrays.
Minimal example, I can do the following:
>>> 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]])]
But I'm merely looking for an option within the split
function where I could define include_split_point=True
, which would give me a result as such:
[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]])]
推荐阅读
- node.js - 使用 NPM 安装 Dat
- python - 在python中制作素数但在打印else语句时出错
- python - 创建一个包含 N 个随机 (x,y,z) 点的列表,这些点彼此之间的距离至少为 r
- wso2 - 将 WSO2 API 管理器与 Identity Server 和单独的 Identity Server 密钥管理器一起使用
- excel - 如何解决 VBA 中的函数运行时错误
- r - 在一个 df 中使用名称来创建另一个数据框的相同列副本
- spring-boot - Swagger 返回响应:“无法解析 JSON。原始结果:”
- sql - 如何使用 SQL 显示每年“最受欢迎的名字”
- acumatica - 在选择器控制方面需要帮助
- python - 在 tkinter 中重新创建从左到右的滚动文本