How to get linear value from Dataframe

Question:

I have simple question.

i want to get linear value with dataframe.(like N-D Lookuptable in Matlab simulink)

type python
import numpy as np
import scipy as sp
import pandas as pd

X = np.array([100,200,300,400,500])
Y = np.array([1,2,3])
W = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3]])

df = pd.DataFrame(W,index=Y,columns=X)
#   100  200  300  400  500
#1    1    1    1    1    1
#2    2    2    2    2    2
#3    3    3    3    3    3

#want function.
#Ex
#input x = 150 y= 1 
#result = 1

#input x = 100 y = 1.5
#result = 1.5

somebody let me know, if there is a function or a lib, or how can I do this, or Some beetter way.

I just want to make a filter or getting function with some array data, using Numpy, Pandas or Scipy, if possible.

Asked By: G Oh

||

Answers:

You are looking for scipy.interpolate.RectBivariateSpline:

#lib
import numpy as np
from scipy import interpolate

#input
X = np.array([100,200,300,400,500])
Y = np.array([1,2,3])
W = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3]])

#solution
spline = interpolate.RectBivariateSpline(X, Y, W.T, kx=1, ky=1)

#example
spline(150, 1) #returns 1
spline(100, 1.5) #returns 1.5

Let me know if it solves the issue.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.