Association Rule Mining Action Set

Training a Model by Using the mbAnalysis Action

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Visual Data Mining and Machine Learning: Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

This example trains the model by using data that are available in the sampsio.assocs data set. This data set contains 1,000 transactions of 1,000 customers. Each observation contains information about the customer in the Customer variable, information about the product in the Product variable (which is the target variable), and information about time in the Time variable (which is ignored). To perform association discovery, the input data table must have a separate observation for each product that was purchased by each customer. You must assign the Customer (ID) role to one variable and the target model role to another variable when you create the data table. Only one variable is supported as the Customer variable, and only one variable is supported as the target variable.

You can load the sampsio.assocs data set into your CAS session by specifying your CAS engine libref in the first statement in the following DATA step. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.assocs;
   set sampsio.assocs;
run;

The following DATA steps create two level hierarchies: firstLevel and secondLevel.

data mycas.firstLevel;
  length Item $20 Category $20;
    Item='apples'; Category='Fruits'; output;
    Item='artichok'; Category='Vegetables'; output;
    Item='avocado'; Category='Fruits'; output;
    Item='baguette'; Category='Bread'; output;
    Item='bordeaux'; Category='Wine'; output;
    Item='bourbon'; Category='Whisky'; output;
    Item='chicken'; Category='Non-Veg'; output;
    Item='coke'; Category='Cold-Drink'; output;
    Item='corned_b'; Category='Non-Veg'; output;
    Item='cracker'; Category='Vegetables'; output;
    Item='ham'; Category='Non-Veg'; output;
    Item='heineken'; Category='Beer'; output;
    Item='hering'; Category='Non-Veg'; output;
    Item='ice_crea'; Category='Cold-Drink'; output;
    Item='olives'; Category='Vegetables'; output;
    Item='peppers'; Category='Fruits'; output;
    Item='sardines'; Category='Non-Veg'; output;
    Item='soda'; Category='Cold-Drink'; output;
    Item='steak'; Category='Non-Veg'; output;
    Item='turkey'; Category='Non-Veg'; output;
 run;

data mycas.secondLevel;
  length Category $20 Dept $20;
    Category='Fruits'; Dept='Breakfast'; output;
    Category='Vegetables'; Dept='Breakfast'; output;
    Category='Bread'; Dept='Breakfast'; output;
    Category='Wine'; Dept='Drinks'; output;
    Category='Whisky'; Dept='Drinks'; output;
    Category='Non-Veg'; Dept='Breakfast'; output;
    Category='Cold-Drink'; Dept='Drinks'; output;
    Category='Beer'; Dept='Drinks'; output;
 run;

The following statements run an association analysis on the mycas.assocs data table by using the mbAnalysis action:


 proc cas;
     action ruleMining.mbanalysis /
         hierarchy={{name="firstLevel"}, {name="secondLevel"}}
         supmin=1 items=8 minitems=2 maxitems=10
         nLHS_range={Lower=2 UPPER=3} nRHS_range={Lower=4 UPPER=5}
         separator = "*" antecedentList={"Fruits"} consequentList={"Non-Veg" }
         idVariable="Customer"
         out={name="out", replace=true}
         outrule={name="outrule", replace=true}
         outfreq={name="outfreq", replace=true}
         table={name="assocs"}
         tgtVariable="Product"
         savestate= {name="mystateArm", replace=true} ;
 run;
 quit;

The table parameter names the input data table to be analyzed. The supmin parameter specifies the minimum level of support for a rule. The items parameter specifies the number of items in a rule. The minitems parameter specifies the minimum basket size to be analyzed, and the maxitems parameter specifies the maximum basket size to be analyzed. The nLHS_range parameter specifies the range of items in the left-hand side (LHS) of a rule, and the nRHS_range parameter specifies the range of items in the right-hand side (RHS) of a rule. The separator parameter specifies the separator character in the antecedent (left-hand side) or consequent (right-hand side) of a rule. The default value is "&". The antecedentList parameter specifies the list of regular expression strings to match in the antecedent of a rule. The consequentList parameter specifies the list of regular expression strings to match in the consequent of a rule. The hierarchy parameter specifies items’ hierarchies (also called groups or categories). The idVariable parameter specifies the variable that is used to group the target variable into baskets. The tgtVariable parameter specifies a single categorical variable to use as the target variable. The out parameter specifies the table to contain frequent item sets to generate rules. The outfreq parameter specifies the table to contain unique frequent items along with their transaction counts and support. The outrule parameter specifies the table to contain rules along with their support, confidence, and lift.

Training a Model by Using the mbAnalysis Action

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the basket data to the comma-separated-value (CSV) file basket.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="basket.csv"}

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

To use the mbAnalysis action to perform association discovery on the Basket data table, first create the data tables that you need.

You can create the Basket data table by copying the following rows of data from the basket.csv file:


Customer,Item
Brian,Low Fat Milk
Brian,Feta Cheese
Brian,Cake
Brian,Pizza
Brian,Ice Cream
Brian,Pancakes
Ikea,Low Fat Milk
Ikea,Edam Cheese
Ikea,Pizza
Ikea,Ice Cream
Juan's,Skim Milk
Juan's,Edam Cheese
Juan's,Ice Cream
Juan's,Cake

