【问题标题】:Forcing data to fit points with curve_fit使用curve_fit强制数据拟合点
【发布时间】:2017-03-17 00:59:49
【问题描述】:

我在使用 Scipy 中包含的 curve_fit 函数时遇到了一点问题。这是我想要适应的功能:

def funclog(x, a, b, c, d):
   return a * np.log(b * x + c) + d

我遇到的问题是我希望拟合函数在某些点上具有特定值(y(min)=0 和 y(max)=1)。如何使用 curve_fit 强制这些点?

谢谢

【问题讨论】:

标签: python numpy scipy


【解决方案1】:

x=0x=1 处具有特定值的拟合要求意味着参数abcd 根据两个方程组受到约束:

funclog(0, a, b, c, d) = 0, funclog(1, a, b, c, d) = 1

对于您正在考虑的funclog 的形式,您可以求解关于ad 的方程组,得到(唯一)解

a = 1/(-log(c) + log(b + c))d=log(c)/(log(c) - log(b + c))

(假设bc 使得分母不等于0)。

ad 的这些表达式替换为funclog 会得到一个新的拟合函数,即,

(log(c) - log(b*x + c))/(log(c) - log(b + c)),

默认情况下满足约束。 bc 的值可以通过curve_fit 找到。

【讨论】:

  • 非常感谢,这就是我要找的!
【解决方案2】:

您可以尝试使用边界:

bounds = ([amin, bmin, cmin, dmin], [amax, bmax, cmax, dmax])
(or np.inf  -np.inf if limes of param is in infininty)

下一个

popt1, pcov1 = curve_fit(funclog, x, y, bounds=bounds)

【讨论】:

  • 问题是我不想绑定我的参数而是函数本身的结果
  • 也许您应该在拟合后尝试“标准化”结果?你知道最大 Y 为 1,最小 Y 为 0?
猜你喜欢
  • 2012-06-07
  • 2021-11-05
  • 2016-01-16
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2020-05-05
  • 2019-02-12
  • 1970-01-01
相关资源
最近更新 更多