添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Scipy covariance of parameters could not be estimated. Working for similar data why not this data.

Ask Question

I am trying to create a curve fit for the data in "time" and "volts" np arrays. When I try and fit the data I get the error "covariance of parameters could not be estimated". If I put in other data points though it works. I am not sure what could be going wrong here.

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

For example if I use the data:

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)

Then the fit works great.

you need to give realistic starting values for a and b to curve_fit(). The default is (completely idiotic) to use values of 1.0 for all parameters, without warning. For your data, this is a pretty bad guess, and will cause the fit to fail. Trying better starting points like

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

will work much better....

@JamesPhillips you keep asking that question in SO comments, and the answer is always the same: I looked at a plot of the data and GUESSed. In this case I also plotted the initial model with curve_fit idiotic, cannot-ever-be-justified, and guaranteed-to-cause-problems of silently defaulting to values of 1 for all variables. For this data and model, it gives a fit so bad that it cannot recover. The error is entirely with curve_fit -- it should insist on explicit defaults. Curve fitting requires initial values -- guesses -- from the user. – M Newville Mar 8, 2018 at 12:26 @JamesPhillips That's fair, but I think you have asked this question many times in similar SO comments. I don't think I can really give a different answer. Do you have a concern with or insight into how initial guesses are made or may be automated? – M Newville Mar 8, 2018 at 15:01

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.