# Apriori Algorithm Visualization & Interpretation in R

I would like to visualize the items that are frequently bought together. Also I would like to know how to interpret the visualization. The dataset is taken from https://www.kaggle.com/sivaram1987/association-rule-learningapriori . The code is as follows

df <- read.transactions(
sep = ",",
rm.duplicates = TRUE
)

set.seed(250)

rules1 <- apriori(df,
parameter = list(supp = 0.004,conf = 0.2))

arules::itemFrequencyPlot(df,
topN = 20,
col=brewer.pal(8,'Pastel2'),
main = 'Relative Item Frequency Plot',
type = 'relative',
ylab = 'Item Frequency'
)

plot(
rules1[1:10],
method = 'matrix',
control = list(reorder='none')#lift
)


The visualized result is as follows.

How to interpret the result?

Here is how you can interpret for matrix-based visualization (refer to https://cran.r-project.org/web/packages/arulesViz/vignettes/arulesViz.pdf):

Formally, the visualized matrix is constructed as follows. We start with the set of association rules $$R = \{, \ldots , \ldots \}$$ where $$a_i$$ is the antecedent (LHS of an association rule), $$c_i$$ is the consequent (RHS of a rule) and $$m_i$$ is the selected interest measure (LIFT e.g.,) for the $$i^{th}$$ rule for $$i = 1, \ldots, n$$.

In R we identify the set of $$K$$ unique antecedents and $$L$$ unique consequent. We create a $$L × K$$ matrix $$M$$ with one column for each unique antecedent and one row for each unique consequent. Finally, we populate the matrix by setting $$M_{lk} = m_i$$ for $$i = 1, \ldots, n$$ and $$l$$ and $$k$$ corresponding to the position of $$a_i$$ and $$c_i$$ in the matrix.

Note that $$M$$ will contain many empty cells since many potential association rules will not meet the required minimum thresholds on support and confidence.

Now, let's first see top 10 association rules the apriori has produced as output:

DATAFRAME(rules1[1:10])


Let's find the unique antecedents (LHS items in the rules)

unique(DATAFRAME(rules1[1:10])[,1])
[1] {}                     {body spray}           {cider}                {nonfat milk}          {pasta}                {extra dark chocolate}
[7] {gums}
Levels: {} {body spray} {cider} {nonfat milk} {pasta} {extra dark chocolate} {gums}


and consequents (RHS items in the rules)

unique(DATAFRAME(rules1[1:10])[,2])
[1] {mineral water} {french fries}  {eggs}          {escalope}      {shrimp}        {spaghetti}
Levels: {mineral water} {french fries} {eggs} {escalope} {shrimp} {spaghetti}


With the above, it's straightforward to interpret the visualization matrix, as shown below:

• the cells represent lifts, color-coded, for association rules $$l \to r$$, if present in the apriori output
• the x axis corresponds to antecedents (LHS items $$l$$) and
• the y axis corresponds to consequents (RHS items $$r$$) corresponding to the association rules.

• what does the {} in LHS imply? Sep 25 at 13:10
• Also how did you extracted the LHS and RHS from rules? Please explain what does [,2] imply in the unique( ). Sep 25 at 13:26
• This is a single item rule, which denotes the probability of the mineral water in an itemset alone, that's why the support and confidence are same for the item, i.e., $P(mineralwater \cap \{\})=P(mineralwater|\{\})$. Sep 25 at 14:13
• 2nd column in the data frame Sep 25 at 14:36