一维numpy数组的Python中值过滤器 [英] Python Median Filter for 1D numpy array

查看:102
本文介绍了一维numpy数组的Python中值过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尺寸为 dim_array numpy.array .我期待获得像 scipy.signal.medfilt(data,window_len)这样的中值过滤器.

I have a numpy.array with a dimension dim_array. I'm looking forward to obtain a median filter like scipy.signal.medfilt(data, window_len).

这实际上不适用于 numpy.array ,可能是因为维度是(dim_array,1)而不是(dim_array,).

This in fact doesn't work with numpy.array may be because the dimension is (dim_array, 1) and not (dim_array, ).

如何获得这种过滤器?

接下来,另一个问题,如何获得其他过滤器,即最小,最大,均值?

Next, another question, how can I obtain other filter, i.e., min, max, mean?

推荐答案

基于 此帖子 ,我们可以创建滑动窗口以将此类窗口的 2D 数组设置为其中的行.这些窗口仅是 data 数组的视图,因此没有内存消耗,因此非常有效.然后,我们只需沿每行 axis = 1 使用这些 ufuncs .

Based on this post, we could create sliding windows to get a 2D array of such windows being set as rows in it. These windows would merely be views into the data array, so no memory consumption and thus would be pretty efficient. Then, we would simply use those ufuncs along each row axis=1.

因此,例如 sliding- median`可以这样计算-

Thus, for example sliding-median` could be computed like so -

np.median(strided_app(data, window_len,1),axis=1)

对于其他 ufuncs ,只需在其中使用相应的 ufunc 名称: np.min np.max & np.mean .请注意,这是为了提供使用 ufunc 支持的功能的通用解决方案.

For the other ufuncs, just use the respective ufunc names there : np.min, np.max & np.mean. Please note this is meant to give a generic solution to use ufunc supported functionality.

为了获得最佳性能,仍然必须研究为这些目的而构建的特定功能.对于所请求的四个函数,我们具有内建函数,例如-

For the best performance, one must still look into specific functions that are built for those purposes. For the four requested functions, we have the builtins, like so -

中位数: scipy.signal.medfilt .

Max: scipy.ndimage.filters.maximum_filter1d .

Max : scipy.ndimage.filters.maximum_filter1d.

Min: scipy.ndimage.filters.minimum_filter1d .

Min : scipy.ndimage.filters.minimum_filter1d.

平均值: scipy.ndimage.filters.uniform_filter1d

这篇关于一维numpy数组的Python中值过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