[英]Separable filter on numpy array
Say I have a numpy array a
, and I want to create a new array, b
such that b[i, j]
is a function of, say: 假设我有一个numpy数组
a
,我想创建一个新数组, b
这样b[i, j]
是函数,比如说:
a[i-1, j-1], a[i-1, j ], a[i-1, j+1],
a[i , j-1], a[i , j ], a[i , j+1],
a[i+1, j-1], a[i+1, j ], a[i+1, j+1]
What would be the fastest way to do this? 最快的方法是什么?
As this is a separable filter, is there any way to run this in multiple threads? 由于这是一个可分离的过滤器,有没有办法在多个线程中运行它? (not processes, because I would have to copy the data back)
(不是进程,因为我必须将数据复制回来)
Or is writing C code to bypass the GIL mandatory? 或者正在编写C代码以绕过GIL强制执行?
Partial solutions (like assuming the function is linear) are welcome too. 部分解决方案(如假设功能是线性的)也是受欢迎的。
An idealized numpy
way of working with a sliding window like this is to construct a 4D array 像这样使用滑动窗口的理想化的
numpy
方法是构造一个4D阵列
C.shape = (N,M,3,3)
where 哪里
C[i,j,:,:] = np.array([a[i-1, j-1], a[i-1, j ], a[i-1, j+1],
a[i , j-1], a[i , j ], a[i , j+1],
a[i+1, j-1], a[i+1, j ], a[i+1, j+1]])
and write your function do some sort of reduction on the last 2 dimensions. 并编写你的函数在最后2个维度上做一些减少。
sum
or mean
would be typical, eg sum
或mean
是典型的,例如
B = C.sum(axis=(2,3))
Other SO questions show how to use np.lib.stride_tricks.as_strided
to construct such an array. 其他SO问题显示如何使用
np.lib.stride_tricks.as_strided
构造这样的数组。 But with only a 3x3 subarray, it might be just as fast to do something like 但是只有3x3的子阵列,做同样的事情可能同样快
C = np.zeros((N,M,3,3))
C[:,:,0,0] = a[:-1,:-1]
etc.
(or use hstack
and vstack
to the same effect). (或使用
hstack
和vstack
达到相同的效果)。
But a nice thing (or maybe not so nice) about the strided approach is that it doesn't involve copy any data of a
- it is just a view. 但是一件很好的事(或许不是那么好)有关跨入的办法是,它不涉及复制的任何数据
a
-它只是一个视图。
As to splitting the job into pieces, I can imagine using slices of C
(on the 1st 2 dimensions), eg 至于将作业分成几部分,我可以想象使用
C
片(在前2个维度上),例如
C[0:100,0:100,:,:].sum(axis=(2,3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.