1

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.

Graph of two curves.

2 Answers 2

5

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...

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.