public IActionResult GetProductUnitDemandEstimation(float 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 = productId,
                year      = year,
                month     = month,
                units     = units,
                avg       = avg,
                count     = count,
                max       = max,
                min       = min,
                prev      = prev
            };

            ProductUnitRegressionPrediction nextMonthUnitDemandEstimation = null;

            //Predict
            nextMonthUnitDemandEstimation = this.productSalesModel.Predict(inputExample);

            return(Ok(nextMonthUnitDemandEstimation.Score));
        }
Ejemplo n.º 2
0
        private static void TestPredictionRegression(MLContext mlContext, string outputModelPath, string dataPath)
        {
            Console.WriteLine("Testing Product Unit Sales Forecast Regression model");

            // Read the model that has been previously saved by the method SaveModel.

            ITransformer trainedModel;

            using (var stream = File.OpenRead(outputModelPath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
            }

            var predictionEngine = mlContext.Model.CreatePredictionEngine <SaleData, ProductUnitRegressionPrediction>(trainedModel);

            Console.WriteLine("** Testing Product **");

            // Predict the nextperiod/month forecast to the one provided
            ProductUnitRegressionPrediction prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[0]);

            Console.WriteLine($"Product: {SampleSaleData.MonthlyData[0].id}, month: {SampleSaleData.MonthlyData[0].mes + 1}, year: {SampleSaleData.MonthlyData[0].ano} - Real value (units): {SampleSaleData.MonthlyData[0].siguiente}, Forecast Prediction (units): {prediction.Score}");

            // Predicts the nextperiod/month forecast to the one provided
            prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[1]);
            Console.WriteLine($"Product: {SampleSaleData.MonthlyData[1].id}, month: {SampleSaleData.MonthlyData[1].mes + 1}, year: {SampleSaleData.MonthlyData[1].ano} - Forecast Prediction (units): {prediction.Score}");

            //prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[2]);
            //Console.WriteLine($"Product: {SampleSaleData.MonthlyData[2].id}, month: {SampleSaleData.MonthlyData[2].mes + 1}, year: {SampleSaleData.MonthlyData[2].ano} - Forecast Prediction (units): {prediction.Score}");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Predict samples using saved model
        /// </summary>
        /// <param name="outputModelPath">Model file path</param>
        public static void TestPrediction(MLContext mlContext, string outputModelPath = "product_month_fastTreeTweedie.zip")
        {
            ConsoleWriteHeader("Testing Product Unit Sales Forecast Regression model");

            // Read the model that has been previously saved by the method SaveModel.

            ITransformer trainedModel;

            using (var stream = File.OpenRead(outputModelPath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
            }

            var predictionEngine = mlContext.Model.CreatePredictionEngine <ProductData, ProductUnitRegressionPrediction>(trainedModel);

            Console.WriteLine("** Testing Product **");

            // Predict the nextperiod/month forecast to the one provided
            ProductUnitRegressionPrediction prediction = predictionEngine.Predict(SampleProductData.MonthlyData[0]);

            Console.WriteLine($"Product: {SampleProductData.MonthlyData[0].productId}, month: {SampleProductData.MonthlyData[0].month + 1}, year: {SampleProductData.MonthlyData[0].year} - Real value (units): {SampleProductData.MonthlyData[0].next}, Forecast Prediction (units): {prediction.Score}");

            // Predicts the nextperiod/month forecast to the one provided
            prediction = predictionEngine.Predict(SampleProductData.MonthlyData[1]);
            Console.WriteLine($"Product: {SampleProductData.MonthlyData[1].productId}, month: {SampleProductData.MonthlyData[1].month + 1}, year: {SampleProductData.MonthlyData[1].year} - Forecast Prediction (units): {prediction.Score}");
        }
Ejemplo n.º 4
0
        private static void TestPredictionRegression(MLContext mlContext, string outputModelPath, string dataPath)
        {
            Console.WriteLine("Testing Product Unit Sales Forecast Regression model");

            // Read the model that has been previously saved by the method SaveModel.

            ITransformer trainedModel;

            using (var stream = File.OpenRead(outputModelPath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
            }

            //using (var reader = new StreamReader(dataPath))
            //{
            //    reader.ReadLine();
            //    while (!reader.EndOfStream)
            //    {
            //        var line = reader.ReadLine();
            //        var values = line.Split(',');
            //        int value = Convert.ToInt32(values[4]);
            //        for (int i = 0; i < value - 1; i++)
            //        {
            //            Console.Write("*");
            //        }
            //        Console.Write("*\n");
            //    }
            //}

            var predictionEngine = mlContext.Model.CreatePredictionEngine <SaleData, ProductUnitRegressionPrediction>(trainedModel);

            Console.WriteLine("** Testing Product **");

            // Predict the nextperiod/month forecast to the one provided
            ProductUnitRegressionPrediction prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[0]);

            Console.WriteLine($"Product: {SampleSaleData.MonthlyData[0].id}, month: {SampleSaleData.MonthlyData[0].mes + 1}, year: {SampleSaleData.MonthlyData[0].ano} - Real value (units): {SampleSaleData.MonthlyData[0].siguiente}, Forecast Prediction (units): {prediction.Score}");

            // Predicts the nextperiod/month forecast to the one provided
            prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[1]);
            Console.WriteLine($"Product: {SampleSaleData.MonthlyData[1].id}, month: {SampleSaleData.MonthlyData[1].mes + 1}, year: {SampleSaleData.MonthlyData[1].ano} - Forecast Prediction (units): {prediction.Score}");

            //prediction = predictionEngine.Predict(SampleSaleData.MonthlyData[2]);
            //Console.WriteLine($"Product: {SampleSaleData.MonthlyData[2].id}, month: {SampleSaleData.MonthlyData[2].mes + 1}, year: {SampleSaleData.MonthlyData[2].ano} - Forecast Prediction (units): {prediction.Score}");
        }
        /// <summary>
        /// Predict samples using saved model
        /// </summary>
        /// <param name="outputModelPath">Model file path</param>
        public static void TestPrediction(MLContext mlContext, string outputModelPath = "product_month_fastTreeTweedie.zip")
        {
            ConsoleWriteHeader("Testing Product Unit Sales Forecast Regression model");

            // Read the model that has been previously saved by the method SaveModel

            ITransformer trainedModel;

            using (var stream = File.OpenRead(outputModelPath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
            }

            var predictionEngine = mlContext.Model.CreatePredictionEngine <ProductData, ProductUnitRegressionPrediction>(trainedModel);

            Console.WriteLine("** Testing Product 1 **");

            // Build sample data
            ProductData dataSample = new ProductData()
            {
                productId = 263,
                month     = 10,
                year      = 2017,
                avg       = 91,
                max       = 370,
                min       = 1,
                count     = 10,
                prev      = 1675,
                units     = 910
            };

            // Predict the nextperiod/month forecast to the one provided
            ProductUnitRegressionPrediction prediction = predictionEngine.Predict(dataSample);

            Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Real value (units): 551, Forecast Prediction (units): {prediction.Score}");

            dataSample = new ProductData()
            {
                productId = 263,
                month     = 11,
                year      = 2017,
                avg       = 29,
                max       = 221,
                min       = 1,
                count     = 35,
                prev      = 910,
                units     = 551
            };

            // Predicts the nextperiod/month forecast to the one provided
            prediction = predictionEngine.Predict(dataSample);
            Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecast Prediction (units): {prediction.Score}");

            Console.WriteLine(" ");

            Console.WriteLine("** Testing Product 2 **");

            dataSample = new ProductData()
            {
                productId = 988,
                month     = 10,
                year      = 2017,
                avg       = 43,
                max       = 220,
                min       = 1,
                count     = 25,
                prev      = 1036,
                units     = 1094
            };

            prediction = predictionEngine.Predict(dataSample);
            Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Real Value (units): 1076, Forecasting (units): {prediction.Score}");

            dataSample = new ProductData()
            {
                productId = 988,
                month     = 11,
                year      = 2017,
                avg       = 41,
                max       = 225,
                min       = 4,
                count     = 26,
                prev      = 1094,
                units     = 1076
            };

            prediction = predictionEngine.Predict(dataSample);
            Console.WriteLine($"Product: {dataSample.productId}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecasting (units): {prediction.Score}");
        }
Ejemplo n.º 6
0
        public static void PredictionRegression(int id_predict, SqlParameter[] parameters, string outputModelPath = "product_month_fastTreeTweedie.zip")
        {
            MLContext mlContext = new MLContext(seed: 1);

            // Read the model that has been previously saved by the method SaveModel.
            ITransformer trainedModel;

            using (var stream = File.OpenRead(ModelPath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
            }

            var          predictionEngine = mlContext.Model.CreatePredictionEngine <SaleData, ProductUnitRegressionPrediction>(trainedModel);
            DBManagement connect          = new DBManagement();
            ProductUnitRegressionPrediction prediction = null;
            int mescalc  = ((DateTime)parameters[3].Value).Month;
            int anocalc  = ((DateTime)parameters[3].Value).Year;
            int anterior = 0;

            for (int i = 0; i < (int)parameters[1].Value; i++)
            {
                SaleData lastMonth = null;

                //int mescalc = ((DateTime)parameters[3].Value).Month;
                //int anocalc = ((DateTime)parameters[3].Value).Year;
                if (i == 0)
                {
                    lastMonth = connect.getLastMonthSaleData(988, anocalc, mescalc);
                }
                else
                {
                    lastMonth = new SaleData()
                    {
                        siguiente = 0,
                        id        = (float)988,
                        ano       = (float)anocalc,
                        mes       = (float)mescalc,
                        unidades  = (float)prediction.Score,
                        anterior  = (float)anterior
                    };
                }
                // Predict the nextperiod/month forecast to the one provided
                prediction = predictionEngine.Predict(lastMonth);
                anterior   = (int)lastMonth.unidades;

                SqlParameter[] parametersPrevision = connect.getparametersPrevisionArray();
                parametersPrevision[0].Value = id_predict;
                parametersPrevision[1].Value = lastMonth.id;
                if ((int)lastMonth.mes + 1 <= 12)
                {
                    parametersPrevision[2].Value = new DateTime((int)lastMonth.ano, ((int)lastMonth.mes + 1), 1);
                }
                else
                {
                    parametersPrevision[2].Value = new DateTime((int)lastMonth.ano + 1, 1, 1);
                }
                parametersPrevision[3].Value = prediction.Score;
                connect.insertPrevision(parametersPrevision);
                mescalc = (((DateTime)parameters[3].Value).Month + i) % 12 + 1;
                anocalc = ((DateTime)parameters[3].Value).Year + (((DateTime)parameters[3].Value).Month + i) / 12;
            }
        }