0

I am really new in Python, hence I am asking a simple question:

I have a sets of data (x1, x2, x3, x4, x5) and corresponding (y1, y2, y3, y4, y5). Now, how can I use Python to find a y value for a given x value? (x lies in between x1 to x5)

As an example: Let say, I want to find a value of Y for X = 0.9 for the following sets of data.

X        Y
0.5     12
1.2     17 
1.3     23
1.6     29
2.1     33

Thanks in advance!!

  • 1
    This seems to be more of a statistics or mathematics question than a programming question. Once you figure out what technique to use, you can ask about how to apply that in python. – rje Sep 18 '14 at 14:51
  • 1
    Please post what you've tried to do so far – Jesuisme Sep 18 '14 at 14:52
  • You've tagged `numpy` and `scipy` so I assume you've used those in your attempts at solving this, if so could we see? – Joe Smart Sep 18 '14 at 14:55
  • This question IS NOT a duplicate. One thing is asking "how to fit data to THIS specific curve", and other is "how can I found a function that relates one data set to another"... that happens to be a curve fitting, but doesn't have to be done with Univariate Spline. – xbello Sep 26 '14 at 06:28

1 Answers1

1

You can use a polyfit.

from numpy import polyfit


print polyfit([0.5, 1.2, 1.3, 1.6, 2.1],
              [12, 17, 23, 29, 33],
              1)  # Replace this number for the degree of the polinomium

Output degree 1 -> [ 14.02332362   4.00874636]
Output degree 2 -> [  1.17847672  10.98436544   5.64150351]

What you obtain are the coeficients for the curves:

  • Grade 1: y = 14.02332362x + 4.00874636
  • Grade 2: y = 1.17847672x2 + 10.98436544x + 5.64150351
xbello
  • 7,223
  • 3
  • 28
  • 41