如何获得加权高斯滤波器 [英] How to obtain a weighted gaussian filter
问题描述
我有一组加权 x,y
点,如下所示(全套为这里):
I have a set of weighted x,y
points, like shown below (the full set is here):
# x y w
-0.038 2.0127 0.71
0.058 1.9557 1
0.067 2.0016 0.9
0.072 2.0316 0.83
...
我需要找到一条平滑的线,以根据分配给每个点的重要性来调整这些点,即:权重越大,则数据点应具有更大的相关性.
I need to find a smoothed line that adjusts these points according to the importance assigned to each, ie: more weight means the data point should have more relevance.
这是我到目前为止所拥有的代码,它基本上应用了 gaussian_filter1d 到数据(我从这个问题中得到了这个主意: python中的线平滑算法?):
This is the code I have so far, which basically applies a gaussian_filter1d to the data (I got the idea from this question: line smoothing algorithm in python?):
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter1d
# Read data from file.
data = np.loadtxt('data_file', unpack=True)
x, y, w = data[0], data[1], data[2]
# Return evenly spaced numbers over a specified interval.
t = np.linspace(0, 1, len(x))
t2 = np.linspace(0, 1, 100)
# One-dimensional linear interpolation.
x2 = np.interp(t2, t, x)
y2 = np.interp(t2, t, y)
# Obtain Gaussian filter with fixed sigma value.
sigma = 7
x3 = gaussian_filter1d(x2, sigma)
y3 = gaussian_filter1d(y2, sigma)
# Make plot.
cm = plt.cm.get_cmap('RdYlBu')
plt.scatter(x, y, marker="o", c=w, s=40, cmap=cm, lw=0.5, vmin=0, vmax=1)
plt.plot(x3, y3, "r", lw=2)
plt.show()
此代码生成以下图(蓝色点的权重值较高):
This code produces the following plot (bluer dots have a higher weight value):
问题在于,这种拟合不考虑分配给每个点的权重.如何将这些信息引入高斯滤波器?
The problem is that this fit does not consider the weights assigned to each point. How can I introduce that information into the gaussian filter?
推荐答案
请注意,以下想法不是解决方法,也不是确切的解决方案,但值得尝试.
Note that the following idea is workaround not an exact solution, but it is worth to try.
这个想法是使用w
weight参数来重复x
和y
中的相应值.因此,例如,如果将w
缩放到范围[1,10]
,则x
中的所有对应值,因此y
中的所有对应值将被重复10次,而w
等于10.即,新的x
,x
和y
中值的频率合并.完成此操作后,将新的算法提供给您的算法有望带来所需的结果,如下面的工作示例所示.
The idea is to use w
weight parameter to repeat corresponding values in x
and y
. So if you scale w
for example into range [1,10]
all corresponding values in x
and so in y
will be duplicated 10 times for w
equal to 10. That is, new x
, y
will be created. In this way we incorporate the weight as frequency of values in x
and y
, indeed. Having this done, feeding the new ones to your algorithm hopefully gives you desired results as shown in the worked examples below.
- 对于第一个数字,蓝色到红色的光谱对应于从低到高的权重.标题编号是如上所述的复制因子.
- 第二个数字,您的数据,我们没有碰到您的颜色格式.
这篇关于如何获得加权高斯滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!