Home > Net >  How to optimize multivariate functions in Python
How to optimize multivariate functions in Python

Time:12-10

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.

CodePudding user response:

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)
  • Related