我试图在python中重新实现IDL函数:
http://star.pst.qub.ac.uk/idl/REBIN.html
它通过平均缩小二维数组的整数倍。
例如:
>>> a=np.arange(24).reshape((4,6))
>>> a
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
我想通过取相关样本的平均值将其调整为(2,3),预期输出为:
>>> b = rebin(a, (2, 3))
>>> b
array([[ 3.5, 5.5, 7.5],
[ 15.5, 17.5, 19.5]])
即b[0,0] = np.mean(a[:2,:2]), b[0,1] = np.mean(a[:2,2:4])
等。
我相信我应该重塑成一个四维数组,然后在正确的切片上取平均值,但无法找出算法。你有什么线索吗?
我试着缩小一个光栅——取一个大约6000×2000大小的光栅,把它变成一个任意大小的小光栅,在以前的存储箱大小上取平均值。我找到了一个使用SciPy的解决方案,但是我无法让SciPy安装到我正在使用的共享托管服务上,所以我只编写了这个函数。可能有更好的方法可以做到这一点,不需要在行和列之间循环,但这似乎确实有效。
这方面的好处是,旧的行数和列数不必被新的行数和列数整除。
J.F.塞巴斯蒂安对二维宾宁有很好的回答。下面是他的“rebin”函数的一个版本,它适用于N个维度:
相关问题 更多 >
编程相关推荐