How to implement custom prediction model
Glossary Item Box
Introduction
Prediction service of the lookup field uses methods of statistic analysis for learning on the base of historical data and prediction of values for new records.
For more information about these functions please refer to the “Machine learning service” article.
Case description
Implement automatic prediction for the [AccountCategory] column by the values of the [Country], [EmployeesNumber] and [Industry] field while saving the account record. The following conditions should be met:
- Model learning should be created on the base of account records for last 90 days.
- Moodel Retraining should be performed every 30 days.
- Permissible value of prediction accuracy for the model – 0,6.
To complete this case you need to check the correctness of the value of the [Creatio cloud services API key] (CloudServicesAPIKey code) system setting and the URL of the predictive service in the [Service endpoint Url] field of the [ML problem types] lookup.
You can use the page UI-tools (fields and filters), as well as the [Predict data] business process element to perform the described case. You can find more cases on implementing prediction using the default Creatio tools in the "Predictive analysis” article.
Case implementation algorithm
1. Model learning
To train the model:
1. Add a record to the [ML Model] lookup. Values of the record fields are given in the Table 1.
Table 1. Values of the record fields of the MLModel lookup
Field | Value |
---|---|
Name | Predict account category |
ML problem type | Lookup prediction |
Target schema for prediction | Account |
Quality metric low limit | 0,6 |
Model retrain frequency (days) | 30 |
Training set metadata |
{ "inputs": [ { "name": "CountryId", "type": "Lookup", "isRequired": true }, { "name": "EmployeesNumberId", "type": "Lookup", "isRequired": true }, { "name": "IndustryId", "type": "Lookup", "isRequired": true } ], "output": { "name": "AccountCategoryId", "type": "Lookup", "displayName": "AccountCategory" } } |
Training set query |
new Select(userConnection) .Column("a", "Id").As("Id") .Column("a", "CountryId") .Column("a", "EmployeesNumberId") .Column("a", "IndustryId") .Column("a", "AccountCategoryId") .Column("c", "Name").As("AccountCategory") .From("Account").As("a") .InnerJoin("AccountCategory").As("c").On("c", "Id").IsEqual("a", "AccountCategoryId") .Where("a", "CreatedOn").IsGreater(Column.Parameter(DateTime.Now.AddDays(-90))) You can find examples of queries in the "Creating data queries for the machine learning model” article. |
Predictions enabled (checkbox) | Enable |
2. Perform the [Execute model training job] action on the [ML Model] lookup field.
Wait until the values of the [Model processing status] field will be changed in following sequence: DataTransfer, QueuedToTrain, Training, Done. The process may take several hours to finish (it depends on the amount of passed data and general workload of the predictive service.
2. Performing the prediction
To start the predictions:
1. Create a business process in the user package. Select the saving of the [Contact] object as a start signal for the process. Check if the required fields are populated (Fig. 1).
Fig. 1. Start signal properties.
2. Add the MLModelId lookup parameter that refers to the [ML Model] entity. Select the record with the [Predict account category] model as a value.
3. Add the RecordId lookup parameter that refers to the [Account] entity. Select a reference for theRecordId parameter of the [Signal] element as a value.
4. Add a [Script task] element on the business process diagram and add the following code there:
var userConnection = Get<UserConnection>("UserConnection"); // Getting the Id of the Account record. Guid entityId = Get<Guid>("RecordId"); // Geeting the id of the model. var modelId = Get<Guid>("MLModelId"); var connectionArg = new ConstructorArgument("userConnection", userConnection); // Object for calling prediction. var predictor = ClassFactory.Get<MLEntityPredictor>(connectionArg); // Call of the forecasting service. The Data is saved in MLPrediction and in case of high probability of forecasting the data is saved in the required field of the Account. predictor.PredictEntityValueAndSaveResult(modelId, entityId); return true;
After saving and compiling the process, the prediction will be performed for new accounts. The prediction will be displayed on the account edit page.
This implementation of the prediction slows down the saving an account record because call of the prediction service is executed in 2 seconds. This can reduce the performance of the mass operations with data saving, like import from Excel.