API reference¶
The package exposes its public API from the top-level fpgrowth namespace:
from fpgrowth import (
FPNode,
FPTree,
mine_frequent_itemsets,
parse_transaction_line,
read_database,
read_transactions,
format_itemset,
write_frequent_itemsets,
)
Mining¶
mine_frequent_itemsets ¶
mine_frequent_itemsets(transactions: Iterable[Iterable[Hashable]], min_support: float = 0.5, *, absolute: bool = False) -> Dict[FrozenSet[Hashable], int]
Mine every frequent itemset in transactions with FP-Growth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transactions
|
Iterable[Iterable[Hashable]]
|
An iterable of transactions, each an iterable of items. Duplicate items within a transaction are ignored. Items may be any hashable objects, though integers and strings are typical. |
required |
min_support
|
float
|
The minimum support threshold. By default this is a
relative frequency in |
0.5
|
absolute
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
Dict[FrozenSet[Hashable], int]
|
A dictionary mapping each frequent itemset (a :class: |
Dict[FrozenSet[Hashable], int]
|
its absolute support count. Only non-empty itemsets are returned. An |
Dict[FrozenSet[Hashable], int]
|
empty dictionary means no itemset met the threshold. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> mine_frequent_itemsets([{1, 2}, {1, 3}, {1, 2, 3}], 2 / 3, absolute=True)
{frozenset({1}): 3, frozenset({3}): 2, frozenset({1, 3}): 2, frozenset({2}): 2, frozenset({1, 2}): 2}
Source code in src/fpgrowth/core.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
Tree types¶
FPTree ¶
A prefix tree over transactions with per-item node-link chains.
Build a tree by calling :meth:add_transaction for every transaction.
Each transaction must already be sorted in the global item order
(conventionally descending support, with ties broken deterministically).
Applying one consistent order to every transaction is what allows
transactions to share prefixes; it is also what makes the conditional
trees produced during mining correct.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
The root :class: |
|
header |
Dict[Hashable, FPNode]
|
Mapping from item to the first node of that item's link chain. |
Source code in src/fpgrowth/core.py
add_transaction ¶
Insert a single transaction into the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[Hashable]
|
Items already sorted in the global item order. |
required |
count
|
int
|
Weight of the transaction. |
1
|
Source code in src/fpgrowth/core.py
nodes ¶
Iterate over every node in the tree labelled with item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Hashable
|
The item to search for. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
Each |
FPNode
|
class: |
FPNode
|
node links. Yields nothing when the item is absent from the tree. |
Source code in src/fpgrowth/core.py
support ¶
Return the total count of every node labelled with item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Hashable
|
The item to measure. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The summed |
int
|
not present. |
Source code in src/fpgrowth/core.py
prefix_paths ¶
Return the prefix paths of item.
A prefix path is the sequence of nodes from the root down to a node
labelled item (the root is excluded, item's node is the last
element). One path is returned per occurrence of item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Hashable
|
The item whose prefix paths are wanted. |
required |
Returns:
| Type | Description |
|---|---|
List[List[FPNode]]
|
A list of paths, each a list of :class: |
List[List[FPNode]]
|
root's child down to the matching node. |
Source code in src/fpgrowth/core.py
FPNode ¶
A single node of an FP-tree.
A node stores an item label, the number of transactions routed through it, a pointer to its parent, a mapping of its children keyed by item, and a link to the next node in the tree that carries the same item. The chain of same-item links is called the node links; it lets the mining routine visit every occurrence of an item without walking the entire tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Hashable
|
The item label stored in this node. The root uses |
required |
count
|
int
|
Number of transactions routed through this node. |
1
|
parent
|
Optional['FPNode']
|
The parent node, or |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
item |
The item label stored in this node. |
|
count |
Number of transactions routed through this node. |
|
parent |
The parent node, or |
|
children |
Dict[Hashable, 'FPNode']
|
Mapping from item label to the child :class: |
link |
Optional['FPNode']
|
Next node in the same-item link chain, or |
Source code in src/fpgrowth/core.py
Reading and writing¶
read_database ¶
read_database(path: str, item_parser: Callable[[str], Hashable] = int) -> Tuple[int, List[FrozenSet[Hashable]]]
Read a transaction database file in full.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the transaction database file. |
required |
item_parser
|
Callable[[str], Hashable]
|
Callable converting each item token to an item. |
int
|
Returns:
| Type | Description |
|---|---|
int
|
A tuple |
List[FrozenSet[Hashable]]
|
integer read from the first line; |
Tuple[int, List[FrozenSet[Hashable]]]
|
parsed transactions. The two are not required to agree. |
Source code in src/fpgrowth/io.py
read_transactions ¶
read_transactions(path: str, item_parser: Callable[[str], Hashable] = int) -> List[FrozenSet[Hashable]]
Read only the transactions from a transaction database file.
This is a convenience wrapper around :func:read_database that discards
the declared transaction count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the transaction database file. |
required |
item_parser
|
Callable[[str], Hashable]
|
Callable converting each item token to an item. |
int
|
Returns:
| Type | Description |
|---|---|
List[FrozenSet[Hashable]]
|
The list of parsed transactions. |
Source code in src/fpgrowth/io.py
parse_transaction_line ¶
parse_transaction_line(line: str, item_parser: Callable[[str], Hashable] = int) -> Tuple[int, int, FrozenSet[Hashable]]
Parse one transaction line from a transaction database file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
A single line such as |
required |
item_parser
|
Callable[[str], Hashable]
|
Callable converting each item token to an item. Defaults to
:class: |
int
|
Returns:
| Type | Description |
|---|---|
int
|
A tuple |
int
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the line does not have exactly three tab-separated fields or the id/count fields are not integers. |
Source code in src/fpgrowth/io.py
format_itemset ¶
Render an itemset as a stable, space-separated string.
Items are ordered by their string representation so that output is deterministic across runs and independent of set iteration order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
itemset
|
Iterable[Hashable]
|
The items to render. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The items joined by single spaces. |
Source code in src/fpgrowth/io.py
write_frequent_itemsets ¶
Write frequent itemsets and their supports to a text file.
The first line is |FPs| = <count>. Each following line is
<items> : <support>. Lines are sorted by itemset size and then by
their rendered text so that the file is deterministic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination file path. Parent directories are created if needed. |
required |
itemsets
|
Mapping[FrozenSet[Hashable], int]
|
Mapping from itemset (a :class: |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of itemsets written. |