python拟合sir模型_如何在python中实现自动模型确定和两种状态模型拟合?

Currently, I've been doing model fitting in Prism manually for all my data. It's quite tedious and time consuming. I wonder if there is any way to improve the efficiency in data analysis. I'm familiar with Python so I'd like to start with python to think of a better workflow. Your help is much appreciated.

Two questions:

How to do two state model fitting in python (figure 1)? In my case, it will has an initial linear increase followed by horizontal plateau state. I wish to the way of implementation in python and the way to automatically detect the turning point where the changes occurs (Ideally I can get the time where it occurs and the slope as well)

Another case is when the state become exponentially or polynomial. Is there any way in python to automatically tell which model is the best.

解决方案

Scipy provides a least square curve fit method that supports custom defined functions. Here is an example for the first model:

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()

More information about scipy.optimize.curve_fit can be found in the reference guide. For polynomials, numpy provides standard functions with numpy.polyfit and numpy.poly1d, but you still have to provide the expected degree.

The sum of squared residuals can be used to compare the accuracy of different fit functions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值