Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
ChrisGruber
Product and Topic Expert
Product and Topic Expert
2,285

Introduction


 

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.

Prerequisites:


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()
print(factory)

 



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 classification.py 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 os.name == 'nt':
CONFIG_DIRECTORY = AA_DIRECTORY + "/EXE/Clients/CPP"
else:
CONFIG_DIRECTORY = AA_DIRECTORY + "/KxShell"

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

def userConfirm(self, iSource, iPrompt):
pass

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

def stopCallBack(self, iSource):
pass

# 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, "", "")
config_store.loadAdditionnalConfig("KxShell.cfg")

# 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)
model.guessSpaceDescription("Training")

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

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

# Update the parameter tree
model.getParameter("")

# 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
try:
import matplotlib.pyplot as plt
bar_width = .6
plt.bar(range(n_top), values, bar_width, alpha=.45)
plt.gca().set_ylabel('Contribution')
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)
plt.show()

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 https://help.sap.com/http.svc/download?deliverable_id=20569176

 
1 Comment