You are on page 1of 4

3/13/24, 9:52 PM SAP HANA PAL – K-Means Algorithm or How to do Cust...

- SAP Community

CREATE TYPE CONTROL_T_TELCO_S AS TABLE(

"Name" VARCHAR(100),

"intArgs" INTEGER,

"doubleArgs" DOUBLE,

"strArgs" VARCHAR(100));

/* This is the table that contains the input and output tables */

DROP table PDATA_TELCO_S;

CREATE column table PDATA_TELCO_S(

"ID" INTEGER,

"TYPENAME" VARCHAR(100),

"DIRECTION" VARCHAR(100));

insert into PDATA_TELCO_S values (1,'_SYS_AFL.T_KMEANS_DATA_TELCO_S','in');

insert into PDATA_TELCO_S values (2,'_SYS_AFL.T_KMEANS_TYPE_ASSIGN_TELCO_S','in');

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 20/39


3/13/24, 9:52 PM SAP HANA PAL – K-Means Algorithm or How to do Cust... - SAP Community

insert into PDATA_TELCO_S values (3,'_SYS_AFL.CONTROL_T_TELCO_S','in');

insert into PDATA_TELCO_S values (4,'_SYS_AFL.T_KMEANS_RESULT_TELCO_S','out');

/* Generate the PAL function by calling the AFL Wrapper Generator */

call SYSTEM.afl_wrapper_generator('ValidateTelcoKM','AFLPAL','VALIDATEKMEANS',PDATA_TELCO_S);

Now we should have a procedure in the _SYS_AFL schema called ValidateTelcoKM

Next step is writing the code to execute the validation procedure:

/* This is the table that contains the input parameters */

DROP TABLE #CONTROL_TAB_TELCO_S;

CREATE LOCAL TEMPORARY COLUMN TABLE #CONTROL_TAB_TELCO_S (

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 21/39


3/13/24, 9:52 PM SAP HANA PAL – K-Means Algorithm or How to do Cust... - SAP Community

"Name" VARCHAR(100),

"intArgs" INT,

"doubleArgs" DOUBLE,

"strArgs" VARCHAR(100));

/* Fill the Parameters Table */

INSERT INTO #CONTROL_TAB_TELCO_S VALUES ('VARIABLE_NUM', 8, null, null); --> Number of Attributes used to do
the segmentation

INSERT INTO #CONTROL_TAB_TELCO_S VALUES ('THREAD_NUMBER', 2, null, null); --> Number of threads to be used
during the execution

/* This view shows the result of the clustering process */

DROP VIEW V_KMEANS_TYPE_ASSIGN_TELCO_S;

CREATE VIEW V_KMEANS_TYPE_ASSIGN_TELCO_S AS

SELECT "ID", "CENTER_ASSIGN" AS "TYPE_ASSIGN" FROM PAL_KMEANS_RESASSIGN_TAB_TELCO;

/* This table will contain the silhouette score of the entire data set */

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 22/39


3/13/24, 9:52 PM SAP HANA PAL – K-Means Algorithm or How to do Cust... - SAP Community

DROP TABLE KMEANS_SVALUE_TAB_TELCO_S;

CREATE COLUMN TABLE KMEANS_SVALUE_TAB_TELCO_S (

"NAME" VARCHAR (50),

"S" DOUBLE

);

/* Call the Validate KMeans procedure */

CALL ValidateTelcoKM(LSPARVIERI.TELCO, V_KMEANS_TYPE_ASSIGN_TELCO_S, "#CONTROL_TAB_TELCO_S",


KMEANS_SVALUE_TAB_TELCO_S) with overview;

/* Show the results */

SELECT * FROM KMEANS_SVALUE_TAB_TELCO_S;

The resulting Silhouette Score for my clustering process is:

Since this value is greater than 0 and close enough to 1, it means we used the right number of Ks when running the
clustering process.

https://community.sap.com/t5/technology-blogs-by-members/sap-hana-pal-k-means-algorithm-or-how -to-do-customer-segmentation-for-the/ba-p/12976696/page/2 23/39

You might also like