Skip to Content

This is a follow-up of a previous post: Debrief a model.

We have reached the step where our classification model shows enough accuracy and robustness to be used in production. One way of deploying our model is to export its scoring equation and hand over the code (e.g. Java, C++, SQL) to a member of the IT team who will test that code and move it to the production environment.

In this article, you will see how to generate the scoring equation of a trained model using the Python API of SAP Predictive Analytics inside a Jupyter notebook.


Loading the Model

We start with a blank notebook.

We run the below code to import the Automated Analytics library and load the model.

import sys
sys.path.append(r"C:\Program Files\SAP Predictive Analytics\Desktop\Automated\EXE\Clients\Python35")
import os
os.environ['PATH'] = r"C:\Program Files\SAP Predictive Analytics\Desktop\Automated\EXE\Clients\CPP"

AA_DIRECTORY = "C:\Program Files\SAP Predictive Analytics\Desktop\Automated"

import aalib

class DefaultContext(aalib.IKxenContext):
    def __init__(self): 

    def userMessage(self, iSource, iMessage, iLevel):
        return True

    def userConfirm(self, iSource, iPrompt):

    def userAskOne(iSource, iPrompt, iHidden):

    def stopCallBack(iSource):

frontend = aalib.KxFrontEnd([])
factory = frontend.getFactory()
context = DefaultContext()

factory.setConfiguration("DefaultMessages", "true")
config_store = factory.createStore("Kxen.FileStore")
config_store.setContext(context, 'en', 10, False)
config_store.openStore(AA_DIRECTORY + "\EXE\Clients\CPP", "", "")

# LOAD Model
model_name = "My Classification Model"
store = factory.createStore("Kxen.FileStore")
store.openStore(folder_name, "", "")
model = store.restoreLastModelD(model_name)


Generating the Java Code

The method described below allows us to generate the scoring equation in Java.


We specify the java file name and the folder where to write that file. Then we generate the code.

output_file = ""
model.generateCode("JAVA", output_folder, output_file)

We took Java as an example. All the code types are listed in the Developer Guide.


Generating the SQL code

Another code generation method exists that is better suited for SQL: model.generateCode2.

Here is the description of that method with its set of parameters.


To illustrate how the SQL code generation works, we will switch to another model built on insurance claims data for fraud detection. In the configuration part, just before training, the claim unique identifier was specified as a key using the code below.

# Set the Key and Target Columns
target_col = "IS_FRAUD"
key_col = "CLAIM_ID"
# Set the Roles
variables = model.getParameter("Protocols/Default/Variables")
variables.setAllValues("Role", "input")
variables.setSubValue(target_col + "/Role", "target")
variables.setSubValue("KxIndex/Role", "skip")
variables.setSubValue("KxIndex/KeyLevel", "0")
variables.setSubValue(key_col + "/Role", "skip")
variables.setSubValue(key_col + "/KeyLevel", "1")

We have loaded the fraud detection model using the method: store.restoreLastModelD.

Now, we generate the SQL code specific to SAP HANA.

output_file = "DETECT_FRAUD_EQUATION.sql"
table_name = "NEW_CLAIMS"
key_col = "CLAIM_ID"
model.generateCode2("HANA", output_folder, output_file, "", table_name, key_col)

That SQL code when executed returns two columns: the claim id and the prediction score. The user can modify the SQL statement based on the business needs: e.g. Sort the results in descending order of score and restrict the list to the top n claims.

To obtain additional information like the Yes/No fraud prediction, we must activate the advanced apply settings mode and specify the columns to be added after the key and the score.

t = model.getTransformInProtocol("Default", 0)
t.changeParameter("Parameters/ExtraMode", "Advanced Apply Settings")
# Get Long Names For Output Columns
model.changeParameter("Parameters/CodeGeneration/UseVarNameAsAlias", "true")
# Prepare Settings Parameter
target_col = "is_fraud"
d_path = "Protocols/Default/Transforms/Kxen.RobustRegression/Parameters/ApplySettings/Supervised/%s" % target_col
settings = model.getParameter(d_path)
# Decision
flag = settings.getSubParameter("PredictedRankCategories")
# Probability Decision
flag = settings.getSubParameter("PredictedRankProbabilities")

We could also request the individual contributions of each predictor.

# Individual Contributions
d_path = "Parameters/ApplySettings/Supervised/%s/Contribution" % target_col
t.changeParameter(d_path, "all")

We generate the SQL file.

output_file = "DETECT_FRAUD_EQUATION_ADV.sql"
model.generateCode2("HANA", output_folder, output_file, "", table_name, key_col)


Another way to deploy a model is to directly make predictions and store the predicted values in a file or a table. This will be the subject of a new article.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply