无法估计参数的Scipy协方差。为类似的数据工作为什么不这个数据。

时间:2018-03-07 23:46:07

标签: python numpy scipy curve-fitting

我正在尝试在“time”和“volts”np数组中创建适合数据的曲线。当我尝试拟合数据时,我得到错误“无法估计参数的协方差”。如果我输入其他数据点虽然有效。我不确定这里会出现什么问题。

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


time = np.array([100,80,70,60,55,50,45,40,35,30,25,22,20])
volts = np.array([6.28,6.04,5.72,5.32,5.08,4.80,4.44,3.96,3.40, 
                  2.80,2.01,1.40,1.01])

## Defining exp func for curve fit
def func(x, a ,b):
    return a * (1 - 2*np.exp(-b * x))

params, extras = curve_fit(func, time, volts)
print(params)

plt.scatter(time, volts)
x_data = np.arange(5,110)
plt.plot(x_data, params[0]*(1-2*np.exp(-params[1]*x_data)))
plt.show()

enter image description here

enter image description here

例如,如果我使用数据:

volts = np.array([2.56, 2.54, 2.52, 2.46, 2.4, 2.38, 2.30, 2.26, 2.18, 2.08, 1.95, 1.80, 1.68, 1.44, 1.22, .9, .46])

time = np.array([])

for i in range(100, 15, -5):
    time = np.append(time, i)

然后合身效果很好。

1 个答案:

答案 0 :(得分:1)

您需要为abcurve_fit()提供真实的起始值。对于所有参数,默认值为(完全愚蠢)使用值1.0,没有警告。对于您的数据,这是一个非常糟糕的猜测,并将导致适合失败。尝试更好的起点,如

 params, extras = curve_fit(func, time, volts, [5.0, 0.1])

会更好......