../ Data Science
-rw-r--r--aguang6.9 KiB6 min read

Association Analysis and Apriori Algorithm

Association analysis is one of the common parts of unsupervised machine learning. In association analysis, our goal is briefly to find things happening together.

Look at this table of people puchasing items:

IDItems
1Milk, Bread
2Butter
3Beer, Tissue
4Milk, Bread, Butter
5Bread

You could probably find that milk and butter are always purchased together.

{Milk}→{Butter}\{Milk\} \to \{\text{Butter}\}

This is what we are tring to do through associate rule mining: exploring a set of data (transactions) and output a kind of descriptive result (association rules).

Basic Concepts

Here are some basic concepts of associate rule mining:

  • Item: a label which appears in some records, such as Milk, Bread,...
  • Itemset: a collection of items, like: {Milk,Bread}\{\text{Milk}, \text{Bread}\}.
  • Association rule: an ordered pair of itemsets, like: {Milk}→{Butter}\{\text{Milk}\} \to \{\text{Butter}\}

For given transactions T={t∣t is an itemset}T = \{ t \mid \text{t is an itemset} \} with nn distinct items, we define:

  • Support count: σ(x)=∣{t∈T∣t property contains x}∣\sigma(x) = \lvert \{t \in T \mid \text{t property contains x}\} \rvert, where xx is a itemset.

And two metrics to evalute how significant a rule r:x→yr: x \to y is:

  • Support: s(x→y)=σ(x∪y)∣T∣s(x \to y) = \frac{\sigma(x \cup y)}{\lvert T \rvert}
  • Confidence: c(x→y)=σ(x∪y)σ(x)c(x \to y) = \frac{\sigma(x \cup y)}{\sigma(x)}

Then, an Association Rule Mining Task could be defined as:

For given transactions TT and thresholds minSupminSup and minConfminConf, find a set of rules:

  • R={x→y∣x∩y=∅∧s(x→y)≥minSup∧c(x→y)≥minConf}R = \{x \to y \mid x \cap y = \empty \land s(x \to y) \geq \text{minSup} \land c(x \to y) \geq \text{minConf} \}

There are 2n×2n 2^{n} \times 2^{n} possibilities of pair xx and yy. Simply enumerating all possible rules could be expotential time complexity and it doesn't work with a growing size of transactions.

As it's easy to know that:

  • ∀r:x→y, if s(x→y)≥minSup,σ(x∪y)∣T∣≥minSup\forall r:x \to y, \text{ if } s(x \to y) \geq \text{minSup}, \frac{\sigma(x \cup y)}{\lvert T \rvert} \geq \text{minSup}

For such kind of a=x∪y∣σ(a)∣T∣≥minSupa = x \cup y \mid \frac{\sigma(a)}{\lvert T \rvert} \geq \text{minSup}, we say aa is a frequent itemset. And it could be better if we calculate all posible frequent itemsets first.

Followed by this idea, the process of association rule mining could be divided into two steps:

  • Find a collection of frequent itemsets I={a∣σ(a)∣T∣≥minSup}I = \{ a \mid \frac{\sigma(a)}{\lvert T \rvert} \geq \text{minSup}\}
  • For each a∈Ia \in I, find all rules {r:x→(a−x)∣c(x→y)≥minConf}\{r : x \to (a - x) \mid c(x \to y) \geq \text{minConf}\}
x Frequent Itemset Generation y Rule Generation x->y z Rule Evaluation y->z

Frequent Itemset Generation

There are many ways to calculate frequent itemsets. One of them is called Apriori algorithm.

This algorithm is based on a pretty simple idea:

  • ∀x⊆y,σ(x)≥σ(y)\forall x \subseteq y, \sigma(x) \geq \sigma(y), where xx, yy are two itemsets

So, if xx isn't a frequent itemset, any super set of xx could not be a frequent item set. Apriori algorithm uses this as prune strategy.

This algorithm starts with an empty set candidate frequent itemsets. Every time, it expands one more item into candidate sets. And prune impossible candidates immediately.

Flow Diagram

a Init x Expand x1 a->x y Count x->y z Prune y->z p Expand xn z->p q Count p->q r Prune q->r b Done r->b

Pseudo code

