Apriori Algorithm in R Programming (original) (raw)

Last Updated : 5 Jul, 2025

The Apriori algorithm is used in data mining to find items that often appear together in transaction data, like shopping baskets. It looks for frequent item groups and builds larger groups from smaller ones that are already common. By doing this, it helps discover useful patterns, such as products that are often bought together.

**Apriori Property:

All non-empty subsets of a frequent itemset must be frequent. Apriori assumes that all subsets of a frequent itemset must be frequent (Apriori property). If an itemset is infrequent, all its supersets will be infrequent.

ImportantTerminologies

**Algorithm

  1. Read each item in the transaction.
  2. Calculate the support of every item.
  3. If support is less than minimum support, discard the item. Else, insert it into frequent itemset.
  4. Calculate confidence for each non- empty subset.
  5. If confidence is less than minimum confidence, discard the subset. Else, it into strong rules.

Implementation of Apriori Algorithm in R

We implement the Apriori algorithm in R using the arules package to perform Market Basket Analysis on the preloaded Groceries dataset.

1. Installing required packages

We install the arules, arulesViz, and RColorBrewer packages to use their functions for association rule mining and visualizations.

install.packages("arules") install.packages("arulesViz") install.packages("RColorBrewer")

`

2. Loading required libraries

We load the installed libraries to access their functions and datasets.

library(arules) library(arulesViz) library(RColorBrewer)

`

3. Importing the dataset

We import the Groceries dataset which contains 9835 grocery transactions.

data("Groceries")

`

4. Applying apriori() function

We apply the apriori() function to generate strong association rules from the dataset.

rules <- apriori(Groceries, parameter = list(supp = 0.01, conf = 0.2))

`

**Output:

apriori

Output

5. Inspecting the rules

We inspect the top 10 association rules generated by the algorithm.

inspect(rules[1:10])

`

**Output:

dataset

Output

6. Plotting item frequency

We create a bar plot to visualize the top 20 items with highest frequency.

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

`

**Output:

plot

Output

Application of Apriori Algorithm