public static void PredictIssue() { // <SnippetLoadModel> ITransformer loadedModel = _mlContext.Model.Load(_modelPath, out var modelInputSchema); // </SnippetLoadModel> // <SnippetAddTestIssue> Issues singleIssue = new Issues() { Title = "Entity Framework crashes", Description = "When connecting to the database, EF is crashing" }; // </SnippetAddTestIssue> //Predict label for single hard-coded issue // <SnippetCreatePredictionEngine> _predEngine = _mlContext.Model.CreatePredictionEngine <Issues, IssuePrediction>(loadedModel); // </SnippetCreatePredictionEngine> // <SnippetPredictIssue> var prediction = _predEngine.Predict(singleIssue); // </SnippetPredictIssue> // <SnippetDisplayResults> Console.WriteLine($"=============== Single Prediction - Result: {prediction.Area} ==============="); // </SnippetDisplayResults> }
public static IEstimator <ITransformer> BuildAndTrainModel(IDataView trainingDataView, IEstimator <ITransformer> pipeline) { //The SdcaMaximumEntropy is your multiclass classification training algorithm. // This is appended to the pipeline and accepts the featurized Title and Description (Features) and the Label input parameters to learn from the historic data. var trainingPipeline = pipeline.Append(_mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy("Label", "Features")) .Append(_mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel")); Console.WriteLine($"=============== Training the model ==============="); _trainedModel = trainingPipeline.Fit(trainingDataView); Console.WriteLine($"=============== Finished Training the model Ending time: {DateTime.Now.ToString()} ==============="); // (OPTIONAL) Try/test a single prediction with the "just-trained model" (Before saving the model) Console.WriteLine($"=============== Single Prediction just-trained-model ==============="); // Create prediction engine related to the loaded trained model // <SnippetCreatePredictionEngine1> _predEngine = _mlContext.Model.CreatePredictionEngine <Issues, IssuePrediction>(_trainedModel); // </SnippetCreatePredictionEngine1> // <SnippetCreateTestIssue1> Issues issue = new Issues() { Title = "WebSockets communication is slow in my machine", Description = "The WebSockets communication used under the covers by SignalR looks like is going slow in my development machine.." }; // </SnippetCreateTestIssue1> // <SnippetPredict> var prediction = _predEngine.Predict(issue); // </SnippetPredict> // <SnippetOutputPrediction> Console.WriteLine($"=============== Single Prediction just-trained-model - Result: {prediction.Area} ==============="); // </SnippetOutputPrediction> // <SnippetReturnModel> return(trainingPipeline); // </SnippetReturnModel> }