Apriori Association Rule Mining in MATLAB
Mine association rules in MATLAB with Apriori, minimum support and confidence, and reported support, confidence, and lift for each rule.

On this page
Association rule mining is a common data mining task. It finds frequent patterns, usually in transactional databases. For example, the rule {bread, butter} → {milk} in a sales dataset means that a customer who buys bread and butter is likely to buy milk too. You can read more in the Wikipedia article on association rule learning (opens in a new tab).
One of the basic methods for this problem is the Apriori algorithm, proposed by Rakesh Agrawal and Ramakrishnan Srikant in 1994. It starts by identifying frequent individual items in the dataset. Then it extends them to larger item sets, as long as those item sets appear often enough in the database to give useful rules. You can read more in the Wikipedia article on the Apriori algorithm (opens in a new tab).
This post shares a MATLAB implementation of Apriori association rule mining. The code is structured, so if you know MATLAB, you will find it easy to use in your research projects.
The problem
The data is a set of transactions. Each transaction is a set of items, for example the products in one shopping basket. A rule links two disjoint item sets: when a transaction contains , it often contains as well. Three standard measures describe how useful a rule is.
Support is the share of transactions that contain all the items of the rule:
Confidence is the share of transactions with that also contain . It estimates the conditional probability of given :
Lift compares the confidence with the overall frequency of :
A lift above 1 means that and co-occur more often than expected under independence using the observed frequencies. A lift of 1 matches that independence baseline in the sample.
How Apriori works
Apriori uses one simple fact: every subset of a frequent item set is also frequent. So a large item set can only be frequent if it is built from smaller frequent item sets. The algorithm works level by level:
- Count every single item and keep those whose support is at least the minimum support threshold.
- Build candidate item sets of size from the frequent item sets of size , count them in the data, and keep the frequent ones.
- Repeat until no new frequent item set is found.
- For every frequent item set with two or more items, split it in all possible ways into and , and compute the support, confidence and lift of each rule .
In this code, step 2 adds one more item to each frequent item set, using only the items that appear in the frequent sets of the current level. The rules are sorted by confidence, and the final rules are those with a confidence of at least the minimum confidence threshold and a lift of at least 1.
How to run it
Run main.m in MATLAB. It loads the transactions T from mydata.mat, calls the main function, and prints the final rules with their support, confidence and lift:
MST=0.22; % Minimum Support Threshold
MCT=0.6; % Minimum Confidence Threshold
[FinalRules, Rules]=Apriori(T,MST,MCT);The sample data has nine transactions over five items, numbered 1 to 5. T is a cell array, and each cell holds the item numbers of one transaction, for example [1 2 5]. To mine your own data, store your transactions in the same form and adjust the two thresholds. Rules holds all the rules before the final selection.
The download has these files:
Apriori.m: the algorithm.main.m: the example script.DisplayRules.m: prints the rules.GetNonTrivialSubsets.mandIsSame.m: helper functions.mydata.mat: the sample transactions.
Check the result
Use one occurrence of each item per transaction and keep item identifiers consistent across the data. The nine-transaction example illustrates counting rather than evidence for a business decision. Review support counts as well as confidence and lift, then check promising rules on separate transactions. High confidence can come from a common consequent, and association does not establish causation.
Applications
Association rules are used in market basket analysis, product recommendations and store layout, and also in web usage mining, the analysis of medical records and the study of other kinds of event data.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Apriori Association Rule Mining in MATLAB (YPML115), Yarpiz, 2015. https://yarpiz.com/apriori-ypml115@misc{yarpiz-ypml115,
author = {{Mostapha Kalami Heris}},
title = {{Apriori Association Rule Mining in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPML115},
url = {https://yarpiz.com/apriori-ypml115}
}Published Updated
Comments