1

I have this dataframe:

enter image description here

where:

x: independent variable
y: independent variable
z: dependent variable

How do I find in Python the optimal "a" and "b" parameters , so that I optimize this function:

z = a*sin(x) + b*sin(y)

I know that the optimal solution is:

a = 0
b = 1

But what Python code should I use? I guess I should use scipy optimize, but I am only able to use it with one independent variable.

1
  • Could you share your dataframe? (e.g. by providing the output of df.to_records()) Commented Dec 10, 2021 at 12:32

1 Answer 1

2

You might want to consider sklearn:

Assuming your dataframe is called df, first thing you might want to do is create a variable X containing the sin-transform of x,y, e.g.

from math import sin


X = [[sin(x), sin(y)] for x, y in zip(df.x, df.y)]

and then a variable Z which is df.z.

Finally, you can just do a linear regression fit usingsklearn, e.g.

from sklearn.linear_model import LinearRegression

reg = LinearRegression().fit(X, Z)
Sign up to request clarification or add additional context in comments.

1 Comment

aahh that's the bit I was missing ! thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.