Empowering everyone to make their BC extensions smarter

Last 2,5 months were very busy for me. I travelled around the world talking about Artificial Intelligence, in particular about AI in Business Central.

Here is my world-tour-map :) I did approx. 28K miles (45K km). The equatorial circumference of Earth is about 25K miles (40K km), and distance to the moon is 240K miles (384K km).

 

Powered by http://www.gcmap.com

So I had sessions at events: Dynamics Dutch community, Directions ASIA, Directions NA, Days Of Knowledge. I guess the only one who did the same was AJ :)

And you know what, I was surprised. I thought that the level of knowledge in our community about AI should be at least 101. But it’s not. It’s near to zero. Shocked? I guess not. Our community is very strong in NAV, but not very flexible in everything around. Even in Business Central related area - I still see many, many, maaaany developers who didn't try Extensions at all, not mentioning AI.

Anyway. I see my goal in empowering everyone to make their BC extensions smarter. And no one will stop me.

ML Prediction API in Business Central 

One of the latest and the most advanced ML features, available in the Business Central is the opportunity to train custom machine learning model directly from AL. If you are building an industry solution, you can add train-ml-model function directly to your Business Central App.

The API is available in the Codeunit 2003 “ML Prediction Management”. Let’s look at how it works.

Predict employee leave

When talking about ML, it’s always important to show usage on real examples. Let’s try to predict employee leave directly from Business Central. That could be a good addition to the HR module.

Data

Data is the engine of Machine Learning. To predict the future, we need to know the past.

Here we have features which influence on prediction – will employee leave or not? 

Publish a general prediction model as a Web Service from a public template

Before the training process, you need to publish prediction ML Web service into your Azure Subscription.

  • Go here
  • Click Open in Azure Machine Learning Studio.
  • Run it, and then deploy it as a web service.
  • In the web service dashboard, copy the API key.

This is a publicly available model prepared by the Microsoft ERP team and designed especially for ML Prediction Management usage.

 

Train ML model from AL

One of the coolest features in ML Prediction API is the opportunity to train a machine learning model directly from AL. Here how it works.

In the VSCode project create a codeunit "Train EmployeeLeave ML"  with function Train().

   

And Variables

  

  • ML Prediction Management – is the main codeunit, which has functions to train, save and use the ML model
  • MyModel – trained model in coded text format
  • MyModelQuality – the quality of the trained model
  • Setup – table, where the trained model is stored
  • EmployeeLeaveEntry – historical data, used to train an ML model

The workflow of Train function is the next

  • Specify the connection to your published general predictive ML Web Service
  • Prepare historical data for the training process. On this step, you can filter data, add new columns, convert datatypes etc.
  • Specify features. Features are the fields, that influence on prediction.
  • Specify label. The label is the field, which you are going to predict.
  • Train model. The goal is to create a so-called learning function, which will take features as an input and will produce prediction in the output. This learning function is the core of machine learning. So, how do you create it? ML Prediction API sends a request to the web service with the historical data, prepared for training, where it applies a statistical algorithm to the data. As a result, you will get trained model in Base64 text format and model quality.
  • Save your trained model locally. It will be used later, in the prediction process itself.
  • Check the model quality. Model quality shows you how good your model in predictions. For example, 80% means that in 80% of cases predictions are correct. The more model quality you have, the better will be predictions. But don’t expect 100% :) 

And if we will train a model using employee-leave-historical-data, we will have the model quality of 96%.

 

Predict, using the trained model

Once you have a trained model, you can apply it to the new data and predict the future. 

Let’s predict if an employee will leave the company in the nearest future, depending on his current state: average work hours, number of projects, salary, satisfaction level, position and other criteria’s.

Create separate Codeunit "Predict Employee Leave ML" with function Predict().

  

And Variables

  • EmployeeExtendedData - is the temporary table, where will populate current employee features and also leave predictions, together with confidences.

The workflow of Predict function is the next

  • Check that you have trained model in place
  • Specify the connection to your published general predictive ML Web Service
  • Prepare input parameters. Generate a temporary table with data (features), which will be used to get leave predictions. The structure of that table should be the same, as you used in the training process. Otherwise, prediction Web Service will not work properly. You can have one or more rows in that table. Predictions will be calculated for each row.
  • Specify the features from the passed table. List them in the same order as you listed, when you trained the model. Otherwise, prediction Web Service will not work properly.
  • Specify a label from the passed table. Use the same field, as you used when you trained model. You can mention here only one field.
  • Predict values and pass there a trained ML model.ML Prediction API could make predictions using classification or regression algorithms. You don’t control that. If the label field has a type of integer or decimal, then regression-tree algorithm “annova” will be applied. Otherwise, it will use a classification algorithm. For classification algorithms, you can also get confidence % of the prediction.
  • Save a forecast result

And if we will Predict Employee Leave, we will get predictions!

In the list view, we can also see confidence %.

Get “Why-Insights”

When I proudly :) showed this to my wife, who is HR, the first question I’ve got was: “Brilliant! But why will Mary leave?” – fair enough. 

Understanding why AI did certain prediction becoming more and more important in our society. This is not only about the trust in AI models, but also about getting insights. 

ML Prediction API has a cool method PlotModel which answers on why question with 1 line of code!

  

You just need to pass the ML model in Base64 format, features and label and you will get a .pdf file with a decision tree.

PlotModel method will return you this pdf file in Base64 format. You can download it as a file using DownloadPlot method or use embedded pdf in Business Center directly. I used AJ blog for that.

  

 

Conclusion

ML Prediction API is a very good starting point to your AI journey. Define Question – Get Data – Train – Predict – Get Insights – Done.

And to finalize this blog I would like to quote Vincent from the Days Of Knowledge Keynote: “AI is not sex, and you are not a teenager. Don’t just talk of it, try it!”. 

Code is available on my GitHub https://github.com/dkatson/BC-MLPredictionAPI-Predict-Employee-Leave