【发布时间】:2020-07-27 17:59:00
【问题描述】:
我正在尝试自动拟合功率谱密度。一个典型的情节看起来像这样(loglog scale):
红色是我尝试安装它的尝试之一。 我想有一种方法可以将最多 3 个线性段拟合到功率谱密度图。 我已经尝试了几种方法,例如使用 curve_fit 查看1:
def logfit(x, a1, a2, b, cutoff):
cutoff = int(params[3])
out = np.empty_like(x)
out[:cutoff] = x[:cutoff]*a1 + b
out[cutoff:] = x[cutoff]*a1 + b + (x[cutoff:] - x[cutoff])*a2
return out
#the fit is only linear on loglog scale, need to apply np.log10 to x and y.
popt, pcov = curve_fit(logfit, np.log10(x), np.log10(y), bounds = ([-2,-2,-np.inf,99],[0,0,np.inf,100]))
这通常提供扁平合身。 我还查看了 [2],使用了 lmfit 包:
def logfit(params, x, data):
a1, a2, b = params['a1'], params['a2'], params['b']
c = int(params['cutoff'])
out = np.empty_like(x)
out[:c] = x[:c]*a1 + b
out[c:] = x[c]*a1 + b + (x[c:] - x[c])*a2
return data - out
params = Parameters()
params.add('a1', value = -0.3, min = -2, max = 0)
params.add('a2', value = -2, min = -2, max = -0.5)
params.add('b', min = 0, max = 5)
params.add('cutoff', min = 150, max = 500)
params = minimize(logfit, params, args=(f, pxx))
但这也会产生完全关闭的拟合(如上图中的红色拟合)。 这是因为参数太多了吗?我原以为这个问题很简单,可以通过优化来解决……
1How to apply piecewise linear fit for a line with both positive and negative slopes in Python?
【问题讨论】:
标签: optimization linear-regression curve-fitting least-squares