我试图在python中重新实现一个IDL函数:
http://star.pst.qub.ac.uk/idl/REBIN.html
通过求平均值减去2d阵列的整数因子.
例如:
>>> 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]])
Run Code Online (Sandbox Code Playgroud)
我想通过取相关样本的平均值将其调整为(2,3),预期输出为:
>>> b = rebin(a, (2, 3))
>>> b
array([[ 3.5, 5.5, 7.5],
[ 15.5, 17.5, 19.5]])
Run Code Online (Sandbox Code Playgroud)
即b[0,0] = np.mean(a[:2,:2]), b[0,1] = np.mean(a[:2,2:4])等等.
我相信我应该重塑为4维数组,然后在正确的切片上取平均值,但无法弄清楚算法.你有什么提示吗?
I have an array and I would like to produce a smaller array by scanning a 2x2 non-overlappingly windows and getting the maximum. Here is an example:
import numpy as np
np.random.seed(123)
np.set_printoptions(linewidth=1000,precision=3)
arr = np.random.uniform(-1,1,(4,4))
res = np.zeros((2,2))
for i in xrange(res.shape[0]):
for j in xrange(res.shape[1]):
ii = i*2
jj = j*2
res[i][j] = max(arr[ii][jj],arr[ii+1][jj],arr[ii][jj+1],arr[ii+1][jj+1])
print arr
print res
Run Code Online (Sandbox Code Playgroud)
So a matrix like this:
[[ 0.393 -0.428 -0.546 0.103]
[ 0.439 -0.154 0.962 0.37 ]
[-0.038 -0.216 -0.314 0.458] …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以通过在Numpy或Scipy中平均特定大小的块,甚至使用 NetC D F 工具或类似工具来减小 3D 矩阵的大小?我不久前使用 strides 编写了一个 2D 函数,但是一个随时可用的函数会有很大帮助。
编辑:
我希望输入和输出的示例如下:
Input's shape: (500, 500, 100)
Calling the function: downsize(input, 10, 10, 10, func)
Output's shape: (50, 50, 10)func其中每个单元格的值都是连续 10x10x10 子矩阵的结果。
或者,代码可以获取所需的矩阵大小作为输入,而不是子矩阵的大小并计算出来。
谢谢