JAVA实现FP_Growth算法(附详细注释)

JAVA实现FP_Growth算法(附详细注释)
在学习数据挖掘的过程中,必然会接触到FP_Growth算法。
而FP_Growth算法需要使用大量的递归调用,比较难以理解和实践,作者在网上多番搜寻也没有找到一个完整详细解释的FP_Growth代码,可能是高手大多不屑于解释自己的代码,无奈只能自己动手,有不足之处请多谅解。
程序编写过程中部分参考了JAVA实现FP-GROWTH算法https://www.cnblogs.com/liguangsunls/p/6892482.html,数据集使用的是FP-Tree算法的实现https://www.cnblogs.com/zhangchaoyang/articles/2198946.html中的trolley.txt。

具体实现
作者编写了两个类来实现FP_Growth算法的相关功能,其中TreeNode类用以保存树结点的信息,FPTree类用以实现需要用到的各种方法。

TreeNode类

import java.util.ArrayList;
import java.util.Comparator;

public class TreeNode{
   
	private String name;											//项名称
	private int count;												//支持度
	private TreeNode parent;										//父结点
	private ArrayList<TreeNode> childs;								//子结点(列表)
	private TreeNode nextSameNode;									//指向下一个相同结点

	public TreeNode() {
   
		super();
	}

	public TreeNode(String name, int count) {
   
		super();
		this.name = name;
		this.count = count;
		this.childs = new ArrayList<TreeNode>();
		this.parent = null;
		this.nextSameNode = null;
	}

	public TreeNode(String name, int count, TreeNode parent) {
   
		super();
		this.name = name;
		this.count = count;
		this.childs = new ArrayList<TreeNode>();
		this.parent = parent;
		this.nextSameNode = null;
	}

	public String getName() {
   
		return name;
	}

	public void setName(String name) {
   
		this.name = name;
	}

	public int getCount() {
   
		return count;
	}

	public void setCount(int count) {
   
		this.count = count;
	}

	public TreeNode getParent() {
   
		return this.parent;
	}
	
	public boolean hasParent(){
   
		return this.parent == null ? false : true;
	}

	public void setParent(TreeNode parent) {
   
		this.parent = parent;
	}
	
	public void addChild(TreeNode child){
   
		this.childs.add(child);
	}
	
	public void addChilds(ArrayList<TreeNode> childs){
   
		this.childs.addAll(childs);
	}
	
	public void delChild(TreeNode child){
   
		this.childs.remove(child);
	}	
	
	public TreeNode getNextSameNode() {
   
		return this.nextSameNode;
	}
	
	public boolean hasNextSameNode(){
   
		return this.nextSameNode == null ? false : true;
	}

	public void setNextSameNode(TreeNode nextSameNode) {
   
		this.nextSameNode = nextSameNode;
	}

	public ArrayList<TreeNode> getChilds() {
   
		return childs;
	}
	
	public void inc(int i){
   
		this.count += i;
	}
	
	@Override
	public String toString() {
   
		return this.getName() + ":" + this.getCount();
	}
}

/*
 * @function:为实现使用Collections.sort方法对泛型为TreeNode的List进行排序,需要实现Comparator接口
 */
class TreeNodeSort implements Comparator<TreeNode>{
   

	@Override
	public int compare(TreeNode o1, TreeNode o2) {
   
		int result = Integer.compare(o2.getCount(), o1.getCount());		//实现降序排列
		//int result = Integer.compare(o1.getCount(), o2.getCount());	//实现升序排列
		if(result != 0)
			return result;
		return o1.getName().compareTo(o2.getName
  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
FP-growth算法是一种用于频繁模式挖掘的算法,它的思想是通过建立一棵FP树来快速发现频繁项集。FP-growth算法的主要步骤如下: 1. 构建FP树:遍历所有的事务,对于每个事务,按照出现次数从高到低排序,构建一棵FP树。 2. 构建条件模式基:对于每个频繁项集,找到它在FP树上的所有前缀路径,这些前缀路径的末尾节点就是频繁项集的支持度计数,将这些前缀路径作为条件模式基。 3. 递归构建FP树:对于每个频繁项集,以它在FP树上出现的所有节点为起点,重新构建一棵新的FP树,并且递归地对新的FP树进行频繁项集挖掘。 4. 合并频繁项集:通过递归地构建FP树,得到所有的频繁项集,将这些频繁项集合并起来,得到最终的频繁模式。 以下是Python中实现FP-growth算法的基本步骤: 1. 定义一个节点类,表示FP树中的每个节点。 2. 定义一个函数,用于构建FP树。 3. 定义一个函数,用于构建条件模式基。 4. 定义一个函数,用于递归地构建FP树,并挖掘频繁项集。 5. 定义一个函数,用于合并频繁项集。 6. 最后,调用以上函数,实现FP-growth算法。 当然,也可以使用已经实现好的Python包来进行FP-growth算法实现。比如,可以使用mlxtend包中的fp_growth函数,直接传入事务数据,就可以得到频繁项集。示例代码如下: ```python from mlxtend.preprocessing import TransactionEncoder from mlxtend.frequent_patterns import fp_growth # 事务数据 transactions = [['apple', 'beer'], ['apple', 'beer', 'diaper'], ['apple', 'diaper'], ['beer', 'diaper']] # 转换事务数据为布尔矩阵 te = TransactionEncoder() te_ary = te.fit_transform(transactions) df = pd.DataFrame(te_ary, columns=te.columns_) # 使用fp_growth函数得到频繁项集 frequent_itemsets = fp_growth(df, min_support=0.5, use_colnames=True) print(frequent_itemsets) ``` 上述代码中,使用TransactionEncoder将事务数据转换成布尔矩阵,然后使用fp_growth函数得到频繁项集。min_support参数表示最小支持度,use_colnames参数表示是否使用列名作为频繁项集的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值