简体   繁体   English

指数衰减曲线适合 Python。 错误:优化警告

[英]Exponential Decay Curve fit on Python. Error: Optimizewarning

I am trying to make a curvefit for my data.我正在尝试对我的数据进行曲线拟合。 I am using the exponential function as I need tau (time constant) for further analysis.我正在使用指数函数,因为我需要 tau(时间常数)进行进一步分析。 I am new to Python and trying the scipy curvefit function for the first time.我是 Python 新手,第一次尝试使用 scipy curvefit 函数。 However, I just obtain a straight line and the code gives OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn但是,我只是获得了一条直线,并且代码给出了OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn

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


x = np. array( [ 246, 248, 250, 252, 254,256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282,284])
y = np.array( [ 6.38954156, 6.32462716 ,6.21843495,5.96263661, 5.66970206, 5.37948873, 5.06358679 ,4.83386528 ,4.64262524, 4.5091623,  4.38658648, 4.46124359, 4.52878251, 4.56084217 ,4.47660524,4.5323503,  4.46626654, 4.58289275, 4.42962004, 4.29622583])

def curvefit( x, a, invtau,c):
      return a * np.exp(-x*invtau)+c
popt, pcov = curve_fit( curvefit, x, y)   #Curvefit
a, invtau, c = popt         #summarize the parameter values
x_new = np.arange(min(x), max(x),1)
y_new = curvefit (x_new, a, invtau, c)
plt.figure()
plt.plot(x, y, label='data')
plt.plot (x_new, y_new, '--', label='fit')
plt.legend (loc='upper right')

I saw that the output of y_new is same for all the rows because of which it gives a straight line.我看到y_new的输出对于所有行都是相同的,因此它给出了一条直线。

Question: How to estimate the parameters to make a curvefit for my data?问题:如何估计参数以对我的数据进行曲线拟合?

This is the output I get这是我得到的输出

Set some good starting values for your fit parameters.为您的拟合参数设置一些好的起始值。 The default will be 1 for all parameters ( a , invtau and c ), but especially a and invtau will be far from 1. Then, the optimizer algorithm will fail to find a good minimum.所有参数( ainvtauc )的默认值都是 1,但特别是ainvtau将远离 1。然后,优化器算法将无法找到一个好的最小值。

I tried with我试过

popt, pcov = curve_fit( curvefit, x, y, p0=[1, 1/250, 4])   #Curvefit

which yields a decent fit, and values for a , invtau and c of 4335942530.560109 , 0.0865584128551326 and 4.238729282912057 , respectively.这产生了合适的拟合, ainvtauc的值分别为4335942530.5601090.08655841285513264.238729282912057

(Note that I had the starting value for a as 1 , which is still far away from its actual value. But having invtau closer to its actual value, lets the algorithm then also find a good fit for a . It is overall still better to provide more accurate starting values for all parameters, and since you're modeling your data, you probably know what to expect for those values, and use that as a starting point.) (请注意,我有起始值a1 ,这仍然是远离它的实际价值。但有invtau接近其实际价值,让那么算法也找到了非常适合于a ,这是总体还是比较好为所有参数提供更准确的起始值,并且由于您正在对数据进行建模,因此您可能知道这些值的期望值,并将其用作起点。)

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

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