Getting started¶
This tutorial walks you through installing fpgrowth, mining a small example
database from the command line, and then doing the same thing from Python. It
takes a few minutes and assumes only a working Python 3.7+ interpreter.
1. Get the code¶
Clone the repository and enter it:
2. Install the package¶
This installs the fpgrowth package and the fpgrowth console script. If you
prefer not to install anything, you can run the tool straight from the checkout
by prefixing commands with PYTHONPATH=src and using python3 -m fpgrowth.
3. Look at the example database¶
The repository ships a tiny worked example in data.txt:
The first line says there are four transactions. Each following line is tab-separated: a transaction id, an item count, and the items. The file formats reference documents this precisely.
4. Mine frequent itemsets¶
Run the miner with a minimum support of 50%:
You should see progress messages and a final count, then the results written to
results.txt:
Each line is <items> : <support>. For example, 2 5 : 3 means the itemset
{2, 5} appears in three of the four transactions. At a 50% threshold, an
itemset must appear in at least two transactions.
5. Mine the same database from Python¶
The command line is a thin wrapper around the library. Do the same work in a REPL or script:
from fpgrowth import mine_frequent_itemsets
transactions = [
{1, 3, 4},
{2, 3, 5},
{1, 2, 3, 5},
{2, 5},
]
patterns = mine_frequent_itemsets(transactions, min_support=0.5)
for itemset in sorted(patterns, key=lambda s: (len(s), sorted(s))):
print(sorted(itemset), "->", patterns[itemset])
The dictionary keys are frozensets and the values are absolute support counts.
mine_frequent_itemsets returned the same nine itemsets you saw in the file.
6. Try changing the threshold¶
Lower the threshold to 25% (an absolute support of one transaction):
Now item 4, which appears only once, becomes frequent and more itemsets
appear. Raising -m toward 1.0 restricts the output to itemsets shared by
every transaction.
Next steps¶
- Turn common tasks into recipes in the how-to guides.
- Learn the exact API in the reference.
- Understand why the algorithm works in How FP-Growth works.