Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python interpolation

I have a set of data that looks like:

Table-1 
    X1    | Y1
    ------+--------
    0.1   | 0.52147
    0.02  | 0.8879 
    0.08  | 0.901
    0.11  | 1.55 
    0.15  | 1.82
    0.152 | 1.95

Table-2
    X2   | Y2
    -----+------
    0.2  | 0.11
    0.21 | 0.112
    0.34 | 0.120  
    0.33 | 1.121       

I have to interpolate the Y2 value from Table-2 for the X1 values from Table-1, i.e., I need to find the values of Y2 for the following values of X:

    X1     |  Y2
    -------+-------
    0.1    |
    0.02   |
    0.08   |
    0.11   |
    0.15   |
    0.152  |

Note: Both Table-1 & 2 have unequal intervals. The number of (X, Y) entries will differ, for e.g., here we have 6 (X1, Y1) entries in Table-1 and only 4 (X2, Y2) in Table-2.

Which interpolation algorithm should I use in Numpy, and how do I proceed?


1 Answers

numpy.interp seems to be the function you want: pass your X1 as the first argument x, your X2 as the second argument xp, your Y2 as the third argument fp, and you'll get the Y values corresponding to the X1 coordinates.

Y2_at_X1 = np.interp(X1, X2, Y2)

I'm assuming you want to completely ignore the existing Y1 values. This is what the above snippet does. Otherwise you'll have to clarify your question to explain what role you might have for Y1!

If you want more than linear interpolation, I suggest you look at scipy.interpolate and its tutorial rather than trying to stretch numpy beyond its simplicity ;-).

like image 132
Alex Martelli Avatar answered Sep 12 '25 13:09

Alex Martelli