0% found this document useful (0 votes)
25 views

SAP HANA PAL - K-Means Algorithm or How To Do Cust... - SAP Community-C

The document describes how to use the K-Means clustering algorithm in SAP HANA to segment customers into groups. It includes code to define table types to store the input customer data, cluster centers, and algorithm parameters. The code then generates a K-Means procedure and runs it, passing in sample customer data and settings for number of clusters, initialization method, distance metric, and maximum iterations.

Uploaded by

jefferyleclerc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

SAP HANA PAL - K-Means Algorithm or How To Do Cust... - SAP Community-C

The document describes how to use the K-Means clustering algorithm in SAP HANA to segment customers into groups. It includes code to define table types to store the input customer data, cluster centers, and algorithm parameters. The code then generates a K-Means procedure and runs it, passing in sample customer data and settings for number of clusters, initialization method, distance metric, and maximum iterations.

Uploaded by

jefferyleclerc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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

- SAP Community

/* Table Type that will be used as the input parameter

that will contain the data that I would like to cluster */

DROP TYPE PAL_KMEANS_DATA_TELCO;

CREATE TYPE PAL_KMEANS_DATA_TELCO AS TABLE(

"ID" INT,

"AVG_CALL_DURATION" DOUBLE,

"AVG_NUMBER_CALLS_RCV_DAY" DOUBLE,

"AVG_NUMBER_CALLS_ORI_DAY" DOUBLE,

"DAY_TIME_CALLS" DOUBLE,

"WEEK_DAY_CALLS" DOUBLE,

"CALLS_TO_MOBILE" DOUBLE,

"SMS_RCV_DAY" DOUBLE,

"SMS_ORI_DAY" DOUBLE,

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 7/39


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

primary key("ID")

);

/* Table Type that will be used as the output parameter

that will contain the centers for each cluster */

DROP TYPE PAL_KMEANS_CENTERS_TELCO;

CREATE TYPE PAL_KMEANS_CENTERS_TELCO AS TABLE(

"CENTER_ID" INT,

"V000" DOUBLE,

"V001" DOUBLE,

"V002" DOUBLE,

"V003" DOUBLE,

"V004" DOUBLE,

"V005" DOUBLE,

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 8/39


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

"V006" DOUBLE,

"V007" DOUBLE

);

/* Table Type that will be used to specify

the different parameters to run the KMeans Algorithm */

DROP TYPE PAL_CONTROL_TELCO;

CREATE TYPE PAL_CONTROL_TELCO AS TABLE(

"NAME" VARCHAR (50),

"INTARGS" INTEGER,

"DOUBLEARGS" DOUBLE,

"STRINGARGS" VARCHAR (100)

);

/* This table is used to generate the KMeans procedure

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 9/39


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

and will point the AFL Wrapper Generator to the different

table types that we just created */

DROP TABLE PDATA_TELCO;

CREATE COLUMN TABLE PDATA_TELCO(

"ID" INT,

"TYPENAME" VARCHAR(100),

"DIRECTION" VARCHAR(100) );

/* Fill the table */

INSERT INTO PDATA_TELCO VALUES (1, '_SYS_AFL.PAL_KMEANS_DATA_TELCO', 'in');

INSERT INTO PDATA_TELCO VALUES (2, '_SYS_AFL.PAL_CONTROL_TELCO', 'in');

INSERT INTO PDATA_TELCO VALUES (3, '_SYS_AFL.PAL_KMEANS_RESASSIGN_TELCO', 'out');

INSERT INTO PDATA_TELCO VALUES (4, '_SYS_AFL.PAL_KMEANS_CENTERS_TELCO', 'out');

/* Creates the KMeans procedure that executes the KMeans Algorithm */

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 10/39


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

call SYSTEM.afl_wrapper_generator('PAL_KMEANS_TELCO', 'AFLPAL', 'KMEANS', PDATA_TELCO);

After executing this code we should see a new procedure in the _SYS_AFL schema called PAL_KMEANS_TELCO

Run the K-Means Procedure

I generated the K-Means procedure so now I need to write the code that will execute it:

/* This table will contain the parameters that will be used

during the execution of the KMeans procedure.

For Eexample, the number of clusters you would like to use */

DROP TABLE PAL_CONTROL_TAB_TELCO;

CREATE COLUMN TABLE PAL_CONTROL_TAB_TELCO(

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 11/39


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

"NAME" VARCHAR (50),

"INTARGS" INTEGER,

"DOUBLEARGS" DOUBLE,

"STRINGARGS" VARCHAR (100)

);

/* Fill the parameters table */

INSERT INTO PAL_CONTROL_TAB_TELCO VALUES ('THREAD_NUMBER',2,null,null); --> Number of threads to be used


during the execution

INSERT INTO PAL_CONTROL_TAB_TELCO VALUES ('GROUP_NUMBER',3,null,null); --> Number of clusters

INSERT INTO PAL_CONTROL_TAB_TELCO VALUES ('INIT_TYPE',4,null,null); --> This parameter will specify how to
select the initial center of each cluster

INSERT INTO PAL_CONTROL_TAB_TELCO VALUES ('DISTANCE_LEVEL',2,null,null); --> Which distance to use. In this
case I'm using Euclidean

INSERT INTO PAL_CONTROL_TAB_TELCO VALUES ('MAX_ITERATION',100,null,null); --> Maximum Iterations

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 12/39

You might also like