Home > Software engineering >  How to get linear value from Dataframe
How to get linear value from Dataframe

Time:02-02

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.

CodePudding user response:

You are looking for scipy.interpolate.RectBivariateSpline:

#lib
import numpy as np
import scipy as sp

#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.

  • Related