简体   繁体   English

python中pandas数据的指数曲线拟合

[英]Exponential curve fitting of pandas data in python

I'm trying to fit an exponential curve to some data represented by a pandas dataframe.我正在尝试将指数曲线拟合到由 Pandas 数据框表示的某些数据。 The data looks like this:数据如下所示:

在此处输入图片说明

The code I've used for curve fitting:我用于曲线拟合的代码:

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

t = df['time'].values
ym = df['value'].values

def func(t, c0, c1, c2, c3):
    return c0 + c1*t - c2*np.exp(-c3*t)

p0 = [6e6, 0.01, 100, 0.01]
c, cov = curve_fit(func, t, ym, p0)

print(c) # Output: [-5.46019366e+06  3.19567938e+03  1.00000000e+08  1.00000000e+06]

yp = func(t, c[0], c[1], c[2], c[3])

plt.figure()
plt.plot(t/60, ym)
plt.plot(t/60, yp)

However, the fitted curve always seem to be linear like this:然而,拟合曲线似乎总是像这样的线性:

在此处输入图片说明

I have tried different methods I've found online and always get the same linear result.我尝试了在网上找到的不同方法,并且总是得到相同的线性结果。 My dataframe look like this, were Cycle_id corresponds to "time", and peak correspond to "value":我的数据框看起来像这样, Cycle_id 对应于“时间”,峰值对应于“值”:

在此处输入图片说明

Any suggestion on how to fit this data is much appreciated, since I can't seem to find any errors in my code upon reviewing it, thus not getting any further..任何关于如何拟合这些数据的建议都非常感谢,因为我在查看代码时似乎无法在我的代码中找到任何错误,因此没有进一步......

Sorry for the poor answer.抱歉回答不好。 I have not enough knowledge about Python in practical use.在实际使用中,我对 Python 的了解不够。 Moreover it is not possible to get sufficiently correct data from a picture.此外,不可能从图片中获得足够正确的数据。 A scanning provided data which was used in the below calculus but the results are probably not accurate.扫描提供的数据用于以下微积分,但结果可能不准确。

I guess that the difficulty that you faced comes from the method of calculus which is iterative starting from "guessed" values of the parameters.我猜您遇到的困难来自微积分方法,该方法从参数的“猜测”值开始迭代。

If we use a non-iterative method which doesn't need initial guessed values the calculus is generally more robust.如果我们使用不需要初始猜测值的非迭代方法,则微积分通常会更稳健。 Such a method is explain in this paper : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales本文解释了这种方法: https : //fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

They are a lot of numerical examples in the paper but unfortunately your function is not treated in full details.它们是论文中的大量数值示例,但不幸的是,您的函数没有得到完整的细节处理。 It is not difficult to adapt the method to this case : See below.使该方法适应这种情况并不困难:见下文。 The result is :结果是:

在此处输入图片说明

Possibly you can use the above values of p,a,b,c as initial values of parameters in a more classical method.也许您可以在更经典的方法中使用上述 p,a,b,c 值作为参数的初始值。

在此处输入图片说明

FOR INFOMATION :信息:

The method of non-iterative regression uses a convenient integral equation which transforms the non-linear regression to a linear regression.非迭代回归方法使用方便的积分方程将非线性回归转换为线性回归。 In the present case the integral equation is :在本例中,积分方程为:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM