2
$\begingroup$

I want to use this as training data

(1, sin(x)), (2, sin(2x)), (3, sin(3x)) 

and answer the question what is (20, ?).

I have tried using existing inbuilt machine learning functions to no avail. I tried this:

trainingset = {1 -> Sin[x], 2 -> Sin[2 x], 3 -> Sin[3 x], 4 -> Sin[4 x]};
p = Predict[trainingset]

Got an error message saying it cannot accept functions as inputs. So my question is what is the workaround for doing this?

$\endgroup$
  • $\begingroup$ Yes, you can use the algorithm called “Linear Regression”. But, is this question about Mathematica? $\endgroup$ – HirenPatel Apr 23 at 20:41
  • $\begingroup$ Is it machine learning or something more traditional? $\endgroup$ – Quasar Supernova Apr 23 at 21:41
  • $\begingroup$ I tried this. trainingset = {1 -> Sin[x], 2 -> Sin[2 x], 3 -> Sin[3 x], 4 -> Sin[4 x]}; $\endgroup$ – Quasar Supernova Apr 23 at 21:52
  • $\begingroup$ p = Predict[trainingset] . Got an error message saying it cannot accept functions as inputs. So my question is what is the workaround for doing this? $\endgroup$ – Quasar Supernova Apr 23 at 21:53
7
$\begingroup$

You are running into problems because you are confusing numeric and symbolic computations.

Predict generates regressions, i.e., it attempts to determine a numerical value. But Sin[2 x] does not have a numerical value unless x is defined. For example, if you change this to:

x = 2.;
train = # -> Sin[#*x] & /@ Range[10]

Then you can generate a model by:

model = Predict[train]

And generate your desired numerical prediction for the input value n = 20

model[20] (* 0.0646506 *)

However, you will find that this approximation is quite bad because (in Mathematica 11.3) the resulting PredictorFunction is a decision tree, which has difficulty extrapolating)

Sin[20*x] (*0.745113 *) `

You might be able to get better results by changing the Method option used for Predict. However, a better solution to your problem is to use FindFormula, which returns a numerical expression

FindFormula[train, n]
(* Sin[2. n] *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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