資源簡(jiǎn)介
Java語(yǔ)言實(shí)現(xiàn)的Apriori算法,數(shù)據(jù)可由excel數(shù)據(jù)表格提供,eclipse導(dǎo)入即可運(yùn)行

代碼片段和文件信息
package?asocciationRule.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;
public?class?AprioriAlgorithm?{
private?Map>?txDatabase;?//?事務(wù)數(shù)據(jù)庫(kù)
private?Float?minSup;?//?最小支持度
private?Float?minConf;?//?最小置信度
private?Integer?txDatabaseCount;?//?事務(wù)數(shù)據(jù)庫(kù)中的事務(wù)數(shù)
private?Map>>?freqItemSet;?//?頻繁項(xiàng)集集合
private?Map?Set>>?assiciationRules;?//?頻繁關(guān)聯(lián)規(guī)則集合
public?AprioriAlgorithm(Map>?txDatabase?Float?minSup
Float?minConf)?{
this.txDatabase?=?txDatabase;
this.minSup?=?minSup;
this.minConf?=?minConf;
this.txDatabaseCount?=?this.txDatabase.size();
freqItemSet?=?new?TreeMap>>();
assiciationRules?=?new?HashMap?Set>>();
}
public?Map?Float>?getFreq1ItemSet()?{//zzx?生成一階頻繁項(xiàng)集
Map?Float>?freq1ItemSetMap?=?new?HashMap?Float>();
Map?Integer>?candFreq1ItemSet?=?this.getCandFreq1ItemSet();
Iterator?Integer>>?it?=?candFreq1ItemSet
.entrySet().iterator();
while?(it.hasNext())?{
Map.Entry?Integer>?entry?=?it.next();
//?計(jì)算支持度
Float?supported?=?new?Float(entry.getValue().toString())
/?new?Float(txDatabaseCount);
if?(supported?>=?minSup)?{
freq1ItemSetMap.put(entry.getKey()?supported);
}
}
return?freq1ItemSetMap;
}
public?Map?Integer>?getCandFreq1ItemSet()?{
Map?Integer>?candFreq1ItemSetMap?=?new?HashMap?Integer>();
Iterator>>?it?=?txDatabase.entrySet()
.iterator();
//?統(tǒng)計(jì)支持?jǐn)?shù),生成候選頻繁1-項(xiàng)集
while?(it.hasNext())?{
Map.Entry>?entry?=?it.next();
Set?itemSet?=?entry.getValue();
for?(String?item?:?itemSet)?{
Set?key?=?new?HashSet();
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;
}
/*
?*?zzx生成m+1階候選項(xiàng)集
?*/
public?Set>?aprioriGen(int?m?Set>?freqMItemSet)?{
Set>?candFreqKItemSet?=?new?HashSet>();
Iterator>?it?=?freqMItemSet.iterator();
Set?originalItemSet?=?null;
while?(it.hasNext())?{
originalItemSet?=?it.next();
Iterator>?itr?=?this.getIterator(originalItemSetfreqMItemSet);
while?(itr.hasNext())?{
Set?identicalSet?=?new?HashSet();?//?兩個(gè)項(xiàng)集相同元素的集合(集合的交運(yùn)算)
identicalSet.addAll(originalItemSet);
Set?set?=?itr.next();
identicalSet.retainAll(set);?//?identicalSet中剩下的元素是identicalSet與set集合中相同的元素
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????9780??2016-03-23?20:58??apriori\AprioriAlgorithm.java
?????文件????????2248??2014-09-15?15:15??apriori\ProperSubsetCombination.java
?????文件????????2242??2014-09-15?15:15??apriori\TestAprioriAlgorithm.java
?????目錄???????????0??2018-10-27?21:30??apriori\
評(píng)論
共有 條評(píng)論