public static MushroomModelPrediction PredictSingleResult(MLContext mlContext, ITransformer model, MushroomModelInput input) { //Creating the prediction engine which takes data model input and output var predictEngine = mlContext.Model.CreatePredictionEngine <MushroomModelInput, MushroomModelPrediction>(model); var predOutput = predictEngine.Predict(input); return(predOutput); }
static void Main(string[] args) { //Creating MLContxet model to be shared accross model building, validation and prediction process MLContext mlContext = new MLContext(); //Loading the data from csv files //Splitting the dataset into train/test sets TrainTestData mushroomTrainTestData = LoadData(mlContext, testDataFraction: 0.25); //Creating data transformation pipeline which transforma the data a form acceptable by model //Returns an object of type IEstimator<ITransformer> var pipeline = ProcessData(mlContext); //passing the transformation pipeline and training dataset to crossvalidate and build the model //returns the model object of type ITransformer var trainedModel = BuildAndTrain(mlContext, pipeline, mushroomTrainTestData.TrainSet); //Sample datainput for predicrtion var mushroomInput1 = new MushroomModelInput { cap_shape = "x", cap_surface = "s", cap_color = "n", bruises = "t", odor = "p", gill_attachment = "f", gill_spacing = "c", gill_size = "n", gill_color = "k", stalk_shape = "e", stalk_root = "e", stalk_surface_above_ring = "s", stalk_surface_below_ring = "s", stalk_color_above_ring = "w", stalk_color_below_ring = "w", veil_type = "p", veil_color = "w", ring_number = "o", ring_type = "p", spore_print_color = "k", population = "s", habitat = "u" }; //Sample datainput for predicrtion var mushroomInput2 = new MushroomModelInput { cap_shape = "b", cap_surface = "y", cap_color = "y", bruises = "t", odor = "l", gill_attachment = "f", gill_spacing = "c", gill_size = "b", gill_color = "k", stalk_shape = "e", stalk_root = "c", stalk_surface_above_ring = "s", stalk_surface_below_ring = "s", stalk_color_above_ring = "w", stalk_color_below_ring = "w", veil_type = "p", veil_color = "w", ring_number = "o", ring_type = "p", spore_print_color = "n", population = "s", habitat = "m" }; //passing trained model and sample input data to make single prediction var result = PredictSingleResult(mlContext, trainedModel, mushroomInput2); Console.WriteLine("================================= Single Prediction Result ==============================="); // Evaluate(mlContext, pipeline, trainedModel, mushroomTrainTestData.TestSet); Console.WriteLine($"Predicted Result: {result.Label}"); Console.ReadKey(); }