Skip to main content
 首页 » 编程设计

python之过采样 Numpy 数组(二维)

2025年05月04日15lidabo

<分区>

numpy/scipy 中是否有函数可以对 2D numpy 数组进行过采样?

例子:

>>> x = [[1,2] 
     [3,4]] 
>>>  
>>> y = oversample(x, (2, 3)) 

会返回

y = [[1,1,2,2], 
     [1,1,2,2], 
     [1,1,2,2], 
     [3,3,4,4], 
     [3,3,4,4], 
     [3,3,4,4]] 

目前我已经实现了自己的功能:

index_x = np.arange(newdim) / olddim 
index_y = np.arange(newdim) / olddim 
 
xx, yy = np.meshgrid(index_x, index_y) 
return x[yy, xx, ...] 

但这看起来不是最好的方法,因为它只适用于 2D reshape 并且有点慢......

有什么建议吗? 非常感谢