mcruz.me
← All writing

FP-Growth Algorithm for Market Basket Analysis

Six grocery baskets show how a prefix tree compresses transactions, and which counts you need before you can turn a pattern into an association rule.

On this page

Suppose three shoppers buy apples and two of them also buy milk. If a fourth shopper arrives with apples, how much reason do I have to expect milk in their basket? My Java market basket project starts several steps upstream of that question. The input has one grocery item per row. Before I can count a combination, I need to decide which rows belong to the same shopping trip.

Deciding what counts as one basket

The parser groups rows by date and member number. Under that definition, milk bought on Monday and apples bought on Friday belong to separate baskets even if the same person paid for both. Two visits from one member on the same date collapse into a single basket. That choice sets a limit on what the result can tell me about individual trips. Transaction grouping in the CSV parser.

I count support by asking how many baskets contain an item. A shopper can buy three cartons of milk and still contribute one observation of milk. The driver sets a minimum support count of 90 for the grocery data. For a small example, I’ll lower it to two and use six baskets where the arithmetic fits on paper. Support counting and item ordering.

1   bread, milk, apples
2   bread, milk, apples
3   bread, milk
4   bread, apples
5   bread
6   milk

Reading a branch of the tree

Bread appears in five baskets, milk in four, and apples in three. I put the most frequent item first in each basket, then insert the items into a prefix tree. The first two baskets follow exactly the same branch. Inserting basket three increments the bread and milk nodes and stops there. Basket four shares the bread node but gives it a second child, apples. Basket six starts a fresh milk branch at the root.

There are now two milk nodes. The one below bread has count three, which means three baskets began with bread followed by milk in the chosen ordering. The one at the root has count one. Adding them recovers the global milk count of four. Reversing that calculation would lose information. A global count of four tells me nothing about which three baskets also had bread. The branching structure carries that evidence. Prefix insertion in FPTree.

What happens when two items tie?

Ordering matters because the tree only shares prefixes that look identical. In the six-basket example, the support counts happen to rank every item unambiguously. Imagine instead two receipts with identical contents, one recorded as bread then milk and the other as milk then bread. Both items have support two. Ideally both receipts increment the same two nodes. That takes a rule for breaking the tie, such as ordering equal-frequency products by name.

There are two sorts in the Java code. The frequency list starts with a map ordered by product name. When sorting each receipt, however, the implementation builds a fresh map in the receipt’s own order and sorts its entries by frequency alone. A stable sort preserves the original order of a tie. Our two receipts can therefore follow different branches. An index of all nodes named milk still lets me recover milk’s total, but the bread–milk pair is split across two different choices of suffix. With a threshold of two, a miner that treats each orientation separately can drop both one-count pieces. I would give every product a position in the global ordering and use that position for every insertion. The two ordering steps.

Following apples back to the root

My tree keeps an index of nodes by item name. Looking up apples finds two nodes. One has count two and the prefix bread, milk. The other has count one and the prefix bread. These weighted paths are the conditional pattern base for apples. Bread gets a total of three and milk gets two. The pair apples and milk therefore has support two. Reading the association in one direction gives a confidence of two out of three for milk given apples. Reading it in the other direction gives two out of four for apples given milk.

Working that example by hand exposes a problem in my final reporting method. Its printed confidence adds the global counts of the suffix item and one prefix item, then divides by the number of retained baskets. For apples and milk that expression gives seven divided by six. A conditional probability above one is enough to show why the report needs the joint count and the antecedent’s count. Both are available for this pair. The rule-reporting code.

Longer combinations need another step. My code pools prefix-item counts and forms subsets with a power set. Two frequent partners of apples might come from entirely separate baskets. To check whether all three occurred together, I need the weighted paths that still preserve those relationships. Han, Pei, and Yin build and mine conditional trees for that purpose. If I continued the project, I’d use the six original baskets to check each reported combination by direct counting before trusting it on a larger file. The original FP-growth paper.

A basket file that separates pairs from triples

Here’s the smallest change I’d make to that hand check to expose the power-set problem. Start a second file with two apples-and-milk baskets, two apples-and-bread baskets, two bread-and-milk baskets, one basket of bread alone, and one of milk alone. Milk and bread each have five observations, apples four. Keep the same support threshold of two. Both apple pairs still qualify. A trip containing all three products has never occurred in this file.

The apple prefix paths retain enough detail to answer the triple question. The milk path has weight two, and the separate bread path also has weight two. Neither path contains both partners. Once those paths are pooled into a map with just the two item names and totals, that distinction disappears. This is a particularly useful case for a data-mining project because every individual count looks plausible even as the resulting combination changes meaning.

I would also keep the original number of baskets alongside the filtered data. Add a seventh receipt to the first file containing only a rare spice. After filtering, it contributes no tree nodes. The apple-and-milk pair still has two occurrences and the confidence for milk given apples is unchanged. Its share of all shopping trips does change, from two in six to two in seven. Remembering that empty receipt matters whenever a report turns an absolute support count into a percentage.