如何在python中实现自动模型确定和两状态模型拟合?
目前,我一直在为所有数据在Prism中手动进行模型拟合。这是相当乏味和耗时的。我想知道是否有办法提高数据分析的效率。我很熟悉Python,所以我想从Python开始考虑一个更好的工作流。非常感谢你的帮助 两个问题:如何在python中实现自动模型确定和两状态模型拟合?,python,scipy,modeling,model-fitting,Python,Scipy,Modeling,Model Fitting,目前,我一直在为所有数据在Prism中手动进行模型拟合。这是相当乏味和耗时的。我想知道是否有办法提高数据分析的效率。我很熟悉Python,所以我想从Python开始考虑一个更好的工作流。非常感谢你的帮助 两个问题: 如何在python中进行双状态模型拟合(图1)?在我的例子中,它将有一个初始线性增加,然后是水平平台状态。我希望了解python的实现方式,以及自动检测变化发生的转折点的方式(理想情况下,我可以得到变化发生的时间和斜率) 另一种情况是当状态变为指数或多项式时。python中是否有任何
Scipy提供了支持自定义函数的最小二乘曲线拟合方法。以下是第一个模型的示例:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
#custom fit function - first slope steeper than second slope
def two_lin(x, m1, n1, m2, n2):
return np.min([m1 * x + n1, m2 * x + n2], axis = 0)
#x/y data points
x = np.asarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.asarray([2, 4, 8, 12, 14, 18, 20, 21, 22, 23, 24])
#initial guess for a steep rising and plateau phase
start_values = [3, 0, 0, 3]
#curve fitting
fit_param, pcov = curve_fit(two_lin, x, y, p0 = start_values)
#output of slope/intercept for both parts
m1, n1, m2, n2 = fit_param
print(m1, n1, m2, n2)
#calculating sum of squared residuals as parameter for fit quality
r = y - two_lin(x, *fit_param)
print(np.sum(np.square(r)))
#point, where the parts intersect
if m1 != m2:
x_intersect = (n2 - n1) / (m1 - m2)
print(x_intersect)
else:
print("did not find two linear components")
#plot data and fit function
x_fit = np.linspace(-1, 11, 100)
plt.plot(x, y, 'o', label='data')
plt.plot(x_fit, two_lin(x_fit, *fit_param), '--', label='fit')
plt.axis([-2, 12, 0, 30])
plt.legend()
plt.show()
关于多项式的更多信息,numpy提供了带有和的标准函数,但您仍然需要提供期望的阶数
残差平方和可用于比较不同拟合函数的精度 注意自动选择模型。通常最复杂的模型/函数最适合数据。嗨,Piinthesky,这是一个很棒的实现。是否有方法返回坡度发生变化的x坐标?添加代码以明确如何提取每个线段的坡度/截距。请把kazemakase的评论写在黑板上。我怎样才能把kazemakase的评论添加到黑板上?当你判断合适的数据时,不要忽视它,想想它。如果你用于判断的数字很好地代表了现实,那么一定要重新评估。看看原始数据-原因,为什么我添加了一个绘图。