scipy.optimize.curve_fit doesn't fit
You are encountering an overflow, since your x
values are just too large to fit np.exp(x)
into a 64 bit floating point number, see np.exp(2015)
. One way to handle this, is to fit the function g(x) = func(x-2011) instead:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
x = np.array([2011, 2012, 2013, 2014, 2015])
y = np.array([255, 349, 449, 554, 658])
def func(x, a, b, c):
return a * np.exp(b * x + c)
def g(x, a, b, c):
return func(x-2011, a, b, c)
popt, pcov = curve_fit(g, x, y)
plt.plot(x, g(x, *popt))
plt.show()