Search code examples
pythonnumpyscipycurve-fittingcovariance

Why does `scipy.optimize.curve_fit` with only two points raise OptimizeWarning: Covariance of the parameters could not be estimated?


Why doesn't this simple fit work?

import numpy as np
from scipy.optimize import curve_fit
exponential = lambda x, a, b: np.exp(a * x + b)
popt, pcov = curve_fit(f=exponential, xdata=[350, 380], ydata=[48, 17], p0=[-0.01, 1])
print("a =", popt[0])
print("b =", popt[1])

The output is:

python-3.4.4\lib\site-packages\scipy\optimize\minpack.py:715:
OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)

a = -0.03459958889505575
b = 15.981057124177402

Solution

  • It works but in calculating the pcov:

    If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf.

    Which is your case and hence you see the warning message.