import aalib
# Init front-end and factory
frontend = aalib.KxFrontEnd([])
factory = frontend.getFactory()
print(factory)
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
22 | |
14 | |
11 | |
11 | |
9 | |
8 | |
7 | |
7 | |
7 | |
6 |