通过在 numpy/sparse 中将 n^dim 个元素加在一起进行下采样
Downsampling by adding n^dim elements together in numpy/sparse
对于个人项目,我正在尝试对 3d 网格进行下采样,表示为 sparse.COO
数组,具有类似图像的数据(索引编码空间信息,值编码质量)。
据我所知,稀疏涵盖了numpy最重要的部分API,所以我将从这里开始以numpy为例。
不幸的是,一些框架不能很好地处理包含许多非零条目的数组。
我尝试编写一个函数,根据这个问题 NumPy: sum every n columns of matrix,将 2^3=8 个元素的块加在一起,但我的版本弄乱了索引:
sparray = np.arange(10*10*10).reshape(10,10,10)
#number of entries to sum in every direction, 2 for only neighbours
num_entries = 2
#the last 3 axis of the reshaped array
axis = tuple(range(-sparray.ndim,0))
downsampled_array = sparray.reshape(
tuple(
[int(s/num_entries) for s in sparray.shape]+[num_entries]*sparray.ndim
)
).sum(axis=axis)
一个二维示例示例:
sparray = np.arange(4*4).reshape(4,4)
>>>array(
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
axis = tuple(range(-sparray.ndim,0))
>>>(-2, -1)
#This is what the code does:
>>>array(
[[ 6, 22],
[38, 54]])
#should do:
array(
[[10, 19],
[42, 50]]
)
在此先感谢,这可能是一些非常愚蠢的错误。
已解决。需要交错额外的维度:
def downsample(sparray: sparse.COO, block_size: int):
if any(x%block_size != 0 for x in sparray.shape):
return IndexError('One of the Array dimensions is not divisible by the block_size')
axis = tuple(range(1, 2 * sparray.ndim + 1, 2))
shape = tuple(index for s in sparray.shape for index in (int(s/block_size), block_size))
return sparray.reshape(shape).sum(axis=axis)
对于个人项目,我正在尝试对 3d 网格进行下采样,表示为 sparse.COO
数组,具有类似图像的数据(索引编码空间信息,值编码质量)。
据我所知,稀疏涵盖了numpy最重要的部分API,所以我将从这里开始以numpy为例。 不幸的是,一些框架不能很好地处理包含许多非零条目的数组。
我尝试编写一个函数,根据这个问题 NumPy: sum every n columns of matrix,将 2^3=8 个元素的块加在一起,但我的版本弄乱了索引:
sparray = np.arange(10*10*10).reshape(10,10,10)
#number of entries to sum in every direction, 2 for only neighbours
num_entries = 2
#the last 3 axis of the reshaped array
axis = tuple(range(-sparray.ndim,0))
downsampled_array = sparray.reshape(
tuple(
[int(s/num_entries) for s in sparray.shape]+[num_entries]*sparray.ndim
)
).sum(axis=axis)
一个二维示例示例:
sparray = np.arange(4*4).reshape(4,4)
>>>array(
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
axis = tuple(range(-sparray.ndim,0))
>>>(-2, -1)
#This is what the code does:
>>>array(
[[ 6, 22],
[38, 54]])
#should do:
array(
[[10, 19],
[42, 50]]
)
在此先感谢,这可能是一些非常愚蠢的错误。
已解决。需要交错额外的维度:
def downsample(sparray: sparse.COO, block_size: int):
if any(x%block_size != 0 for x in sparray.shape):
return IndexError('One of the Array dimensions is not divisible by the block_size')
axis = tuple(range(1, 2 * sparray.ndim + 1, 2))
shape = tuple(index for s in sparray.shape for index in (int(s/block_size), block_size))
return sparray.reshape(shape).sum(axis=axis)