Apriori Algorithm

Machine Learning
Introduced by
Agrawal, R. & Srikant, R.
Year
1994
Type
Frequent itemset and association rule mining algorithm
Family
Machine learning

What does it do?

The Apriori algorithm, introduced by Agrawal and Srikant in 1994, is a standard method for finding frequent itemsets and association rules in transactional data.

It searches level by level and uses the anti-monotone property of support to skip item combinations that cannot reach a user-set minimum support. The frequent itemsets are then converted into if-then rules that are scored by confidence and lift.

The intuition behind the method

Consider a supermarket receipt database. Apriori relies on the anti-monotone property: if a pair of products is rarely bought together, no larger set containing that pair can be frequent. The algorithm therefore builds candidates level by level, starting with single items, then pairs, then triples. Before counting a candidate in the data, it checks whether all of its subsets were frequent at the previous level, and discards the candidate if they were not. This pruning can avoid counting many candidate itemsets, and how many depends on the data and the minimum support.

History

Agrawal and Srikant introduced the Apriori algorithm at VLDB in 1994. The name reflects the algorithm's use of prior knowledge of frequent k-itemsets to prune candidates at level k+1. Later frequent-itemset methods, including FP-Growth and Eclat, are commonly compared against it.

When to use it

Use Apriori when the goal is to find co-occurrence patterns or if-then relationships in transactional or binary item-presence data. Typical examples are market basket analysis, co-endorsement of survey items, co-occurrence of clinical codes, and association of text terms. It suits moderate-sized datasets where the output needs to be transparent and easy to inspect. Do not use it on very large transaction databases with wide item catalogs, where repeated full scans make it slow; FP-Growth is the usual alternative in that case. It is also unsuitable for continuous numeric variables that have not been discretized, and it cannot establish direction or causation.

Limitations and considerations

  • Each level requires a full pass over the transaction database to count candidates, so runtime grows with database size; FP-Growth is often preferred at scale.
  • A low minimum support produces a large number of candidate itemsets and rules, which becomes hard to read and to validate.
  • The method accepts only binary item-presence data, so continuous variables must be discretized beforehand.
  • Rules are written as antecedent to consequent, but support, confidence and lift are estimated from co-occurrence counts. They carry no time order and no causal direction.
  • The results depend on the minimum support and confidence thresholds, which must be set through domain knowledge or systematic trial.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Each output rule is an explicit if-then statement with support, confidence and lift that can be checked against the underlying counts.
  • The anti-monotone pruning lets Apriori skip counting many candidate itemsets, which keeps an otherwise exponential search manageable on moderate-sized data.
  • No labelled outcome is needed; the method works on binary item-presence data alone.
  • Minimum support and minimum confidence give the analyst direct control over how many rules are returned.
  • The original 1994 paper by Agrawal and Srikant is the reference point for later frequent-itemset methods such as FP-Growth, which makes the algorithm a useful baseline.

Applications

  • Market basket analysis: finding products that are frequently purchased together, to inform shelf layout and promotions.
  • Medical informatics: identifying symptoms, diagnoses or prescriptions that co-occur in patient records.
  • Web usage mining: finding pages or features that users tend to visit together.
  • Survey and questionnaire analysis: detecting items or scale responses that are systematically endorsed together.
  • Recommender system bootstrapping: generating candidate item associations before a collaborative filtering model is deployed.
  • Fraud review support: Apriori finds frequent co-occurrences rather than anomalies, so its rules can serve only as one input to a separate anomaly-detection step designed by the analyst.

How it works

How it works

1

Before running the algorithm, the analyst chooses two thresholds. Minimum support is the fraction of transactions that must contain an itemset for it to count as frequent. Minimum confidence is the conditional proportion required for an if-then rule to be kept. Both values control how many patterns are returned, and they are usually set using domain knowledge or trial runs.

2

