Association Rule Mining Action Set
Example 2.1 Training a Model by Using the mbAnalysis Action
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 loaded into CAS. 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 loaded into CAS. 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 loaded into CAS. 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.