3

I am trying to write a code which learns a data set. Now, I want to know the input value for a given output value. Consider, the following code

trainset = {{1,2}, {2,4}, {3,6}}
fn = Predict[trainset]

Now I want to solve for m

Solve[fn[m] == 5, m]

Is there any way to find value of m, other than plotting the function looking for the input value which gives 5.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Anjan Kumar
  • 4,979
  • 1
  • 15
  • 28

1 Answers1

4
trainset = Rule@@@{{1,2}, {2,4}, {3,6}}
fn = Predict[trainset];

First get the predictor function using PredictorInformation:

PredictorInformation[fn,"Function"]
(* 0.0000199999 + 1.99999 #1 & *)

Then invert it using InverseFunction:

fnInv=InverseFunction[PredictorInformation[fn,"Function"]]
(* 0.500003 (-0.0000199999 + 1. #1) & *)

fn[3]
(* 5.99999 *)
fnInv[%]
(* 3. *)

Note: Unfortunately, this approach works only when Method is "LinearRegression"; it does not work for Methods "RandomForest" and "NearestNeighbors" as they do not have the "Function" property.

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thanks kguler. This uses the Linear regression method. What if the method used is "NearestNeighbors", then can we find the inverse function ?? Consider the following code. In this "NearestNeighbors" method is used.

    x1 = Range[0, 0.1, .001]; x2 = x1^2; trainset = Rule @@@ Transpose[{x1, x2}]; fn= Predict[trainset]

    – Anjan Kumar Sep 13 '14 at 04:49
  • @Anjan, unfortunately this approach works only when Method is "LinearRegression"; it does not work for Methods "RandomForest" and "NearestNeighbors" as they do not have the "Function" property. – kglr Sep 13 '14 at 12:06