如何使用scipy和matplotlib来拟合反向sigmoid函数

2024-04-27 21:37:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据点,我可以成功地图表,但现在我想拟合曲线的数据。我查看了其他的stackoverflow答案,发现了一些问题,但我似乎无法实现它们。我知道这个函数是一个反乙状结肠。你知道吗

我想用希尔方程:https://imgur.com/rYqEASm

到目前为止,我试图使用curve_fit()包中的scipy函数来查找参数,但我的代码总是出错。你知道吗

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

x = np.array([1, 1.90, 7.70, 30.10, 120.40, 481.60, 1925.00, 7700.00])
y = np.array([4118.47, 4305.79, 4337.47, 4838.11, 2660.76, 1365.05, 79.21, -16.40])

def fit_hill(t,b,s,i,h):
    return b + ((t-b)/(1 + (((x * s)/i)**-h)))

plt.plot(x,y, 'o')
plt.xscale('log')
plt.show()

params = curve_fit(fit_hill, x, y)

[t,b,s,i,h] = params[0]

Tags: 数据函数importasnp图表pltscipy
1条回答
网友
1楼 · 发布于 2024-04-27 21:37:54

fit\u hill应该有6个参数。 (见https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

适合希尔(x,t,b,s,i,h)。 您应该尝试对参数进行初步猜测。你知道吗

例如,在您的模型中,当x=0时,值为t。因此您可以将x=0的值设置为t的估计值

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

x = np.array([1, 1.90, 7.70, 30.10, 120.40, 481.60, 1925.00])
y = np.array([4118.47, 4305.79, 4337.47, 4838.11, 2660.76, 1365.05, 79.21])

def fit_hill(x,t,b,s,i,h):
    return b + ((t-b)/(1 + (((x * s)/i)**-h)))

plt.plot(x,y, 'o')
popt,pcov = curve_fit(fit_hill, x, y,(4118,200,1,1900,-2))
plt.plot(x,fit_hill(x,*popt),'+')

plt.xscale('log')
plt.show()

你是否画出了你的模型来可视化它是否适合你的数据?你知道吗

仅在s/i中使用的s和i可以替换为模型中的一个变量。你知道吗

相关问题 更多 >