1
$\begingroup$

I am a new user of Wolfram Mathematica. I need your help.

Let's say I have three variables: x, y, z. I want to calculate the "z" depending on x, and y. In other words, I want to find a function for z like z=f(x,y).

I have this data

y = {140, 360, 500, 740, 800};

x = {4, 10, 35, 70, 90};

z = {{97.438, 103.891, 110.344, 118.7545, 124.13475},
    {110.344, 116.797, 123.25, 127.165, 129.515},
    {118.7545, 122.95975, 127.165, 131.08, 131.865},
    {121.444625, 124.8923125, 128.34, 131.4725, 132.2575},
    {124.13475, 126.824875, 129.515, 131.865, 132.65}
    };

I have found this thread enter link description here

based on which I have

  1. Created the plots of the data ListPlot3D[vals, Mesh -> All]

enter image description here 2. Created the predict and prediction is by "LinearRegression"

trainingSet = 
  Flatten[Table[
    Rule[{s[[i]], t[[j]]}, data[[i, j]]], {i, Length@s}, {j, 
     Length@t}], 1];
pf = Predict[trainingSet, Method -> "LinearRegression"];
Show[Plot3D[pf[{x, y}], {x, Min@s, Max@s}, {y, Min@t, Max@t}], 
 ListPointPlot3D[vals, PlotStyle -> {PointSize -> Large}]]
[![enter image description here][3]][3]

LinearRegression results.

enter image description here

it doesn't represent all the points that I need.

  1. By "PredictorInformation[pf, "Function"]" I get the function of my data. The function is = "109.712 + 0.115408 #1 + 0.0168067 #2 &"

However, the prediction is linear which doesn't fully give me what I want to see. There is another prediction method which is "GaussianProcess" and it gives me what I want it to see.

enter image description here

but when I try to get the function of it by "PredictorInformation[pf, "Function"]"

there is an error "PredictorInformation::elmntavs: "Function" is not an available property. Did you mean "FunctionMemory" instead?"

So my question is: Is there a way of getting the function of GaussianProcess prediction?

Or if there is not a way for that. How can I get a function of z = f(x,y) that would represent my data properly?

$\endgroup$
4
  • $\begingroup$ If I'm not mistaken, the full functional expression of a fitted Gaussian process would be a pretty big and ugly beast that basically involves all data points that were used to make the fit as well as a matrix inversion. A Gaussian process is more like an interpolation function than a linear model in that sense. That's probably the reason they don't give you the function explicitly. $\endgroup$ Commented Feb 16, 2019 at 12:06
  • $\begingroup$ Thank you for your comment. Is there another way to get a function of z = f(x,y) that would represent my data properly? $\endgroup$ Commented Feb 16, 2019 at 12:17
  • $\begingroup$ If you just want an analytical formula, you should probably use Fit or NonlinearModelFit (a 2nd degree model will probably do). Predict is a machine learning method that is geared towards prediction of unseen values and not so much towards fitting a model you can easily interpret. $\endgroup$ Commented Feb 16, 2019 at 12:40
  • $\begingroup$ Is there a guide I can use or some examples of this kind of data with z = f(x,y)? $\endgroup$ Commented Feb 16, 2019 at 13:00

1 Answer 1

2
$\begingroup$

Here's how you can fit a 2D polynomial to your data:

y = {140, 360, 500, 740, 800};

x = {4, 10, 35, 70, 90};

z = {{97.438, 103.891, 110.344, 118.7545, 124.13475}, {110.344, 
    116.797, 123.25, 127.165, 129.515}, {118.7545, 122.95975, 127.165,
     131.08, 131.865}, {121.444625, 124.8923125, 128.34, 131.4725, 
    132.2575}, {124.13475, 126.824875, 129.515, 131.865, 132.65}};
fitdata = Catenate @ Table[{x[[i]], y[[j]], z[[i, j]]}, {i, 1, Length[x]}, {j, 1, Length[y]}];
fit = Fit[fitdata, {1, x1, x2, x1 x2, x1^2, x2^2}, {x1, x2}]
Show[
 Plot3D[fit, {x1, Min[x], Max[x]}, {x2, Min[y], Max[y]}, PlotStyle -> Blue],
 ListPlot3D[fitdata]
]

Out[21]= 94.7557 + 0.645641 x1 - 0.00383606 x1^2 + 0.0364044 x2 - 0.000257039 x1 x2 - 2.21253*10^-6 x2^2

enter image description here

$\endgroup$
4
  • $\begingroup$ Hi, thank you so much it worked for me. I changed the fit data equation to (1, x1, x2, x1^2, x1 x2, x2^2, x1^3, x1^2 x2, x1 x2^2, x2^3) and got good results. Also as you suggested I tried to use "NonlinearModelFit" which gave me the "BestFitParameters" but they don't seem to be the best fit parameters. Really thank you so much I appreciate your help. If didn't publish this example I would struggle all day long. $\endgroup$ Commented Feb 17, 2019 at 14:39
  • $\begingroup$ With NonlinearModelFit, you pretty much always need to specify good initial guesses for your parameters. That's just the nature of fitting nonlinear models: there's no algorithm that guarantees a good result. Linear fits (i.e., linear in the basis functions) are much easier in that respect. $\endgroup$ Commented Feb 17, 2019 at 18:45
  • $\begingroup$ I have also found the "FindFormula". Right now don't get how it works but I am trying to find out. $\endgroup$ Commented Feb 18, 2019 at 15:22
  • $\begingroup$ FindFormula does not work for 2D data AFAIK. $\endgroup$ Commented Feb 18, 2019 at 15:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.