0
$\begingroup$

I have this simple code to do a regression on a list of points very close to a parabole. It is purely anecdotal that I happen to know the exact underlying trend, the idea is just to have simple/nice synthetic data. The eventual goal is to do regressions with data whose underlying trend is not known.

   Table[x -> {x, x^2 + RandomReal[{-0.9, 0.9}]}, {x, -5., 5., 0.5}];

net = NetChain[{20, Tanh, 2}];

trained = NetTrain[net, data, BatchSize -> 20]

Show[ListPlot[{Flatten[Table[trained@{x}, {x, -4., 4., 0.25}], 1], 
   data[[All, 2]]}], 
 Plot[x^2, {x, -4., 4.}, PlotStyle -> Darker[Green]]]

Is there a more compact / advisable way to do something equivalent using ports? I ask this because that seems the way to go to include weights (https://reference.wolframcloud.com/language/tutorial/NeuralNetworksExampleWeighting.html.en)

$\endgroup$
4
  • $\begingroup$ If you just want to do a simple (or even a complicated) regression, then just try data = Table[{x, x^2 + RandomReal[{-0.9, 0.9}]}, {x, -5., 5., 0.5}]; lmf = LinearModelFit[data, {x, x^2}, x]. That will give you the ability to look at goodness-of-fit statistics. $\endgroup$
    – JimB
    Commented Sep 5 at 16:56
  • $\begingroup$ Sure, colleagues and I have a lengthy MCMC of our own that would do this, the point is to explore the capabilities of machine learning for forecasts. I am using data for which I now the curve to which they fit just to see at a glance the goodness of fit. But I want to move forward to do regressions of which I do not know the underlying trend (cosmology stuff). I will edit the question thus. $\endgroup$ Commented Sep 5 at 17:01
  • $\begingroup$ Machine learning can certainly be useful but with a little thought might not be necessary or even appropriate for small amounts of data like your example. You might consider Mathematica's FindFormula function when you want to find a general fit that describes the data rather than explaining the data. $\endgroup$
    – JimB
    Commented Sep 5 at 17:09
  • $\begingroup$ The real peoblem has 2000 data with uncertainty. I could use my own mcmc.My question is concerned with mathematica syntax, not with best regresión approaches in general. I could also code a Gaussiana processes test. I will wsit the question again to make.it clearer. $\endgroup$ Commented Sep 5 at 17:42

1 Answer 1

3
$\begingroup$

This is an answer to my question:

data = Flatten@
   Table[x -> {x, x^2 + RandomReal[{-0.9, 0.9}]}, {x, -5., 5., 0.5}];

net = NetChain[{20, Tanh, 2}];

dataset = <|"Input" -> #[[All, 1]], "Output" -> #[[All, 2]]|> &;

trained = NetTrain[net, dataset[data], BatchSize -> 20]

Show[ListPlot[{Flatten[Table[trained@{x}, {x, -4., 4., 0.25}], 1], 
   data[[All, 2]]}], 
 Plot[x^2, {x, -4., 4.}, PlotStyle -> Darker[Green]]]
$\endgroup$

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.