Mathematica Asked on February 26, 2021
I have data structured like {{{i,j},f(i,j)},...}}}
. In my actual data the relationship between f(i,j)
and j,i
is unknown and the goal is to get an estimated f(i,j)
.
Example data:
list = Flatten[
Table[{{i + 0.1 Random[], j + 0.1 Random[]}, 10*i*j + Random[]}, {i,
0, 1, 0.1}, {j, 0, 1, 0.1}], 1]
How do I find a good estimation of f(i,j)
?
As a bonus, I would like to find an interpolating function that can predict f(i,j)
for intermediate values of i,j
not present in the data.
If you have a model for $f$ then you'd go down the fitting route with NonlinearModelFit
, or you could fit a plane with ResourceFunction["PlaneOfBestFit"]
. However, you could also use Predict
here as I will show below:
pf = Predict[Rule @@@ list, Method -> "NeuralNetwork"];
Show[
Plot3D[pf[{x, y}], {x, 0, 1}, {y, 0, 1}, PlotStyle -> Opacity[.25]],
ListPointPlot3D[Flatten /@ list]
]
Using Method->"GaussianProcess"
also produces a good fit. If you have a lot of data, it is important to avoid over-fitting. We can divide the data into a training set and a validation set in a 70% to 30% ratio by random sample, and we can use the validation data to ensure the predictor isn't over-fitting the data (see cross-validation).
list = Flatten[
Table[{{i + 0.1 Random[], j + 0.1 Random[]},
10*i*j + Random[]}, {i, 0, 1, 0.05}, {j, 0, 1, 0.05}], 1];
(* divide the data into 70% training and 30% cross-validation *)
{training, validation} =
TakeDrop[#, Round[Length[#]*0.7]] &@RandomSample[Rule @@@ list];
pf = Predict[training, Method -> "GaussianProcess",
ValidationSet -> validation];
Show[
Plot3D[pf[{x, y}], {x, 0, 1}, {y, 0, 1}, PlotStyle -> Opacity[.25]],
ListPointPlot3D[Flatten /@ list]
]
pm = PredictorMeasurements[pf, validation];
pm["RSquared"]
pm["ComparisonPlot"]
By using PredictorMeasurements
on the validation set, we can gauge how well the fit is generalizing to unseen data:
Correct answer by flinty on February 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP