Reputation: 951
I have some time series data which I am trying to fit to a curve using the polyfit
function in Numpy. I have converted the datetime x-values to numbers using the date2num
function and have graphed the original data, which is approximately 600 data points (01-01-2014 - 10-08-2015).
I am trying to derive an expression for the curve so that I may approximately forecast future data points, eg over the next 30 days beyond my graphed data. However, my polynomial expression, when graphed at any order, is way off. I am sure I am doing something glaringly wrong but cannot seem to crack it.
x = df["dates"]
y= df["brand"]
poly = numpy.polyfit(x, y, 5)
polynomial = numpy.poly1d(poly)
xs = numpy.linspace(x[0], x[-1]+60, len(x)+60)
y_int = polynomial(xs)
plt.plot(x, y)
plt.plot(xs, y_int)
plt.show()
The graph below shows the original curve in blue.
Upvotes: 1
Views: 2354
Reputation: 6398
Try using a degree 1 function over the interval 75 on. Really using a polynomial fit not what you want for this type of function. This looks most like 2 line segments joined together, so a linear fit over some of the points will give you a pretty good fit.
Upvotes: 2
Reputation: 1180
I cant comment because I lack reputation. If an admin moves this to comments I would be glad. I believe the problem is that your data does not look polynomial at all. You get a plateu in the end which cant be achieved by polynomials.
Maybe try another function. Without any knowledge about the origin of the data it is hard to tell what kind of function is usefull...
Upvotes: 5