The fp_growth algorithm is an excellent method in association analysis, which maps the whole transaction database to the tree structure by constructing fp_tree, which greatly reduces the time to scan the database frequently.
The fp_growth algorithm is divided into two steps, the first step is to construct the Fp-tree, and the second step is to extract the frequent itemsets from the Fp-tree.
For more information on correlation analysis and fp_growth and pseudo-code see: What is correlation analysis, fp-growth algorithm introduction.
The main purpose of this paper is to introduce the code of Python implementation fp_growth algorithm.
The Fp_growth project directory has four files:
>fp_growth
? __init__.py
? tree_builder.py
? tree_building.py
? tree_miner.py
Among them, tree_builder.py and tree_building.py files are mainly responsible for the fp-tree structure, while tree_miner.py is used to extract frequent itemsets from the constructed fp-tree.
The file "\__init__.py" where the main program resides is as follows:
#coding =utf-8ImportTree_builderImportTree_minerroutines = [[' Cola ',' Egg ',' Ham '], [' Cola ',' Diaper ',' Beer '], [' Cola ',' Beer ',' Diaper ',' Ham '], [' Diaper ',' Beer '] ]#事务数据集Min_sup =2 #最小支持度计数headertable = {}#头结点表, the index used to store individual itemsTreebuilder = Tree_builder. Tree_builder (Routines=routines, Min_sup=min_sup, headertable=headertable)#建造FP_TreeTree_miner. Tree_miner (Tree=treebuilder.tree, Min_sup=min_sup, headertable=headertable)#对FP_Tree进行频繁项集的挖掘
It mainly achieves three actions:
- Input transaction data set routines;
- Call Tree_builder, construct fp-tree;
- Call Tree_miner, extract frequent itemsets from Fp-tree;
Fp-tree structure See: Fp-growth algorithm Python implementation of the FP-TREE structure
Mining frequent itemsets see: Mining of frequent itemsets implemented by fp-growth algorithm Python
GenerationCodetoAddress: Fp-growth algorithm python implementation (full code).
Note: This code is written in a python2.7+eclipse environment. You can import the project in Eclipse, or you can execute the "__init__.py" file with the python command in the command-line window.
Copyright NOTICE: Reprint Please indicate the source, thank you!
Fp_growth algorithm Python implementation