apriori如何实现apriori算法
apriori 时间:2021-06-22 阅读:(
)
Clementine关联规则Apriori算法事务模式怎么使用
算法: Apriori算法,使用逐层迭代找出频繁项集。
输入:事务数据库D;最小支持度阈值min_sup。
输出:D 中的频繁项集L。
1) L1 = find_frequent_1_itemsets(D);
2) for (k = 2; Lk-1 ≠ ; k++) {
3) Ck = aproiri_gen(Lk-1,min_sup);
4) for each transaction t D{ //scan D for count
5) Ct = subset(Ck,t); //get subsets of t that are candidates
6) for each candidate c Ct
7) c.count++;
8) }
9) Lk={c Ck | c.count ≥ min_sup}
10) }
11) return L = ∪kLk;问读音:null,Apriori,FP-Growth的读法
汉语标出可真不准确,不方便啊
servlet /s?:vlit/--/se wu li te/
HTML 就是一个一个字母的读,它是hyper text markup language简写
null /n?l/--/na ou/
apriori 英文发音为:/?pri?ri/--/e pe rui ao rui/
FP-Growth 英文发音为:/aif pi: gr?uθ/--/F P-ge rou si/
前面一个词一般读中文 普瑞奥瑞
后面的一个词 直接读英文如何实现apriori算法
import?java.util.HashMap;
import?java.util.HashSet;
import?java.util.Iterator;
import?java.util.Map;
import?java.util.Set;
import?java.util.TreeMap;
/**
*?<B>关联规则挖掘:Apriori算法</B>
*?
*?<P>按照Apriori算法的基本思想来实现
*?
*?@author?king
*?@since?2013/06/27
*?
*/
public?class?Apriori?{
private?Map<Integer,?Set<String>>?txDatabase;?//?事务数据库
private?Float?minSup;?//?最小支持度
private?Float?minConf;?//?最小置信度
private?Integer?txDatabaseCount;?//?事务数据库中的事务数
private?Map<Integer,?Set<Set<String>>>?freqItemSet;?//?频繁项集集合
private?Map<Set<String>,?Set<Set<String>>>?assiciationRules;?//?频繁关联规则集合
public?Apriori(
????Map<Integer,?Set<String>>?txDatabase,?
????Float?minSup,?
????Float?minConf)?{
???this.txDatabase?=?txDatabase;
???this.minSup?=?minSup;
???this.minConf?=?minConf;
???this.txDatabaseCount?=?this.txDatabase.size();
???freqItemSet?=?new?TreeMap<Integer,?Set<Set<String>>>();
???assiciationRules?=?new?HashMap<Set<String>,?Set<Set<String>>>();
}
/**
*?扫描事务数据库,计算频繁1-项集
*?@return
*/
public?Map<Set<String>,?Float>?getFreq1ItemSet()?{
???Map<Set<String>,?Float>?freq1ItemSetMap?=?new?HashMap<Set<String>,?Float>();
???Map<Set<String>,?Integer>?candFreq1ItemSet?=?this.getCandFreq1ItemSet();
???Iterator<Map.Entry<Set<String>,?Integer>>?it?=?candFreq1ItemSet.entrySet().iterator();
???while(it.hasNext())?{
????Map.Entry<Set<String>,?Integer>?entry?=?it.next();
????//?计算支持度
????Float?supported?=?new?Float(entry.getValue().toString())/new?Float(txDatabaseCount);
????if(supported>=minSup)?{
?????freq1ItemSetMap.put(entry.getKey(),?supported);
????}
???}
???return?freq1ItemSetMap;
}
/**
*?计算候选频繁1-项集
*?@return
*/
public?Map<Set<String>,?Integer>?getCandFreq1ItemSet()?{
???Map<Set<String>,?Integer>?candFreq1ItemSetMap?=?new?HashMap<Set<String>,?Integer>();
???Iterator<Map.Entry<Integer,?Set<String>>>?it?=?txDatabase.entrySet().iterator();
???//?统计支持数,生成候选频繁1-项集
???while(it.hasNext())?{
????Map.Entry<Integer,?Set<String>>?entry?=?it.next();
????Set<String>?itemSet?=?entry.getValue();
????for(String?item?:?itemSet)?{
?????Set<String>?key?=?new?HashSet<String>();
?????key.add(item.trim());
?????if(!candFreq1ItemSetMap.containsKey(key))?{
??????Integer?value?=?1;
??????candFreq1ItemSetMap.put(key,?value);
?????}
?????else?{
??????Integer?value?=?1+candFreq1ItemSetMap.get(key);
??????candFreq1ItemSetMap.put(key,?value);
?????}
????}
???}
???return?candFreq1ItemSetMap;
}
/**
*?根据频繁(k-1)-项集计算候选频繁k-项集
*?
*?@param?m?其中m=k-1
*?@param?freqMItemSet?频繁(k-1)-项集
*?@return
*/
public?Set<Set<String>>?aprioriGen(int?m,?Set<Set<String>>?freqMItemSet)?{
???Set<Set<String>>?candFreqKItemSet?=?new?HashSet<Set<String>>();
???Iterator<Set<String>>?it?=?freqMItemSet.iterator();
???Set<String>?originalItemSet?=?null;
???while(it.hasNext())?{
????originalItemSet?=?it.next();
????Iterator<Set<String>>?itr?=?this.getIterator(originalItemSet,?freqMItemSet);
????while(itr.hasNext())?{
?????Set<String>?identicalSet?=?new?HashSet<String>();?//?两个项集相同元素的集合(集合的交运算)????
?????identicalSet.addAll(originalItemSet);?
?????Set<String>?set?=?itr.next();?
?????identicalSet.retainAll(set);?//?identicalSet中剩下的元素是identicalSet与set集合中公有的元素
?????if(identicalSet.size()?==?m-1)?{?//?(k-1)-项集中k-2个相同
??????Set<String>?differentSet?=?new?HashSet<String>();?//?两个项集不同元素的集合(集合的差运算)
??????differentSet.addAll(originalItemSet);
??????differentSet.removeAll(set);?//?因为有k-2个相同,则differentSet中一定剩下一个元素,即differentSet大小为1
??????differentSet.addAll(set);?//?构造候选k-项集的一个元素(set大小为k-1,differentSet大小为k)
??????if(!this.has_infrequent_subset(differentSet,?freqMItemSet))
??????????candFreqKItemSet.add(differentSet);?//?加入候选k-项集集合
?????}
????}
???}
???return?candFreqKItemSet;
}
/**
?*?使用先验知识,剪枝。
若候选k项集中存在k-1项子集不是频繁k-1项集,则删除该候选k项集
?*?@param?candKItemSet
?*?@param?freqMItemSet
?*?@return
?*/
private?boolean?has_infrequent_subset(Set<String>?candKItemSet,?Set<Set<String>>?freqMItemSet)?{
Set<String>?tempSet?=?new?HashSet<String>();
tempSet.addAll(candKItemSet);
Iterator<String>?itItem?=?candKItemSet.iterator();
while(itItem.hasNext())?{
String?item?=?itItem.next();
tempSet.remove(item);//?该候选去掉一项后变为k-1项集
if(!freqMItemSet.contains(tempSet))//?判断k-1项集是否是频繁项集
return?true;
tempSet.add(item);//?恢复
}
return?false;
}
/**
*?根据一个频繁k-项集的元素(集合),获取到频繁k-项集的从该元素开始的迭代器实例
*?@param?itemSet
*?@param?freqKItemSet?频繁k-项集
*?@return
*/
private?Iterator<Set<String>>?getIterator(Set<String>?itemSet,?Set<Set<String>>?freqKItemSet)?{
???Iterator<Set<String>>?it?=?freqKItemSet.iterator();
???while(it.hasNext())?{
????if(itemSet.equals(it.next()))?{
?????break;
????}
???}
???return?it;
}
/**
*?根据频繁(k-1)-项集,调用aprioriGen方法,计算频繁k-项集
*?
*?@param?k?
*?@param?freqMItemSet?频繁(k-1)-项集
*?@return
*/
public?Map<Set<String>,?Float>?getFreqKItemSet(int?k,?Set<Set<String>>?freqMItemSet)?{
???Map<Set<String>,?Integer>?candFreqKItemSetMap?=?new?HashMap<Set<String>,?Integer>();
???//?调用aprioriGen方法,得到候选频繁k-项集
???Set<Set<String>>?candFreqKItemSet?=?this.aprioriGen(k-1,?freqMItemSet);
???//?扫描事务数据库
???Iterator<Map.Entry<Integer,?Set<String>>>?it?=?txDatabase.entrySet().iterator();
???//?统计支持数
???while(it.hasNext())?{
????Map.Entry<Integer,?Set<String>>?entry?=?it.next();
????Iterator<Set<String>>?kit?=?candFreqKItemSet.iterator();
????while(kit.hasNext())?{
?????Set<String>?kSet?=?kit.next();
?????Set<String>?set?=?new?HashSet<String>();
?????set.addAll(kSet);
?????set.removeAll(entry.getValue());?//?候选频繁k-项集与事务数据库中元素做差运算
?????if(set.isEmpty())?{?//?如果拷贝set为空,支持数加1
??????if(candFreqKItemSetMap.get(kSet)?==?null)?{
???????Integer?value?=?1;
???????candFreqKItemSetMap.put(kSet,?value);
??????}
??????else?{
???????Integer?value?=?1+candFreqKItemSetMap.get(kSet);
???????candFreqKItemSetMap.put(kSet,?value);
??????}
?????}
????}
???}
RAKsmart怎么样?RAKsmart香港机房新增了付费的DDoS高防保护服务,香港服务器默认接入20Mbps的大陆优化带宽(电信走CN2、联通和移动走BGP)。高防服务器需要在下单页面的IP Addresses Option里面选择购买,分:40Gbps大陆优化高防IP-$461/月、100Gbps国际BGP高防IP-$692/月,有兴趣的可以根据自己的需求来选择!点击进入:RAKsmart官...
AlphaVPS是一家保加利亚本土主机商(DA International Group Ltd),提供VPS主机及独立服务器租用等,数据中心包括美国(洛杉矶/纽约)、德国、英国和保加利亚等,公司办公地点跟他们提供的保加利亚数据中心在一栋楼内,自有硬件,提供IPv4+IPv6,支持PayPal或者信用卡等方式付款。商家提供的大硬盘VPS主机,提供128GB-2TB磁盘,最低年付15欧元起,也可以选择...
每年的7月的最后一个周五是全球性质的“系统管理员日”,据说是为了感谢系统管理员的辛苦工作....friendhosting决定从现在开始一直到9月8日对其全球9个数据中心的VPS进行4.5折(优惠55%)大促销。所有VPS基于KVM虚拟,给100M带宽,不限制流量,允许自定义上传ISO...官方网站:https://friendhosting.net比特币、信用卡、PayPal、支付宝、微信、we...
apriori为你推荐
国家法规数据库哪个常用的法律APP比较好用?无处不在的意思人山无处不花枝的意思是什么上行宽带上行宽带和下行宽带代表什么?cursorlocation如何用ENVI把不同图像中的相同地点的某个像素点的值读出来。按时间把这个点的值连起来,。谢谢好人。泛微协同办公系统泛微OA系统怎么创建新人员空间导航怎么把空间的导航变成只有留言板跟相册particular教程AE的particular的particle设置元宝汇为什么我喝汇圆肾宝喝的阳痿了?ibooks支持什么格式ibooks支持什么格式的电子书flv转aviflv格式的视频怎么转换成avi格式?
host1plus locvps gomezpeer unsplash ixwebhosting 免费个人博客 青果网 isp服务商 hktv 无限流量 工信部网站备案查询 美国迈阿密 腾讯数据库 国外代理服务器 windowssever2008 htaccess web服务器有哪些 中国域名根服务器 rsync 回程 更多