# Can I use machine learning to predict a functions behavior?

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?

• Yes, you can use the algorithm called “Linear Regression”. But, is this question about Mathematica? Apr 23 '19 at 20:41
• Is it machine learning or something more traditional? Apr 23 '19 at 21:41
• I tried this. trainingset = {1 -> Sin[x], 2 -> Sin[2 x], 3 -> Sin[3 x], 4 -> Sin[4 x]}; Apr 23 '19 at 21:52
• 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? Apr 23 '19 at 21:53

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] *)
`