我有一些代码,逻辑上是最好的设置为重嵌套数组。 整体结构是高维稀疏的,所以我必须按照稀疏实现的要求将其转换为二维矩阵,这样它才能适合内存。你知道吗
我现在发现自己在两种格式之间切换,这是复杂和混乱的。我已经编写了一个小函数,从嵌套的输入中计算2d单元格,但是如果我想做一个范围查询,它会变得更加复杂。你知道吗
import numpy as np
dim1 = 1
dim2 = 2
dim3 = 3
dim4 = 4
dim5 = 5
dim6 = 6
sixD = np.arange(720).reshape(dim1, dim2, dim3, dim4, dim5, dim6)
twoD = sixD.transpose(0,1,2,3,4,5).reshape(dim1,-1)
def sixDto2DCell(a, b, c, d, e, f):
return [a, (b*dim3*dim4*dim5*dim6) +
(c*dim4*dim5*dim6) +
(d*dim5*dim6) +
(e*dim6) +
f]
x, y = sixDto2DCell(0, 1, 2, 3, 4, 5)
assert(sixD[0, 1, 2, 3, 4, 5] == twoD[x, y])
所以我在想我该怎么处理这样的查询
sixD[0, 1, 0:, 3, 4, 5]
在二维矩阵中返回相同的值
我是否需要编写一个新函数,或者我是否错过了实现相同功能的内置numpy方法?你知道吗
任何帮助都将不胜感激:-)
进近#1
这里有一种方法可以从2D稀疏矩阵或任何2D数组中提取数据,该数组具有相应的n-dim数组及其沿每个轴的开始和结束索引-
使用提供的样本研究样本案例运行-
进近#2
下面是另一个关于创建沿每个轴的所有索引组合,然后使用
np.ravel_multi_index
来获得平坦的索引-相关问题 更多 >
编程相关推荐