# Implementation of ML models

The engine allows the implementation of models in many forms (from many sources):

* In the form of Java classes, default format for Algolytics ML Platform:
  * The class must be implemented in accordance with the interface definition provided in the engine
  * Possibility to use codes from any DM tools generating a scoring code in Java - proper interface of such code is required
* In the PMML form (PMML codes are generated by many DM tools, e.g. R, SPSS, SAS
* In ["R"](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/tutorials/implementation-of-ml-models/tutorial-how-to-publish-script-in-r-as-a-web-service) or [Python](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/tutorials/implementation-of-ml-models/python-models) package&#x20;
* In the form of a scenario (The [EXPRESSION node](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/scenario-building-blocks/expression) allows implementation of any scoring rules, including scoring models)

&#x20;Added codes can be queried by engine or used in scenarios.

## Upload new model <a href="#upload-new-model" id="upload-new-model"></a>

Scoring code can be added in Scoring code management panel. To add a scoring code, click the Scoring code action button and select Upload new model option, then enter the name of the code and drag the file (or a .zip file in case of R models) with the code to designated area on the right side of the screen, or click this area and select the file with the code to add.

A .zip file containing R model must contain:

* .R file with script containing transformations, calculations and/or models stored in RDS files. A variable with script output must be named rResult, RDS files can be used by refering to their names
* CSV file with input variables (as described in [Creating and testing a scenario](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/scenario-development) section)
* (optionally) RDS file(s) with model(s), these models can be used in the script by reference by file name. To save model as an RDS file, use saveRDS function in R.

a .zip file containg Python model must contain:

* a .pkl file with a serialized python object.
* CSV file with input variables (as described in [Creating and testing a scenario](https://algolytics-technologies.gitbook.io/algolytics/scoring.one-platform/scenario-development) section)
* the .py file should contain a scoring function with access to input variables and model object unpickled from model.pkl

Sample Python model calculates a grade based on the given percentage .zip: [Download](https://scoring-engine.readthedocs.io/en/latest/example/model.zip)

Sample file for creating a .pkl:

```
import pickle

data = {
    '2': 0,
    '3': 50,
    '3.5': 60,
    '4': 70,
    '4.5': 80,
    '5': 90
}

with open('model.pkl', 'wb') as f:
    pickle.dump(data, f)
```

A list of added codes is presented on the bottom of the panel.

## Import from json <a href="#import-from-json" id="import-from-json"></a>

Scoring code can be imported in Scoring code management panel. To import code click the Scoring code action button and select Import from json option, then drag the file with the code to designated area on the right side of the screen, or click this area and select the file with the code to add.

## Export <a href="#export" id="export"></a>

To download all scoring codes in JSON file, click the Scoring code actions button and select the Export option.

## Testing a code <a href="#testing-a-code" id="testing-a-code"></a>

Added codes can be queries in Forms panel. For this purpose, select a model from model drop-down list, enter proper values of displayed input variables (defined in the code), and then click the Query button. If the query is correct, output variables (defined in the code) will be displayed. Otherwise, the engine will display an appropriate error. Queries can be viewed in Browse scoring results panel, after choosing Scoring model or Pmml, R or Python model in Result type drop-down list.
