Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

08. Regression Sales Forecasting

Miguel Veloso edited this page Feb 13, 2019 · 1 revision

Introduction

Regression problems are considered the most basic Machine Learning cases. eShopOnContainersAI uses regression model to predict product and country unit sales for the next period. We defined an "on-premises" workflow which allows to have complete control over the training and evaluation processes. This walkthrough will guide you in the process of creating and consuming the model: obtain training files, execute the training project, update the models and deploy the forecast microservice to deliver predictions for the Dashboard web application.

eShopDashboardML is an stand-alone website application based in monolithic architecture (instead of microservice architecture), that shares the same code base and forecasting models. For more information, check https://github.com/dotnet/machinelearning-samples/tree/master/samples/end-to-end-apps/eShopDashboardML

Training Setup

Step 1: Prepare data file

The model will be trained using the data from eShopOnContainersAI. This data is pre-processed and exposed as CSV files using microservices. In order to facilitate the access to this data, there are downloading links available in the Data section of the Dashboard. In local development box, you can access to the Dashboard which is exposed at http://localhost:5300/Datasets/Forecast

image

NOTE: This guide covers both models (product and country forecasting) used in eShopOnContainersAI. In general, the setup is common to both models, except in places where is explicitly stated.

  • In the case of product forecasting, you need to access to Data / Sales Forecasting and click the button under the section Product unit sales (orderItems.stats.csv).
  • In the case of country forecasting, you need to access to Data / Sales Forecasting and click the button under the section Country Sales (countries.stats.csv).

Step 2: Setup training project

Then copy/move downloaded data files into the src/Services/AI.Sales.Forecasting/Training/AI.Sales.Forecasting.MLNet/data folder, using exactly the same file names:

Run the training project console application, either set up as starting project and hit F5, or debug the project as shown in the following screenshoot:

image

At the end of the execution, the output will be similar to this screenshot:

image

The models are generated in the same folder where the application is installed. So, you should get to the folder \src\Services\AI.SalesForecasting\Training\bin\Debug\netcoreapp2.0. In this folder, you will find a couple of files:

  • country_month_fastTreeTweedie.zip: sales forecasting by country
  • product_month_fastTreeTweedie.zip: sales forecasting by product

Step 3: Setup forecasting project

You must copy model files generated in previous step to the project src/Services/AI.Sales.Forecasting/AI.Sales.Forecasting.MLNet.API:

image

This project contains a folder named ModelsAI, that stores both model files used by the forecasting.

Step 4: Setup web dashboard project

Using Visual Studio

The first time you run the project, you should run the following command in a console located in the WebDashboardRazor folder:

npm install

Then, you need to Build the WebDashboardRazor project in order to execute the gulp task that copies library files to the wwwroot folder.

Finally, you need to update the docker-compose project (right click in the docker-compose project and select Properties menu), and update the dialog as described in the following screenshot:

image

Then, you can press F5 or Ctrl+F5 for running the project. In a few seconds, a browser will be open showing the dashboard.

Using docker-compose

In order to deploy the dashboard, you should execute following command:

docker-compose up -d webraz

After building and setting up image container, the dashboard should be accessible in the developer box at http://localhost:5300

Code Walkthrough

    public class CountryData
    {
        // next,country,year,month,max,min,std,count,sales,med,prev
        public CountryData(string country, int year, int month, float max, float min, float std, int count, float sales, float med, float prev)
        {
            this.country = country;

            this.year = year;
            this.month = month;
            this.max = max;
            this.min = min;
            this.std = std;
            this.count = count;
            this.sales = sales;
            this.med = med;
            this.prev = prev;
        }

        [ColumnName("Label")]
        public float next;

        public string country;

        public float year;
        public float month;
        public float max;
        public float min;
        public float std;
        public float count;
        public float sales;
        public float med;
        public float prev;
    }

    public class CountrySalesPrediction
    {
        // Below columns are produced by the model's predictor.
        public Single Score;
    }

    public class CountrySales : ICountrySales
    {
        public async Task<CountrySalesPrediction> Predict(string modelPath, string country, int year, int month, float max, float min, float std, int count, float sales, float med, float prev)
        {
            // Load model
            var predictionEngine = await CreatePredictionEngineAsync(modelPath);

            // Build country sample
            var countrySample = new CountryData(country, year, month, max, min, std, count, sales, med, prev);

            // Returns prediction
            return predictionEngine.Predict(countrySample);
        }

        private async Task<PredictionModel<CountryData, CountrySalesPrediction>> CreatePredictionEngineAsync(string modelPath)
        {
            PredictionModel<CountryData, CountrySalesPrediction> model = await PredictionModel.ReadAsync<CountryData, CountrySalesPrediction>(modelPath);
            return model;
        }
    }

The class CountryData represents the input data for the model, using the same schema as the input training file, and the class CountrySalesPrediction represents the output label (result) of the model. The model is loaded asynchronously using the function PredictionModel.ReadAsync, which returns a PredictionModel object that executes model prediction using the Predict method.

Web Dashboard

image

News

Setup

Scenarios

Clone this wiki locally