算法描述
(1)扫描全部数据,产生候选1-项集的集合C1;
(2)根据最小支持度,由候选1-项集的集合C1产生频繁1-项集的集合L1;
(3)对k>1,重复执行步骤(4)、(5)、(6);
(4)由Lk执行连接和剪枝操作,产生候选(k+l)-项集的集合Ck+1;
(5)根据最小支持度,由候选(k+l)-项集的集合Ck+1,产生频繁(k+1)-项集的集合Lk+1;
(6)若L≠Φ,则k=k+1,跳往步骤(4);否则,跳往步骤(7);
(7)根据最小置信度,由频繁项集产生强关联规则,结束。
代码
public class Apriori2
{
private final static int SUPPORT = 8; // 支持度阈值
private final static double CONFIDENCE = 0.6; // 置信度阈值
private final static String ITEM_SPLIT = " "; // 项之间的分隔符
private final static String CON = "->"; // 项之间的分隔符
/**
* 算法主程序
* @param dataList
* @return Map<String, Integer>
*/
public Map<String, Integer> apriori(ArrayList<String> dataList)
{
Map<String, Integer> stepFrequentSetMap = new HashMap<>(findFrequentOneSets(dataList));//找出候选
// 1-项集
//频繁项集
Map<String, Integer> frequentSetMap = new HashMap<String, Integer>(stepFrequentSetMap);
while(stepFrequentSetMap.size() > 0)//上一步的频繁项集不为空
{
Map<String, Integer> candidateSetMap = aprioriGen(stepFrequentSetMap);//生成候选集
Set<String> candidateKeySet = candidateSetMap.keySet();
//扫描D,进行计数,计算候选集的支持度计数
for(String data:dataList)
{
for(String candidate:candidateKeySet)
{
boolean flag = true;
String[] strings = candidate.split(ITEM_SPLIT);
for(String string:strings)
{
if(!data.contains(string + ITEM_SPLIT))//找不出候选项集中的元素,退出
{
flag = false;
break;
}
}
if(flag)
candidateSetMap.put(candidate, candidateSetMap.get(candidate)+1);//支持度加一
}
}
//从候选集中找到符合支持度的频繁项集
stepFrequentSetMap.clear();
for(String candidate:candidateKeySet)
{
Integer count = candidateSetMap.get(candidate);
if(count>=SUPPORT)
stepFrequentSetMap.put(candidate