用java写Apriori算法
时间: 2024-05-11 20:18:49 浏览: 109
以下是用 Java 实现 Apriori 算法的示例代码:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Apriori {
public static void main(String[] args) {
// 构造事务数据库
List<List<String>> transactions = new ArrayList<>();
transactions.add(new ArrayList<String>() {{
add("A");
add("B");
add("C");
}});
transactions.add(new ArrayList<String>() {{
add("A");
add("C");
add("D");
}});
transactions.add(new ArrayList<String>() {{
add("B");
add("C");
add("E");
}});
transactions.add(new ArrayList<String>() {{
add("A");
add("C");
add("D");
}});
transactions.add(new ArrayList<String>() {{
add("B");
add("E");
}});
// 设置支持度阈值和置信度阈值
double minSupport = 0.4;
double minConfidence = 0.6;
// 调用 Apriori 算法进行频繁项集和关联规则挖掘
Map<List<String>, Double> frequentItemsets = apriori(transactions, minSupport);
List<AssociationRule<String>> associationRules = generateAssociationRules(frequentItemsets, minConfidence);
// 输出结果
System.out.println("频繁项集:");
for (Map.Entry<List<String>, Double> entry : frequentItemsets.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println("关联规则:");
for (AssociationRule<String> rule : associationRules) {
System.out.println(rule);
}
}
/**
* Apriori 算法
*
* @param transactions 事务数据库
* @param minSupport 支持度阈值
* @return 频繁项集及其支持度
*/
public static Map<List<String>, Double> apriori(List<List<String>> transactions, double minSupport) {
// 计算候选频繁项集的支持度
Map<List<String>, Double> candidateItemsets = new HashMap<>();
for (List<String> transaction : transactions) {
for (String item : transaction) {
List<String> itemset = new ArrayList<String>() {{
add(item);
}};
if (candidateItemsets.containsKey(itemset)) {
candidateItemsets.put(itemset, candidateItemsets.get(itemset) + 1);
} else {
candidateItemsets.put(itemset, 1.0);
}
}
}
// 迭代计算频繁项集
Map<List<String>, Double> frequentItemsets = new HashMap<>();
while (!candidateItemsets.isEmpty()) {
// 过滤掉支持度小于阈值的候选频繁项集
candidateItemsets.entrySet().removeIf(entry -> entry.getValue() / transactions.size() < minSupport);
// 将剩余的候选频繁项集加入频繁项集列表
frequentItemsets.putAll(candidateItemsets);
// 生成下一层的候选频繁项集
candidateItemsets = generateCandidateItemsets(candidateItemsets.keySet());
for (List<String> transaction : transactions) {
for (List<String> candidateItemset : candidateItemsets.keySet()) {
if (transaction.containsAll(candidateItemset)) {
candidateItemsets.put(candidateItemset, candidateItemsets.get(candidateItemset) + 1);
}
}
}
}
return frequentItemsets;
}
/**
* 生成下一层的候选频繁项集
*
* @param itemsets 上一层的频繁项集
* @return 下一层的候选频繁项集
*/
private static Map<List<String>, Double> generateCandidateItemsets(Set<List<String>> itemsets) {
Map<List<String>, Double> candidateItemsets = new HashMap<>();
for (List<String> itemset1 : itemsets) {
for (List<String> itemset2 : itemsets) {
if (itemset1.equals(itemset2)) {
continue;
}
List<String> newItemset = new ArrayList<>(itemset1);
newItemset.addAll(itemset2);
Collections.sort(newItemset);
if (!candidateItemsets.containsKey(newItemset)) {
candidateItemsets.put(newItemset, 0.0);
}
}
}
return candidateItemsets;
}
/**
* 生成关联规则
*
* @param frequentItemsets 频繁项集及其支持度
* @param minConfidence 置信度阈值
* @return 关联规则
*/
public static List<AssociationRule<String>> generateAssociationRules(Map<List<String>, Double> frequentItemsets, double minConfidence) {
List<AssociationRule<String>> associationRules = new ArrayList<>();
for (Map.Entry<List<String>, Double> entry : frequentItemsets.entrySet()) {
List<String> itemset = entry.getKey();
double support = entry.getValue() / transactions.size();
if (itemset.size() > 1) {
for (int i = 0; i < itemset.size(); i++) {
List<String> antecedent = new ArrayList<>(itemset.subList(0, i));
List<String> consequent = new ArrayList<>(itemset.subList(i, itemset.size()));
double confidence = support / frequentItemsets.get(antecedent);
if (confidence >= minConfidence) {
associationRules.add(new AssociationRule<>(antecedent, consequent, support, confidence));
}
}
}
}
return associationRules;
}
/**
* 关联规则
*
* @param <T> 项集元素类型
*/
public static class AssociationRule<T> {
private List<T> antecedent;
private List<T> consequent;
private double support;
private double confidence;
public AssociationRule(List<T> antecedent, List<T> consequent, double support, double confidence) {
this.antecedent = antecedent;
this.consequent = consequent;
this.support = support;
this.confidence = confidence;
}
public List<T> getAntecedent() {
return antecedent;
}
public List<T> getConsequent() {
return consequent;
}
public double getSupport() {
return support;
}
public double getConfidence() {
return confidence;
}
@Override
public String toString() {
return antecedent + " => " + consequent + " : Support = " + support + ", Confidence = " + confidence;
}
}
}
```
这个示例代码中包含 Apriori 算法和关联规则生成的实现,你可以根据自己的需求进行修改和扩展。
阅读全文