You can create the Category data table by copying the following rows of data from the category.csv file:


Item,Category
Whole Milk,Milk
Low Fat Milk,Milk
Skim Milk,Milk
Feta Cheese,Cheese
Edam Cheese,Cheese
Waffles,Breakfast
Pancakes,Breakfast
Pizza,Dinner
Ice Cream,Dessert
Cake,Dessert

You can create the Dept data table by copying the following rows of data from the dept.csv file:


Category,Department
Milk,Dairy Products
Cheese,Dairy Products
Breakfast,Frozen Foods
Dinner,Frozen Foods
Dessert,Frozen Foods

Then you can use the following code to load the ruleMining action set and use the mbAnalysis action to perform discovery:

s:loadactionset{actionset="ruleMining"}
s:mbanalysis{
 hierarchy={hierarchy1="Category", hierarchy2="Dept"},
 idVariable="Customer", tgtVariable="Item", items=8, supmin=2, minitems=1,
 maxitems=10, out={name="out1", replace=true},table={name="basket"},
 nLHS_range={Lower=2, UPPER=3}, nRHS_range={Lower=4, UPPER=5},
 separator = "*", antecedentList={"Fruits"}, consequentList={"Non-Veg" },
 outfreq={name="outfreq1", replace=true},
 outrule={name="outrule1", replace=true},savestate={name="mystateArm", replace=true}
 }

The table parameter names the input data table to be analyzed. The supmin parameter specifies the minimum level of support for a rule. The items parameter specifies the number of items in a rule. The minitems parameter specifies the minimum basket size to be analyzed, and the maxitems parameter specifies the maximum basket size to be analyzed. The nLHS_range parameter specifies the range of items in the left-hand side (LHS) of a rule. The nRHS_range parameter specifies the range of items in the right-hand side (RHS) of a rule. The separator parameter specifies the separator character in antecedents or consequents in a rule. The default value is "&". The antecedentList parameter specifies the list of regular expression strings to match in antecedent of a rule. The consequentList parameter specifies the list of regular expression strings to match in consequent of a rule. The hierarchy parameter specifies items’ hierarchies (also called groups or categories). The idVariable parameter specifies the variable that is used to group the target variable into baskets. The tgtVariable parameter specifies a single categorical variable to use as the target variable. The out parameter specifies the table to contain frequent item sets to generate rules. The outfreq parameter specifies the table to contain unique frequent items along with their transaction counts and support. The outrule parameter specifies the table to contain rules along with their support, confidence, and lift. The savestate parameter saves the state of the action in an analytic store, mystateArm, which is a binary file that contains the action’s state after the action completes the training phase of data analysis.

Training a Model by Using the mbAnalysis Action

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the basket data to the comma-separated-value (CSV) file basket.csv and then use the following code to load the CSV file into CAS:

s.upload_file('basket.csv')

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

To use the mbAnalysis action to perform association discovery on the Basket data table, first create the data tables that you need.

You can create the Basket data table by copying the following rows of data from the basket.csv file:

Customer,Item
Brian,Low Fat Milk
Brian,Feta Cheese
Brian,Cake
Brian,Pizza
Brian,Ice Cream
Brian,Pancakes
Ikea,Low Fat Milk
Ikea,Edam Cheese
Ikea,Pizza
Ikea,Ice Cream
Juan's,Skim Milk
Juan's,Edam Cheese
Juan's,Ice Cream
Juan's,Cake

You can create the Category data table by copying the following rows of data from the category.csv file:

Item,Category
Whole Milk,Milk
Low Fat Milk,Milk
Skim Milk,Milk
Feta Cheese,Cheese
Edam Cheese,Cheese
Waffles,Breakfast
Pancakes,Breakfast
Pizza,Dinner
Ice Cream,Dessert
Cake,Dessert

You can create the Dept data table by copying the following rows of data from the dept.csv file:

Category,Department
Milk,Dairy Products
Cheese,Dairy Products
Breakfast,Frozen Foods
Dinner,Frozen Foods
Dessert,Frozen Foods

Then you can use the following code to load the ruleMining action set and use the mbAnalysis action to perform discovery:

s.loadactionset("ruleMining")

s.rulemining.mbanalysis(
hierarchy=[{'name':'Category'},
{'name':'Dept'}],
idVariable="Customer", tgtVariable="Item",
items=8, supmin=2,minitems=1, maxitems=10,
nLHS_range={'Lower':2, 'UPPER':3}, nRHS_range={'Lower':4, 'UPPER':5},
separator = "*", antecedentList={"Fruits"}, consequentList={"Non-Veg" },
savestate={'name':'myarmState'},
out={'name':'out1'},
table={'name':'basket'},
outfreq={'name':'outfreq1'},
outrule={'name':'outrule1'})

