/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static async Task TestPrediction(string outputModelPath = "country_month_fastTreeTweedie.zip") { Console.WriteLine(" "); Console.WriteLine("*********************************"); Console.WriteLine("Testing Country Sales Forecast model"); // Read the model that has been previously saved by the method SaveModel var model = await PredictionModel.ReadAsync <CountryData, CountrySalesPrediction>(outputModelPath); Console.WriteLine("** Testing Country 1 **"); // Build sample data var dataSample = new CountryData() { country = "United Kingdom", month = 10, year = 2017, med = 309.945F, max = 587.902F, min = 135.640F, std = 1063.932092F, prev = 856548.78F, count = 1724, sales = 873612.9F, }; // Predict sample data var prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(6.0084501F,10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score,10)}"); dataSample = new CountryData() { country = "United Kingdom", month = 11, year = 2017, med = 288.72F, max = 501.488F, min = 134.5360F, std = 707.5642F, prev = 873612.9F, count = 2387, sales = 1019647.67F, }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score,10)}"); Console.WriteLine(" "); Console.WriteLine("** Testing Country 2 **"); dataSample = new CountryData() { country = "United States", month = 10, year = 2017, med = 400.17F, max = 573.63F, min = 340.395F, std = 340.3959F, prev = 4264.94F, count = 10, sales = 5322.56F }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(3.805769F,10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score,10)}"); dataSample = new CountryData() { country = "United States", month = 11, year = 2017, med = 317.9F, max = 1135.99F, min = 249.44F, std = 409.75528F, prev = 5322.56F, count = 11, sales = 6393.96F, }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score,10)}"); }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static void TestPrediction(MLContext mlContext, string outputModelPath = "country_month_fastTreeTweedie.zip") { ConsoleWriteHeader("Testing Country Sales Forecast model"); ITransformer trainedModel; using (var stream = File.OpenRead(outputModelPath)) { trainedModel = mlContext.Model.Load(stream); } //ITransformer trainedModel; //using (var file = File.OpenRead(outputModelPath)) //{ // trainedModel = TransformerChain // .LoadFrom(mlContext, file); //} var predictionEngine = trainedModel.CreatePredictionEngine <CountryData, CountrySalesPrediction>(mlContext); Console.WriteLine("** Testing Country 1 **"); // Build sample data var dataSample = new CountryData() { country = "United Kingdom", month = 10, year = 2017, med = 309.945F, max = 587.902F, min = 135.640F, std = 1063.932092F, prev = 856548.78F, count = 1724, sales = 873612.9F, }; // Predict sample data var prediction = predictionEngine.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(6.0084501F, 10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); dataSample = new CountryData() { country = "United Kingdom", month = 11, year = 2017, med = 288.72F, max = 501.488F, min = 134.5360F, std = 707.5642F, prev = 873612.9F, count = 2387, sales = 1019647.67F, }; prediction = predictionEngine.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); Console.WriteLine(" "); Console.WriteLine("** Testing Country 2 **"); dataSample = new CountryData() { country = "United States", month = 10, year = 2017, med = 400.17F, max = 573.63F, min = 340.395F, std = 340.3959F, prev = 4264.94F, count = 10, sales = 5322.56F }; prediction = predictionEngine.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(3.805769F, 10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); dataSample = new CountryData() { country = "United States", month = 11, year = 2017, med = 317.9F, max = 1135.99F, min = 249.44F, std = 409.75528F, prev = 5322.56F, count = 11, sales = 6393.96F, }; prediction = predictionEngine.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static void TestPrediction(string outputModelPath = "country_month_fastTreeTweedie.zip") { ConsoleWriteHeader("Testing Country Sales Forecast model"); var env = new LocalEnvironment(seed: 1); //Seed set to any number so you have a deterministic environment ITransformer model; using (var file = File.OpenRead(outputModelPath)) { model = TransformerChain .LoadFrom(env, file); } var predictor = model.MakePredictionFunction <CountryData, CountrySalesPrediction>(env); Console.WriteLine("** Testing Country 1 **"); // Build sample data var dataSample = new CountryData() { country = "United Kingdom", month = 10, year = 2017, med = 309.945F, max = 587.902F, min = 135.640F, std = 1063.932092F, prev = 856548.78F, count = 1724, sales = 873612.9F, }; // Predict sample data var prediction = predictor.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(6.0084501F, 10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); dataSample = new CountryData() { country = "United Kingdom", month = 11, year = 2017, med = 288.72F, max = 501.488F, min = 134.5360F, std = 707.5642F, prev = 873612.9F, count = 2387, sales = 1019647.67F, }; prediction = predictor.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); Console.WriteLine(" "); Console.WriteLine("** Testing Country 2 **"); dataSample = new CountryData() { country = "United States", month = 10, year = 2017, med = 400.17F, max = 573.63F, min = 340.395F, std = 340.3959F, prev = 4264.94F, count = 10, sales = 5322.56F }; prediction = predictor.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(3.805769F, 10)}, Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); dataSample = new CountryData() { country = "United States", month = 11, year = 2017, med = 317.9F, max = 1135.99F, min = 249.44F, std = 409.75528F, prev = 5322.56F, count = 11, sales = 6393.96F, }; prediction = predictor.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month to predict: {dataSample.month + 1}, year: {dataSample.year} - Predicted Forecast (US$): {Math.Pow(prediction.Score, 10)}"); }