# Multidimensional fit and interpolation for scattered unstructured data

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.

## 1 Answer

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: • You can use pf[{x,y}] to get intermediate values. Note you can change the Method to something else. If you use Method->"LinearRegression" you'll get a plane in the plot. Try Method->"GaussianProcess" - it produces a good fit too. The other methods are more jumpy. – flinty Sep 17 '20 at 13:58
• thanks a lot! One question: With Mathematica 12.0 I get following error message with my data:PredictorMeasurementsObject::mlnaseth: "RSquared" is not an available property. Did you mean "MeanSquare" instead? – Luke Sep 17 '20 at 18:41
• It's in 12.1 - I think it was a new feature. – flinty Sep 17 '20 at 20:01