Lably
Skip to content

Apriori algorithm

Data-mining algorithm

  • 5 min read
  • 3 chapters

Related on Lably

Through Algorithm, Relational database and Database

Overview

The Apriori algorithm was proposed by Agrawal and Srikant in 1994. Apriori is designed to operate on databases containing transactions (for example, collections of items bought by customers, or details of a website frequentation or IP addresses). Other algorithms are designed for finding association rules in data having no transactions (Winepi and Minepi), or having no timestamps (DNA sequencing). Each transaction is seen as a set of items (an itemset). Given a threshold C, the Apriori algorithm identifies the item sets which are subsets of at least C transactions in the database.

Apriori uses a "bottom up" approach, where frequent subsets are extended one item at a time (a step known as candidate generation), and groups of candidates are tested against the data. The algorithm terminates when no further successful extensions are found.

Apriori uses breadth-first search and a Hash tree structure to count candidate item sets efficiently. It generates candidate item sets of length k from item sets of length k−1. Then it prunes the candidates which have an infrequent sub pattern. According to the downward closure lemma, the candidate set contains all frequent k-length item sets. After that, it scans the transaction database to determine frequent item sets among the candidates.

The pseudo code for the algorithm is given below for a transaction database T, and a support threshold of ε. Usual set theoretic notation is employed, though note that T is a multiset. Ck is the candidate set for level k. At each step, the algorithm is assumed to generate the candidate sets from the large item sets of the preceding level, heeding the downward closure lemma. count[c] accesses a field of the data structure that represents candidate set c, which is initially assumed to be zero. Many details are omitted below, usually the most important part of the implementation is the data structure used for storing the candidate sets, and counting their frequencies.

<b id="mwMA">Apriori</b>(T, ε) L<sub id="mwMQ">1</sub> ← {large singleton itemsets} k ← 2 <b id="mwMg">while</b> L<sub id="mwMw">k−1</sub> <b id="mwNA">is not</b> empty C<sub id="mwNQ">k</sub> ← Generate_candidates(L<sub id="mwNg">k−1</sub>, k) <b id="mwNw">for</b> transactions t <b id="mwOA">in</b> T D<sub id="mwOQ">t</sub> ← {c in C<sub id="mwOg">k</sub> : c ⊆ t} <b id="mwOw">for</b> candidates c <b id="mwPA">in</b> D<sub id="mwPQ">t</sub> count[c] ← count[c] + 1 L<sub id="mwPg">k</sub> ← {c in C<sub id="mwPw">k</sub> : count[c] ≥ ε} k ← k + 1 <b id="mwQA">return</b> Union(L<sub id="mwQQ">k</sub>) <b id="mwQg">over all</b> k <b id="mwQw">Generate_candidates</b>(L, k) result ← empty_set() <b id="mwRA">for all</b> p ∈ L, q ∈ L <b id="mwRQ">where</b> p and q differ in exactly one element c ← p ∪ q <b id="mwRg">if</b> u ∈ L <b id="mwRw">for all</b> u ⊆ c <b id="mwSA">where</b> |u| = k-1 result.add(c) <b id="mwSQ">return</b> result

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication