如何在python中实现自动模型确定和两种状态模型拟合? [英] How to implement automatic model determination and two state model fitting in python?

查看:267
本文介绍了如何在python中实现自动模型确定和两种状态模型拟合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我一直在Prism中手动对所有数据进行模型拟合.这是非常繁琐且耗时的.我想知道是否有任何方法可以提高数据分析的效率.我对Python很熟悉,所以我想从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.

两个问题:

  1. 如何在python中拟合两个状态模型(图1)?在我的情况下,它将具有初始线性增加,然后是水平平稳状态.我希望采用python的实现方式以及自动检测发生变化的转折点的方式(理想情况下,我也可以获取发生变化的时间和斜率)

  1. 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)

另一种情况是状态变为指数或多项式. python中有什么方法可以自动判断哪种模型是最好的.

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

图1

推荐答案

Scipy提供了一种最小二乘曲线拟合方法,该方法支持自定义函数.这是第一个模型的示例:

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

有关的详细信息可以在参考指南中找到scipy.optimize.curve_fit.对于多项式,numpy提供了具有 numpy.poly1d ,但是您仍然必须提供期望的程度.

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.

这篇关于如何在python中实现自动模型确定和两种状态模型拟合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