My dataframe looks like:
X Y Val
10 10 17
10 30 28
10 40 35
10 50 15
20 10 17
20 30 18
20 40 33
20 50 15
40 10 37
40 30 29
40 40 49
40 50 40
I want to extract a interpolated "Val"
for given "X"
and "Y"
even if they are not in the df.
For example: I can very easily get "Val"
for X=10
and Y=50
.
But, suppose I want X=15
and Y=15
which is not in dataframe? Is it possible to extract the same? Does Pandas have any such functionality?
I don't know about pandas, but you can do it through scipy.interpolate.interp2d
:
f = interp2d(x = df['X'], y = df['Y'], z = df['Val'])
Then you can check the value of the function in a given point (x, y)
:
>>> f(15, 15)
array([17.97919182])
As an entry point to machine learning, this could be a good way to experiment with a linear regression model:
import pandas as pd
from sklearn.linear_model import LinearRegression
# create a sample dataframe
data = {'X': [10, 20, 30, 40], 'Y': [10, 20, 30, 40], 'Val': [100, 200, 300, 400]}
df = pd.DataFrame.from_dict(data)
# define the target and input data for training
target = df.Val
df.drop(['Val'], axis=1, inplace=True)
input_df = df
# fit the linear regression model
regression_model = LinearRegression()
regression_model.fit(input, target)
# create another sample dataframe to test output
prediction_data = {'X': [50, 60, 70], 'Y': [50, 60, 70]}
prediction_df = pd.DataFrame.from_dict(prediction_data)
# make predictions
regression_model.predict(prediction_df)
Which gives the output:
array([500., 600., 700.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With