title: ML.NET Create Prediction Engine using Dynamic Class
body: <p>I have an ML.NET application where I have to create interface IDataView dynamically after compile time to be used for training. I found this <a href="https://stackoverflow.com/a/55738567/1394922">thread</a> and I've been able to successfully create a dynamic interface for the training data set and then use it to train a model. My problem comes in when I try to use that same interface in order to create a prediction using that trained model. The <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.modeloperationscatalog.createpredictionengine?view=ml-dotnet" rel="nofollow noreferrer">docs show</a> that you should create a prediction engine where you have to define both the input and output class types in order to create the engine. Something like:</p>
  <pre><code>mlContext.Model.CreatePredictionEngine&lt;TSrc,TDst&gt;(ITransformer, DataViewSchema)
  </code></pre>
  <p>where <em><strong>TSrc</strong></em> and <em><strong>TDst</strong></em> are class types that are known at compile time. My problem is that I don't know the structure of the input class type at compile time and have to create a dynamic interface for the input data source. The output class object can be defined since the parameters are known, but I'm unsure how to proceed with a dynamic input.</p>
  <p>I thought I could maybe try to use something like <em>GetType()</em> on the interface but it says that &quot;implicitly-typed variables cannot have multiple declarators&quot;. My simplified example looks like this:</p>
  <pre><code>public class ModelOutput
  {
      public string PredictedLabel { get; set; }
      public float[] Score { get; set; }
  }
  
  public class MakePrediction
  {
      protected void Solve(IDataView data, ITransformer model)
      {
          var mlContext = new MLContext();
          var engine = mlContext.Model.CreatePredictionEngine&lt;data.GetType(), ModelOutput&gt;(model, data.Schema);
      }
  }
  </code></pre>