1
$\begingroup$

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(
  'C:\\Users\\write\\Documents\\R\\data\\Market_Basket_Optimisation.csv',
  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.

enter image description here

How to interpret the result?

$\endgroup$
0
$\begingroup$

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 = \{<a_1, c_1, m_1>, \ldots <a_i, c_i, m_i>, \ldots <a_n, c_n, m_n>\}$ 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])

enter image description here

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.

enter image description here

$\endgroup$
4
  • $\begingroup$ what does the {} in LHS imply? $\endgroup$ Sep 25 at 13:10
  • $\begingroup$ Also how did you extracted the LHS and RHS from rules? Please explain what does [,2] imply in the unique( ). $\endgroup$ Sep 25 at 13:26
  • $\begingroup$ 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|\{\})$. $\endgroup$ Sep 25 at 14:13
  • $\begingroup$ 2nd column in the data frame $\endgroup$ Sep 25 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.