通过平均调整大小或重新组合 numpy 二维数组
我正在尝试在 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])
等等。
我相信我应该重塑为 4 维数组,然后在正确的切片上取平均值,但无法找出算法。你有什么提示吗?
I am trying to reimplement in python an IDL function:
http://star.pst.qub.ac.uk/idl/REBIN.html
which downsizes by an integer factor a 2d array by averaging.
For example:
>>> 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]])
I would like to resize it to (2,3) by taking the mean of the relevant samples, the expected output would be:
>>> b = rebin(a, (2, 3))
>>> b
array([[ 3.5, 5.5, 7.5],
[ 15.5, 17.5, 19.5]])
i.e. b[0,0] = np.mean(a[:2,:2]), b[0,1] = np.mean(a[:2,2:4])
and so on.
I believe I should reshape to a 4 dimensional array and then take the mean on the correct slice, but could not figure out the algorithm. Would you have any hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是基于您链接的答案的示例(为了清楚起见):
作为一个函数:
Here's an example based on the answer you've linked (for clarity):
As a function:
JF Sebastian 对于 2D 分箱有一个很好的答案。这是他的“rebin”函数的一个版本,适用于 N 维:
J.F. Sebastian has a great answer for 2D binning. Here is a version of his "rebin" function that works for N dimensions:
这是一种使用矩阵乘法来完成您所要求的操作的方法,不需要新的数组维度来除旧的数组维度。
首先,我们生成一个行压缩器矩阵和一个列压缩器矩阵(我确信有一种更简洁的方法可以做到这一点,甚至可能单独使用 numpy 操作):
...所以,例如,
get_row_compressor(5, 3)
为您提供:并且
get_column_compressor(3, 2)
为您提供:然后只需通过行压缩器进行预乘,通过列压缩器进行后乘即可获得压缩矩阵:
使用此技术,
产量:
Here's a way of doing what you ask using matrix multiplication that doesn't require the new array dimensions to divide the old.
First we generate a row compressor matrix and a column compressor matrix (I'm sure there's a cleaner way of doing this, maybe even using numpy operations alone):
... so, for instance,
get_row_compressor(5, 3)
gives you:and
get_column_compressor(3, 2)
gives you:Then simply premultiply by the row compressor and postmultiply by the column compressor to get the compressed matrix:
Using this technique,
yields:
我试图缩小栅格的比例 - 采用大约 6000 x 2000 大小的栅格,并将其转换为任意大小的较小栅格,该栅格在之前的 bin 大小中正确平均了值。我找到了使用 SciPy 的解决方案,但后来我无法将 SciPy 安装在我正在使用的共享托管服务上,所以我只是编写了这个函数。可能有更好的方法来做到这一点,不涉及循环遍历行和列,但这似乎确实有效。
这样做的好处是旧的行数和列数不必被新的行数和列数整除。
I was trying to downscale a raster -- take a roughly 6000 by 2000 size raster and turn it into an arbitrarily sized smaller raster that averaged the values properly across the previous bins sizes. I found a solution using SciPy, but then I couldn't get SciPy to install on the shared hosting service I was using, so I just wrote this function instead. There is likely a better ways to do this that doesn't involve looping through the rows and columns, but this does seem to work.
The nice part about this is that the old number of rows and columns don't have to be divisible by the new number of rows and columns.
我对 MarcTheSpark 的答案有疑问,除了某些特定的输出形状外,该答案效果很好。我必须在 get_row_compressor 的第一个条件中更改 round() 函数的值。
如果我的声誉足够的话,我只会发表评论。
我还添加了一个片段来重新整理一维数组。
I had a problem with MarcTheSpark's answer, which worked great except for some specific output shapes. I had to change the value of the round() function in the first condition of get_row_compressor.
Would have commented only, if my reputation was enough.
I also add a snippet to rebin 1D arrays.