尝试使用 scipy.curve_fit 将数据拟合到 cotangens 函数

发布于 2025-01-10 17:22:09 字数 1434 浏览 0 评论 0原文

我的数据与 arccos(x) 的外观非常相似,所以我认为我会成功地做到这一点。使用代码:

from scipy.optimize import curve_fit
        
x0 = [240, 320] 
y0 = [100, 99, 90, 60, 30, 10, 1, 0, 0] 

x = np.linspace(x0[0], x0[1], int((x0[1]-x0[0])/10+1))

def fun(x, a, b, c):
    return np.pi/2 - c*np.arctan(x*a + b)
        
p, c = curve_fit(f=fun, xdata=x, ydata=y0)
plt.plot(x, fun(x, *p))

我得到几乎直线 - 不是 arccot 形状,并且 y 域在 3.6 和 4.6 之间,不在数据范围 (0, 100) 内。 我想我遗漏了一些东西,但是设置 bounds 没有帮助。也许应该填充函数中的一些附加参数或者我自己的参数是错误的?请给我一些提示。

编辑

发现错误:

def fun(x, a, b, c, d):
    return (np.pi/2 - np.arctan(x*a + c)) * b + d

该函数已被分离,并且还添加了新参数。

顺便说一句,我发现这些数据拟合点使 arccot 的行为不像这种类型的典型函数,因为新的 y

array([102.37654247,  97.634854  ,  87.58354894,  62.42799136,
        27.36151993,  10.72835311,   3.75400887,   0.15012275,
        -2.0169414 ])

所以有一些值超过 100在 0 以下,我设置的域中有什么不好。也许有一些方法可以通过使用其他参数来拟合域,或者也许我只是过度拟合?

编辑2

找到了最适合的值

x0 = np.array([200, 250, 260, 275, 290, 300, 350])
y0 = np.array([100, 97, 90, 50, 10, 3, 0])

,这些值略有变化,但arccot非常僵硬,而且对对称性非常敏感,因此需要做出一些妥协。

检查x0设置的域之外的一些值仍然存在offs:

fun(220, *p)
Out[448]: 99.71484816434646

fun(210, *p)
Out[449]: 100.25808335333575

但它可以满足一些ifs。

My data is quite similar to what the arccos(x) looks like so I think I'd succeed to make it. Using code:

from scipy.optimize import curve_fit
        
x0 = [240, 320] 
y0 = [100, 99, 90, 60, 30, 10, 1, 0, 0] 

x = np.linspace(x0[0], x0[1], int((x0[1]-x0[0])/10+1))

def fun(x, a, b, c):
    return np.pi/2 - c*np.arctan(x*a + b)
        
p, c = curve_fit(f=fun, xdata=x, ydata=y0)
plt.plot(x, fun(x, *p))

I get almost straight line - not arccot shape and y domain is between 3.6 and 4.6, not in data range (0, 100).
I guess I'm missing something, but setting bounds didn't help. Maybe some additional parameters in the function should be populated or my own are wrong? Please give me some hint.

EDIT

Found an error:

def fun(x, a, b, c, d):
    return (np.pi/2 - np.arctan(x*a + c)) * b + d

The function has been detached and new parameters added as well.

Btw, I've fount those points of data fit make the arccot not behaving like typical function of this type, because the new y values are

array([102.37654247,  97.634854  ,  87.58354894,  62.42799136,
        27.36151993,  10.72835311,   3.75400887,   0.15012275,
        -2.0169414 ])

so there are some values over 100 and under 0, what is not good in domain I've set. Maybe there's some method to fit domain by using some other parameter or maybe I just overfitting?

EDIT 2

Found the best fit with values

x0 = np.array([200, 250, 260, 275, 290, 300, 350])
y0 = np.array([100, 97, 90, 50, 10, 3, 0])

which are slightly changed but the arccot is very stiff and its so symmetry sensitive, so there's a need to have some compromise.

Checking some values outside domain set by x0 there are still offs:

fun(220, *p)
Out[448]: 99.71484816434646

fun(210, *p)
Out[449]: 100.25808335333575

but it's to be satisfied with some ifs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2025-01-17 17:22:09

答案是:

def fun(x, a, b, c, d):
    return (np.pi/2 - np.arctan(x*a + c)) * b + d

函数构建错误。具有 4 个参数的函数过度拟合的问题是,该函数只有一个形状变化条目,导致我永远无法将其拟合到所有点,即使它们似乎位于“目标”上。可以删除 cd 并获得更好的配合。或者例如将函数更改为多项式。

The answer is:

def fun(x, a, b, c, d):
    return (np.pi/2 - np.arctan(x*a + c)) * b + d

The function was wrongly built. The issues with overfitting such function with 4 parameters which has only one entry for shape change cause that I never fit it to all points, even they seems to be on the "target". It's possible to remove c and d and get better fit. Or just change function to polynomial for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文