我想知道是否可以在curve_fit()
中设置参数的界限,使得界限依赖于另一个参数。例如,假设我想将直线的斜率设置为大于截距。
def linear(x, m, b):
return lambda x: (m*x) + b
def plot_linear(x, y):
B = ([b, -np.inf], [np.inf, np.inf])
p, v = curve_fit(linear, x, y, bounds = B)
xs = np.linspace(min(x), max(x), 1000)
plt.plot(x,y,'.')
plt.plot(xs, linear(xs, *p), '-')
我知道这不起作用,因为参数 b 在边界内调用之前没有定义,但我不确定是否有办法使其起作用?
最佳答案
我们总是可以重新参数化w.r.t。具体的曲线拟合问题。例如,如果您想拟合y=mx+b s.t. m >= b,它可以用另一个参数k重写为m=b+k*k,我们可以用参数b,k现在如下:
def linear(x, m, b):
return m*x + b
def linear2(x, k, b): # constrained fit, m = b + k**2 >= b
return (b+k**2)*x + b
def plot_linear(x, y):
p, v = curve_fit(linear, x, y)
print(p)
# [3.1675609 6.01025041]
p2, v2 = curve_fit(linear2, x, y)
print(p2)
# [2.13980283e-05 4.99368661e+00]
xs = np.linspace(min(x), max(x), 1000)
plt.plot(x,y,'.')
plt.plot(xs, linear(xs, *p), 'r-', label='unconstrained fit')
plt.plot(xs, linear2(xs, *p2), 'b-', label='constrained (m>b) fit')
plt.legend()
现在让我们使用约束和无约束拟合函数来拟合以下数据的曲线(请注意,无约束最佳拟合的斜率将小于截距)
x = np.linspace(0,1,100)
y = 3*x + 5 + 2*np.random.rand(len(x))
plot_linear(x, y)
关于python - 使用参数作为 scipy.optimize.curve_fit 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66238980/