Protoss Reed
Protoss Reed

Reputation: 265

Python interpolation data list of lists

I have got this data:

X = [[10, 6, 0], [8, 6, 0], [4, 3, 0]]
Y = [[29, 28, 27], [26, 25, 24], [23, 22, 21]]

I need to interpolate between values in X. for instance:

D = np.linspace(10,0,num = 6)

Out:[  0.   2.   4.   6.   8.  10.]

So result should be like:

 "D"     "Y[0]"      "Y[1]"   "Y[2]"
  0        27          24        21
  2        ?           ?         ?
  4        ?           ?         23
  6        28          25        ?
  8        ?           26        ?
  10       29          ?         ?

I know there is np.interp() I tried and it works only for one dimensional list:

z =[0,5,10]
v= [29,28,27]
x = np.linspace(10,0,num = 4)
d=np.interp(x, z, v)
print (d)

But If I have list of lists it doen't work.

Upvotes: 2

Views: 1814

Answers (1)

John Coleman
John Coleman

Reputation: 52008

Use zip. Also, it looks like you want to reverse the sublists. Maybe something like:

points = np.linspace(0,10,num = 6)

cols = (points,) + tuple(np.interp(points,x[::-1],y[::-1]) for x,y in zip(X,Y))

np.stack(cols,axis=1)

which has output:

array([[  0.        ,  27.        ,  24.        ,  21.        ],
       [  2.        ,  27.33333333,  24.33333333,  21.66666667],
       [  4.        ,  27.66666667,  24.66666667,  23.        ],
       [  6.        ,  28.        ,  25.        ,  23.        ],
       [  8.        ,  28.5       ,  26.        ,  23.        ],
       [ 10.        ,  29.        ,  26.        ,  23.        ]])

This shows interpolation. It seems like you might want to use extrapolation in some entries in some columns.

Upvotes: 2

Related Questions