2

I need to be able to extrapolate, using 2-n data points, a curved trend line which can then be 'queried'. In my head it would look a bit like this (the blue line):

Example graph

This is for a 'calories calculator' - I have data points regarding the amount of calories burned for an activity based on a certain weight: e.g. if you're 65kg, you'll burn around 420 calories, if you're 70kg, you'll burn 480 calories, if you're 75kg, you'll burn 550 calories, etc. etc. One axis would be for calories, the other for weight.

Obviously if I wanted to find out the amount of calories burned for a weight where I don't have a data point, I would need a trend line to 'query', which brings me on to the second part of my question: how would I go about doing this?

In summary:

  • How do I extrapolate a trend line in Python?
  • How do I 'query' this trend line to get estimates based on a point on this trend line?
Community
  • 1
  • 1
johneth
  • 2,858
  • 5
  • 27
  • 26
  • 2
    possible duplicate of [Is there easy way in python to extrapolate data points to the future?](http://stackoverflow.com/questions/1599754/is-there-easy-way-in-python-to-extrapolate-data-points-to-the-future) – Eli Bendersky Sep 04 '11 at 12:55
  • 1
    You should probably read this: http://en.wikipedia.org/wiki/Scatter_plot which includes a discussion on how to fit a best fit line. – Richard H Sep 04 '11 at 12:56

1 Answers1

4

numpy and scipy contain routines that let you fit expressions to data points. once you have an expression you can plot it for any range of time you like.

this answer - Nonlinear e^(-x) regression using scipy, python, numpy - contains an example of non-linear regression (it's an exponential, like in your question, but one with negative exponent - in general, fitting and extrapolating exponentials with positive exponents is a bad idea because the extrapolation is so sensitive to noise / uncertainty that it quickly becomes meaningless).

Community
  • 1
  • 1
andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • I ended up using numpy's `polyfit` and `poly1d` functions for what I needed. After some preliminary tests, it seems to work fairly accurately. – johneth Sep 05 '11 at 10:30
  • cool - try to keep the order of the polynomial as low as possible. that will help you extrapolate to larger times without veering off. – andrew cooke Sep 05 '11 at 10:49