public IActionResult GetProductUnitDemandEstimation(string productId, [FromQuery] int year, [FromQuery] int month, [FromQuery] float units, [FromQuery] float avg, [FromQuery] int count, [FromQuery] float max, [FromQuery] float min, [FromQuery] float prev) { // Build product sample var inputExample = new ProductData(productId, year, month, units, avg, count, max, min, prev); ProductUnitPrediction nextMonthUnitDemandEstimation = null; //Set the critical section if using Singleton for the PredictionFunction object // //lock(this.productSalesPredFunction) //{ // Returns prediction nextMonthUnitDemandEstimation = this.productSalesPredFunction.Predict(inputExample); //} // // Note that if using Scoped instead of singleton in DI/IoC you can remove the critical section // It depends if you want better performance in single Http calls (using singleton) // versus better scalability ann global performance if you have many Http requests/threads // since the critical section is a bottleneck reducing the execution to one thread for that particular Predict() mathod call // return(Ok(nextMonthUnitDemandEstimation.Score)); }
public IActionResult GetProductUnitDemandEstimation(string productId, [FromQuery] int year, [FromQuery] int month, [FromQuery] float units, [FromQuery] float avg, [FromQuery] int count, [FromQuery] float max, [FromQuery] float min, [FromQuery] float prev) { // Build product sample var inputExample = new ProductData(productId, year, month, units, avg, count, max, min, prev); ProductUnitPrediction nextMonthUnitDemandEstimation = null; //Predict nextMonthUnitDemandEstimation = this.productSalesModel.Predict(inputExample); return(Ok(nextMonthUnitDemandEstimation.Score)); }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static async Task TestPrediction(string outputModelPath = "product_month_fastTreeTweedie.zip") { Console.WriteLine("*********************************"); Console.WriteLine("Testing product forecasting model"); // Read the model that has been previously saved by the method SaveModel var model = await PredictionModel.ReadAsync <ProductData, ProductUnitPrediction>(outputModelPath); // Build sample data ProductData dataSample = new ProductData() { ProductId = "428", Year = 2018, Month = 8, Units = 404, Avg = 5.179487f, Count = 78, Max = 60, Min = 1, Prev = 335 }; // Predict sample data ProductUnitPrediction prediction = model.Predict(dataSample); Console.WriteLine($"Product: {dataSample.ProductId}, month: {dataSample.Month + 1}, year: {dataSample.Year} - Real value: {dataSample.Units}, Forecasting: {prediction.Score}"); dataSample = new ProductData() { ProductId = "428", Year = 2018, Month = 9, Units = 302, Avg = 5.206896f, Count = 58, Max = 40, Min = 1, Prev = 404 }; prediction = model.Predict(dataSample); Console.WriteLine($"Product: {dataSample.ProductId}, month: {dataSample.Month + 1}, year: {dataSample.Year} - Forecasting: {prediction.Score}"); }