Skip to Content



In a previous article on this subject, I walked you through setting up SAP Predictive Analytics with Python.  As I mentioned in the article, Python is one of the most used languages for machine learning and is well equipped in numeric calculation.  Embedding predictive analytics libraries into a Python application is a natural progression for SAP Predictive Analytics.  In this article, we will take you through using Jupyter Notebook with SAP Predictive Analytics.  Jupyter Notebook is user friendly application that many people use with Python.  Jupyter Notebook is a browser based tool that enables one to create and share documents that can include live code, equations, visualizations and description text for the code and solutions.


Follow the previous article.

Steps to Follow


  • Launch Anaconda Navigator. Press the Windows start button  and type “Anaconda”, click on the “Anaconda Navigator” icon the appears as shown below.

  • If you see the Install button with the Jupyter Notebook, press this Install button (boxed in red below) to install Jupyter Notebook. If the button is titled “Launch”, proceed to the next step.

  • SAP Predictive Analytics requires Python version 3.5. Click on the Environments link boxed in red below to manage the environment.

  • Double click on the “py35” environment below to switch to the Python version 3.5 environment.

  • Press the Home link boxed in red below.

  • Launch Jupyter Notebook by clicking the “Launch” button boxed in red below.

  • Click on ‘New’ drop-down menu and select ‘Python 3’ boxed in red below.

  • Test if the library will load into Jupyter Notebook. Copy and paste the following code into the Jupyter Notebook editor and press the run button boxed in red below.
import aalib
# Init front-end and factory
frontend = aalib.KxFrontEnd([])
factory = frontend.getFactory()


If you see the response similar to above, then the library is loading for you and you are ready work with the SAP Predictive Analytics automated library within Jupyter Notebook.


  • Now lets try an example. Paste the following code into the Jupyter Notebook editor.  You may need to change line 6 if you installed SAP Predictive Analytics in a different location then the default.  This code is based on the example included with the product.
import os
import aalib

##Line 5: Change this line if your installation directory is different
AA_DIRECTORY = "\Program Files\SAP Predictive Analytics\Desktop\Automated"

# Path to KxShell.cfg file
if == 'nt':

class DefaultContext(aalib.IKxenContext):
""" Class used to display feedback from Automated """
    def userMessage(self, iSource, iMessage, iLevel):
        if iLevel != 6:
        return True

    def userConfirm(self, iSource, iPrompt):

    def userAskOne(self, iSource, iPrompt, iHidden):

    def stopCallBack(self, iSource):

# Init front-end and factory

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

# Configure automated with the license, and other settings
factory.setConfiguration("DefaultMessages", "true")
config_store = factory.createStore("Kxen.FileStore")
config_store.setContext(context, 'en', 8, False)
config_store.openStore(CONFIG_DIRECTORY, "", "")

# Create a model
model = factory.createModel("Kxen.SimpleModel")
model.setContext(context, 'en', 8, False)

# Configure it for Regression/Classification
model.pushTransformInProtocol("Default", "Kxen.RobustRegression")
store = model.openNewStore("Kxen.FileStore", AA_DIRECTORY + "/Samples/Census",
                          "", "")
model.newDataSet("Training", "Census01.csv", store)

# Specify variables roles:
# all variables as input, except the target and the key
target = "class"
variables = model.getParameter("Protocols/Default/Variables")
variables.setAllValues("Role", "input")
variables.setSubValue(target + "/Role", "target")
variables.setSubValue("KxIndex/Role", "skip")

# train the model with all default settings
model.sendMode(aalib.Kxen_learn, store)

# Update the parameter tree

# Get back the contributions
regr_path = ("Protocols/Default/Transforms/Kxen.RobustRegression/Results/%s"
             % target)
contrib_param = model.getParameter(regr_path + "/MaxCoefficients")

# Get back all the (Name, Contrib) pair
contribs = contrib_param.getSubEntries("Contrib")

# sort them descending
sorted_contribs = sorted(contribs, key=lambda x: float(x[1]), reverse=True)
print("Top Influencers:")
n_top = 5
names = [x[0] for x in sorted_contribs[:n_top]]
values = [float(x[1]) for x in sorted_contribs[:n_top]]
for name, value in zip(names, values):
    print("{:>30} {:.4f}".format(name, value))

# Graph it if possible
    import matplotlib.pyplot as plt
    bar_width = .6, values, bar_width, alpha=.45)
    plt.gca().set_xticks([x for x in range(n_top)])
    plt.gca().set_xticklabels(names, rotation=50)
    plt.title("Top %d Influencers" % n_top)

except ImportError:
    print("Module matplotlib not available - can not generate plot")


  • Press the Run button as you had done to test the loading of the library.

  • Scroll down, you should get output similar to the following image:

Congratulations you have successfully run through a SAP Predictive Analytics sample within Jupyter Notebook!!!

Note:  The sample was slightly changed due to setting up location of the AA_DIRECTORY variable based on the installation path.


Related Resources


For more information on the Automated Analytics API see the SAP documentation at


To report this post you need to login first.

1 Comment

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

Leave a Reply