The algorithm makes one full pass over the transaction database to count how often each individual item occurs. Items whose support meets or exceeds the minimum support are kept, and the rest are discarded. The surviving single items form the first level of frequent itemsets.

Formula
\text{support}(X) = \frac{|\{t \in T : X \subseteq t\}|}{|T|}
3

Frequent itemsets of size k are joined pairwise to form candidate itemsets of size k+1. Two itemsets are joined only if they share their first k-1 items, which keeps the candidate set smaller. A new pass over the database then counts the support of each candidate.

4

Before any counting, each candidate of size k+1 is checked against its k-subsets. If any of these subsets was not frequent at level k, the candidate is removed without reading the data. This uses the anti-monotone property to reduce the computation. The remaining candidates are the frequent itemsets of size k+1.

5

For each frequent itemset of size two or more, every split of its items into an antecedent and a consequent gives a candidate rule. Each rule is scored by confidence, which is the proportion of transactions containing the antecedent that also contain the consequent. Rules below the minimum confidence are discarded.

Formula
\text{confidence}(X \Rightarrow Y) = \frac{\text{support}(X \cup Y)}{\text{support}(X)}
6

Remaining rules are ranked by lift, which divides the rule's confidence by the baseline support of the consequent. A lift above 1 means the items co-occur more often than they would if they were independent. A lift near 1 indicates little association beyond the item frequencies, and a lift below 1 indicates that the items co-occur less often than expected.

Formula
\text{lift}(X \Rightarrow Y) = \frac{\text{confidence}(X \Rightarrow Y)}{\text{support}(Y)}

Common pitfalls

  • Setting minimum support too low, which produces a very large number of rules, and failing to filter them by lift.
  • Reading high confidence as causation. When lift is near 1, high confidence mainly reflects how common the consequent is in the data.
  • Applying Apriori to continuous features without discretizing them first, which inflates the item space and degrades results.
  • Reporting rules without checking the number of transactions behind them. For rare itemsets, a small number of transactions can change the support estimate substantially.
  • Keeping uninformative rules, such as those involving items that nearly every transaction contains (for example a store loyalty card).

Frequently asked questions

What is the difference between support, confidence, and lift?

Support is the fraction of all transactions that contain the itemset, and it measures how common the pattern is. Confidence is the proportion of transactions containing the antecedent that also contain the consequent, and it measures how reliable the rule is. Lift divides confidence by the baseline frequency of the consequent. A lift above 1 means the items co-occur more often than if they were independent, but it does not by itself establish that the relationship is genuine, since sampling noise and small counts can also produce high values.

When should I choose FP-Growth over Apriori?

FP-Growth compresses the database into a tree structure and mines it without repeated full scans, so it is often faster on large datasets or when minimum support is low. Apriori is simpler to understand and implement, and it is adequate on small to moderate datasets where runtime is not a constraint.

How do I choose minimum support and confidence?

No universal value exists. A practical approach is to run the algorithm at several support levels, compare how many frequent itemsets and rules each run returns, and pick a level at which the output becomes manageable. Confidence should then be set according to the research or business question, and the top rules should be checked against domain knowledge.

Can Apriori handle numeric or continuous data?

Apriori requires binary item-presence data. Continuous variables must be discretized into bins (e.g., age into low/medium/high) before encoding as items. Poor discretization can severely distort the discovered rules, so binning strategy deserves careful thought.

Does Apriori imply causation between items?

No. Association rules capture co-occurrence, not causation or direction. A rule 'bread → butter' means they often appear in the same basket; it does not mean buying bread causes someone to buy butter, nor that the relationship is asymmetric in any causal sense.

Related methods

Sources

  1. Agrawal, R. & Srikant, R. (1994). Fast algorithms for mining association rules. Proceedings of the 20th International Conference on Very Large Data Bases (VLDB), 487–499.

    Open publication reference
  2. Apriori algorithm. Wikipedia.

    Open publication reference