教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 实用文档 >

Apriori算法及java实现(2)

来源:网络收集 时间:2026-02-26
导读: } } return candidateCollection; } private MapString,Integer getItem1FC(){ MapString,Integer sItem1FcMap=new HashMapString,Integer(); MapString,Integer rItem1FcMap=new HashMapString,Integer();//频繁1

}

}

return candidateCollection;

}

private Map<String,Integer> getItem1FC(){

Map<String,Integer> sItem1FcMap=new HashMap<String,Integer>();

Map<String,Integer> rItem1FcMap=new HashMap<String,Integer>();//频繁1项集

for(String trans:transList){

String[] items=trans.split(ITEM_SPLIT);

for(String item:items){

Integer count=sItem1FcMap.get(item+ITEM_SPLIT);

if(count==null){

sItem1FcMap.put(item+ITEM_SPLIT, 1);

}else{

sItem1FcMap.put(item+ITEM_SPLIT, count+1);

}

}

}

Set<String> keySet=sItem1FcMap.keySet();

for(String key:keySet){

Integer count=sItem1FcMap.get(key);

if(count>=SUPPORT){

rItem1FcMap.put(key, count);

}

}

return rItem1FcMap;

}

public Map<String,Double> getRelationRules(Map<String,Integer> frequentCollectionMap){

Apriori算法详解及java代码实现

Map<String,Double> relationRules=new HashMap<String,Double>();

Set<String> keySet=frequentCollectionMap.keySet();

for (String key : keySet) {

double countAll=frequentCollectionMap.get(key);

String[] keyItems = key.split(ITEM_SPLIT);

if(keyItems.length>1){

List<String> source=new ArrayList<String>();

Collections.addAll(source, keyItems);

List<List<String>> result=new ArrayList<List<String>>();

buildSubSet(source,result);//获得source的所有非空子集

for(List<String> itemList:result){

if(itemList.size()<source.size()){//只处理真子集

List<String> otherList=new ArrayList<String>();

for(String sourceItem:source){

if(!itemList.contains(sourceItem)){

otherList.add(sourceItem);

}

}

String reasonStr="";//前置

String resultStr="";//结果

for(String item:itemList){

reasonStr=reasonStr+item+ITEM_SPLIT;

}

for(String item:otherList){

resultStr=resultStr+item+ITEM_SPLIT;

}

double countReason=frequentCollectionMap.get(reasonStr);

double itemConfidence=countAll/countReason;//计算置信度

if(itemConfidence>=CONFIDENCE){

String rule=reasonStr+CON+resultStr;

relationRules.put(rule, itemConfidence);

}

Apriori算法详解及java代码实现

}

}

}

return relationRules;

}

private void buildSubSet(List<String> sourceSet, List<List<String>> result) {

// 仅有一个元素时,递归终止。此时非空子集仅为其自身,所以直接添加到result中 if (sourceSet.size() == 1) {

List<String> set = new ArrayList<String>();

set.add(sourceSet.get(0));

result.add(set);

} else if (sourceSet.size() > 1) {

// 当有n个元素时,递归求出前n-1个子集,在于result中

buildSubSet(sourceSet.subList(0, sourceSet.size() - 1), result);

int size = result.size();// 求出此时result的长度,用于后面的追加第n个元素时计数 // 把第n个元素加入到集合中

List<String> single = new ArrayList<String>();

single.add(sourceSet.get(sourceSet.size() - 1));

result.add(single);

// 在保留前面的n-1子集的情况下,把第n个元素分别加到前n个子集中,并把新的集加入到result中;

// 为保留原有n-1的子集,所以需要先对其进行复制

List<String> clone;

for (int i = 0; i < size; i++) {

clone = new ArrayList<String>();

for (String str : result.get(i)) {

clone.add(str);

}

clone.add(sourceSet.get(sourceSet.size() - 1));

result.add(clone);

}

Apriori算法详解及java代码实现

}

public static void main(String[] args){

Apriori apriori=new Apriori();

Map<String,Integer> frequentCollectionMap=apriori.getFC();

System.out.println("----------------频繁集"+"----------------");

Set<String> fcKeySet=frequentCollectionMap.keySet();

for(String fcKey:fcKeySet){

System.out.println(fcKey+" : "+frequentCollectionMap.get(fcKey)); }

Map<String,Double> relationRulesMap=apriori.getRelationRules(frequentCollectionMap); System.out.println("----------------关联规则"+"----------------");

Set<String> rrKeySet=relationRulesMap.keySet();

for(String rrKey:rrKeySet){

System.out.println(rrKey+" : "+relationRulesMap.get(rrKey));

}

}

}

…… 此处隐藏:1331字,全部文档内容请下载后查看。喜欢就下载吧 ……
Apriori算法及java实现(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/133437.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)