The table parameter names the input data table to be analyzed. The supmin parameter specifies the minimum level of support for a rule. The items parameter specifies the number of items in a rule. The minitems parameter specifies the minimum basket size to be analyzed, and the maxitems parameter specifies the maximum basket size to be analyzed. The nLHS_range parameter specifies the range of items in the left-hand side (LHS) of a rule. The nRHS_range parameter specifies the range of items in the right-hand side (RHS) of a rule. The separator parameter specifies the separator character in antecedents or consequents in a rule. The default value is "&". The antecedentList parameter specifies the list of regular expression strings to match in antecedent of a rule. The consequentList parameter specifies the list of regular expression strings to match in consequent of a rule. The hierarchy parameter specifies items’ hierarchies (also called groups or categories). The idVariable parameter specifies the variable that is used to group the target variable into baskets. The tgtVariable parameter specifies a single categorical variable to use as the target variable. The savestate parameter saves the state of the action in an analytic store, myarmState, which is a binary file that contains the action’s state after the action completes the training phase of data analysis. The out parameter specifies the table to contain frequent item sets to generate rules. The outfreq parameter specifies the table to contain unique frequent items along with their transaction counts and support. The outrule parameter specifies the table to contain rules along with their support, confidence, and lift.

Training a Model by Using the mbAnalysis Action

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the assocs data to the comma-separated-value (CSV) file assocs.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "assocs.csv", casOut=list(name="assocs"))

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

To use the mbAnalysis action to perform association discovery on the assocs data table, first create the data tables that you need.

You can create the firstlevel data table by copying the following rows of data from the firstlevel.csv file:

Item,Category
apples,Fruits
artichok,Vegetables
avocado,Fruits
baguette,Bread
bordeaux,Wine
bourbon,Whisky
chicken,Non-Veg
coke,Cold-Drink
corned_b,Non-Veg
cracker,Vegetables
ham,Non-Veg
heineken,Beer
hering,Non-Veg
ice_crea,Cold-Drink
olives,Vegetables
peppers,Fruits
sardines,Non-Veg
soda,Cold-Drink
steak,Non-Veg
turkey,Non-Veg

You can create the secondlevel data table by copying the following rows of data from the secondlevel.csv file:

Category,Dept
Fruits,Breakfast
Vegetables,Breakfast
Bread,Breakfast
Wine,Drinks
Whisky,Drinks
Non-Veg,Breakfast
Cold-Drink,Drinks
Beer,Drinks

Then you can use the following code to load the ruleMining action set and use the mbAnalysis action to perform discovery:

data <- cas.read.csv(s,
                  "assocs.csv",
                  header = TRUE,
                  casOut = list(name = "assocs",replace = TRUE))

data1 <- cas.read.csv(s,
                  "firstlevel.csv",
                  header = TRUE,
                  casOut = list(name = "firstlevel",replace = TRUE))

data2 <- cas.read.csv(s,
                  "secondlevel.csv",
                  header = TRUE,
                  casOut = list(name = "secondlevel",replace = TRUE))

loadActionSet(s,'ruleMining')
result <- cas.ruleMining.mbanalysis(s,
                  hierarchy   = c("firstLevel","secondLevel"),
                  supmin      = 200,
                  items       = 8,
                  minitems    = 2,
                  maxitems    = 10,
                  nLHS_range= list(Lower=2, UPPER=3),
                  nRHS_range= list(Lower=4, UPPER=5),
                  separator = "*",
                  antecedentList= list(Fruits"),
                  consequentList= list(Non-Veg"),
                  idVariable  = "Customer",
                  out         = list(name = "out", replace="true"),
                  outrule     = list(name = "outrule", replace="true"),
                  outfreq     = list(name = "outfreq", replace="true"),
                  table       = list(name = "assocs"),
                  tgtVariable = "Product",
                  savestate   = "mystateArm")

The table parameter names the input data table to be analyzed. The supmin parameter specifies the minimum level of support for a rule. The items parameter specifies the number of items in a rule. The minitems parameter specifies the minimum basket size to be analyzed, and the maxitems parameter specifies the maximum basket size to be analyzed. The nLHS_range parameter specifies the range of items in the left-hand side (LHS) of a rule. The nRHS_range parameter specifies the range of items in the right-hand side (RHS) of a rule. The separator parameter specifies the separator character in antecedents or consequents in a rule. The default value is "&". The antecedentList parameter specifies the list of regular expression strings to match in antecedent of a rule. The consequentList parameter specifies the list of regular expression strings to match in consequent of a rule. The hierarchy parameter specifies items’ hierarchies (also called groups or categories). The idVariable parameter specifies the variable that is used to group the target variable into baskets. The tgtVariable parameter specifies a single categorical variable to use as the target variable. The savestate parameter saves the state of the action in an analytic store, mystateArm, which is a binary file that contains the action’s state after the action completes the training phase of data analysis. The out parameter specifies the table to contain frequent item sets to generate rules. The outfreq parameter specifies the table to contain unique frequent items along with their transaction counts and support. The outrule parameter specifies the table to contain rules along with their support, confidence, and lift.

Last updated: November 04, 2020