Apriori(T,minSup)C←∅for x∈{distinct items of T}C←{x}∪I∪{x∪a∣a∈I}C←{a∣a∈C∧σ(x)∣T∣)≥minSup}return C\begin{aligned} & Apriori(T, \text{minSup}) \\ & \quad C \leftarrow \empty \\ & \quad \mathtt{for} \ x \in \{\text{distinct items of T}\} \\ & \quad \quad C \leftarrow \{x\} \cup I \cup \{ {x} \cup a \mid a \in I \}\\ & \quad \quad C \leftarrow \{ a \mid a \in C \land \frac{\sigma(x)}{\lvert T \rvert}) \geq \text{minSup} \} \\ & \quad \mathtt{return} \ C \end{aligned}

Rule Generation

Now, let's turn to the part of rule generation.

As it's easy to know:

  • ∀x,y⊇z,c(x→y)=σ(x)σ(y)≥σ(x∪z)σ(y−z)≥c(x∩z,y−z)\forall x, y \supseteq z, c(x \to y) = \frac{\sigma(x)}{\sigma(y)} \geq \frac{\sigma(x \cup z)}{\sigma(y - z)} \geq c(x \cap z, y - z)

So, for every r:x→yr : x \to y, if c(x→y)≥minConfc(x \to y) \geq \text{minConf}, moving more items from yy to xx doesn't change the requirement of minConf\text{minConf}.

Using this as a prune strategy, we can get a simple rule generation algorithm.

Pseudo Code

RuleGenerate(I)R←∅C←{(∅→a)∣a∈I}while ∣C∣>0Cnext=∅for c:x→y∈Cfor z∈yxnext=x∪{z}ynext=y−{z}if c(xnext→ynext)≥minConfR←R∪{xnext→ynext}elseCnext←Cnext∪{xnext→ynext}C=Cnextreturn R\begin{aligned} & RuleGenerate(I) \\ & \quad R \leftarrow \empty \\ & \quad C \leftarrow \{(\empty \to a) \mid a \in I\} \\ & \quad \mathtt{while} \ \lvert C \rvert > 0 \\ & \quad \quad C_{\text{next}} = \empty \\ & \quad \quad \mathtt{for} \ c: x \to y \in C \\ & \quad \quad \quad \mathtt{for} \ z \in y \\ & \quad \quad \quad \quad x_{\text{next}} = x \cup \{z\} \\ & \quad \quad \quad \quad y_{\text{next}} = y - \{z\} \\ & \quad \quad \quad \quad \mathtt{if} \ c(x_{\text{next}} \to y_{\text{next}}) \geq \text{minConf} \\ & \quad \quad \quad \quad \quad R \leftarrow R \cup \{x_{\text{next}} \to y_{\text{next}}\} \\ & \quad \quad \quad \quad \mathtt{else}\\ & \quad \quad \quad \quad \quad C_{\text{next}} \leftarrow C_{\text{next}} \cup \{x_{\text{next}} \to y_{\text{next}}\} \\ & \quad \quad C = C_{\text{next}}\\ & \quad \mathtt{return} \ R \end{aligned}

Rule Evaluation

Assogication rule mining could produce a huge number of rules. But not all of them could be meaningful. Mostly we need more metrics to evalute generate rueles, which are called interestingness measure.

Drawback of Confidence

Look at this example table of frequency:

yy‾x15520x‾755809010\begin{array}{c|ccc} & y & \overline{y} \\ \hline x & 15 & 5 & 20 \\ \overline{x} & 75 & 5 & 80 \\ & 90 & 10 \\ \end{array}

As we know,

c(x→y)=σ(x∪y)σ(x)=1520=0.75\begin{aligned} c(x \to y) & = \frac{\sigma(x \cup y)}{\sigma(x)} \\ & = \frac{15}{20} \\ & = 0.75 \end{aligned}

since 0.750.75 is sufficienty high, rule x→yx \to y seems to be a pretty significant rule.

But really? P(y)=90100=0.9>0.75P(y) = \frac{90}{100} = 0.9> 0.75, which means XX actually reduces the possibility of YY.

Statistical Dependence

For any xx and yy:

  • If P(x,y)>P(x)×P(y)P(x, y) > P(x) \times P(y), then we say xx and yy are positive related.
  • If P(x,y)<P(x)×P(y)P(x, y) < P(x) \times P(y), then we say xx and yy are negative related.

Interestingness Measure

There are actually manu ways to measure interestingness of association rules, but based on what we've talked before, one simple measure could be (ϕ-coefficient\phi\text{-coefficient}):

ϕ-coefficient=P(x,y)−P(x)P(y)P(x)[1−P(x)]P(y)[1−P(y)] \phi\text{-coefficient} = \frac{P(x, y) - P(x)P(y)}{P(x)[1-P(x)]P(y)[1-P(y)]}

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window