# Introduction

At the 2015 Innovation summit I presented ABC and kmeans analysis.  This document will cover how you can use the Predicative Analysis Library (PAL) that comes with SAP HANA to implement ABC and kmeans analysis.  The primary focus will be on kmeans.  The kmeans application shown her can be used as framework to make the ABC Analysis app.  There is a nice video about the app available on https://www.youtube.com/watch?v=y8CUXdTLKhY&feature=youtu.be.

## What is kmeans analysis?

In predictive analysis, k-means clustering is a method of cluster analysis. The k-means algorithm partitions n observations or records into k clusters in which each observation belongs to the cluster with the nearest center. In marketing and customer relationship management areas, this algorithm uses customer data to track customer behavior and create strategic business initiatives. Organizations can thus divide their customers into segments based on variants such as demography, customer behavior, customer profitability, measure of risk, and lifetime value of a customer or retention probability.

Clustering works to group records together according to an algorithm or mathematical formula that attempts to find centroids, or centers, around which similar records gravitate. The most common algorithm uses an iterative refinement technique.  This definition comes from SAP HANA Predictive Analysis Library (PAL) Reference.  If you are planning to use PAL algorithms in your code I strongly recommend reading the pdf before you continue.  The SPS 8 version of PAL guide talks about kmeans starting on page 32.

# How do I build a kmeans application

Read the kmeans section of PAL guide and copy the sample code to your HANA studio.  You should become very familiar with this code because you use the code in the guide to build your app.

Once you have taking the sample code, walked through, it and executed it you are ready to start implementing your own kmeans analysis.

# Walk through of the kmeans app presented at the 2015 Innovation Summit

Below I will walk you through the kmeans code I demonstrated at the 2015 Innovation Summit.

## Understanding kmeans code

After I familiarized myself with the code shown in PAL guide, I built a calculation view to provide the input data for the function.

Once the calculation view had been successful tested I replaced the hardcode fields from the pdf with a select from the view.

Now I have a kmeans analysis running against real Business One data.  Below are the results.

The attached sql file (kmeans.sql) shows the code used to get results.

The results show our data points and there distance from the center, this is exactly what kmeans offer, but looking at the table of results is not cool.  I do not have a hot app for my executives.

## Turning kmeans Results into a cool app

The data shown above can be presented on a cool webpage.  The web app (could also run on a smart phone) takes the data above and presents it in two different manners.  The upper chart shows total sales amount on the y axis and days till cash is in hand on the x axis. The lower chart shows days past due for the y axis and group position for the x axis.  Both are easy for the user community to consume.

## How do I go from SQL results to SAPUI5 app running in a browser?

The app above comes from a few pieces of Javascript code.  The first piece is a Javascript app that creates all the HANA tables, calls the eraser and creator procedure, populates the control tables, the input tables and calls the kmeans function, which populates the result tables.  After you write the kmeans XSJS script you can test it in the browser.

Above we see results very similar to the HANA SQL results.

## How do I turn the ugly XSJS results into a cool bubble chart

Once the XSJS results are verified the next thing we need to do is build controller, in Javascript that will render the cool results (bubble chart shown in the browser screenshot above).

In the project I created a folder called paldemo.  Inside paldemo I created palkmeans_analysis.controller.js and palkmeans_analysis.view.js.  Inside the controller file I start with sap.ui.controller(“paldemo.palkmeans_analysis”.  Now we have setup the UI controller and it makes a call to palkmeans_analysis.view.js.

The ui.controller provides the look and feel.  The palkmeans_analysis.view.js does the work.  The palkmeans_analysis starts by defining two bubble chart inside a sap.ui.jsview call.

Now we’ve defined the charts that will be seen for our output.  The basic form of the output is set so we move onto sap.viz.ui5.data.FlattenedDataset to build the output dataset.  The layout is built using sap.ui.commons.layout.MatrixLayout and everything is added to the screen using the CreateContent function.

Now everything is put to together on the Javascript side lets write a simple index.html to call the components.

Now you are ready to test the app.  I’ve attached a delivery unit containing the code the file is named KMEANS_PAL_DU-Vendor.tgz.txt.zip.  After downloading rename it to KMEANS_PAL_DU-Vendor.tgz, SCN will not let ne upload tgz or zip files.