weka实战005:基于HashSet实现的apriori关联规则算法

2024-06-11 09:58

本文主要是介绍weka实战005:基于HashSet实现的apriori关联规则算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个一个apriori算法的演示版本,所有的代码都在一个类。仅供研究算法参考


package test;import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;//用set写的apriori算法
public class AprioriSetBasedDemo {class Transaction {/** 购物记录,用set保存多个货物名*/private HashSet<String> pnSet = new HashSet<String>();public Transaction() {pnSet.clear();}public Transaction(String[] names) {pnSet.clear();for (String s : names) {pnSet.add(s);}}public HashSet<String> getPnSet() {return pnSet;}public void addPname(String s) {pnSet.add(s);}public boolean containSubSet(HashSet<String> subSet) {return pnSet.containsAll(subSet);}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();Iterator<String> iter = pnSet.iterator();while (iter.hasNext()) {sb.append(iter.next() + ",");}return "Transaction = [" + sb.toString() + "]";}}class TransactionDB {// 记录所有的Transactionprivate Vector<Transaction> vt = new Vector<Transaction>();public TransactionDB() {vt.clear();}public int getSize() {return vt.size();}public void addTransaction(Transaction t) {vt.addElement(t);}public Transaction getTransaction(int idx) {return vt.elementAt(idx);}}public class AssoRule implements Comparable<AssoRule> {private String ruleContent;private double confidence;public void setRuleContent(String ruleContent) {this.ruleContent = ruleContent;}public void setConfidence(double confidence) {this.confidence = confidence;}public AssoRule(String ruleContent, double confidence) {this.ruleContent = ruleContent;this.confidence = confidence;}@Overridepublic int compareTo(AssoRule o) {if (o.confidence > this.confidence) {return 1;} else if (o.confidence == this.confidence) {return 0;} else {return -1;}}@Overridepublic String toString() {return ruleContent + ", confidence=" + confidence * 100 + "%";}}public static String getStringFromSet(HashSet<String> set) {StringBuilder sb = new StringBuilder();Iterator<String> iter = set.iterator();while (iter.hasNext()) {sb.append(iter.next() + ", ");}if (sb.length() > 2) {sb.delete(sb.length() - 2, sb.length() - 1);}return sb.toString();}// 计算具有最小支持度的一项频繁集 >= minSupportpublic static HashMap<String, Integer> buildMinSupportFrequenceSet(TransactionDB tdb, int minSupport) {HashMap<String, Integer> minSupportMap = new HashMap<String, Integer>();for (int i = 0; i < tdb.getSize(); i++) {Transaction t = tdb.getTransaction(i);Iterator<String> it = t.getPnSet().iterator();while (it.hasNext()) {String key = it.next();if (minSupportMap.containsKey(key)) {minSupportMap.put(key, minSupportMap.get(key) + 1);} else {minSupportMap.put(key, new Integer(1));}}}Iterator<String> iter = minSupportMap.keySet().iterator();Vector<String> toBeRemoved = new Vector<String>();while (iter.hasNext()) {String key = iter.next();if (minSupportMap.get(key) < minSupport) {toBeRemoved.add(key);}}for (int i = 0; i < toBeRemoved.size(); i++) {minSupportMap.remove(toBeRemoved.get(i));}return minSupportMap;}public void buildRules(TransactionDB tdb,HashMap<HashSet<String>, Integer> kItemFS, Vector<AssoRule> var,double ruleMinSupportPer) {// 如果kItemFS的成员数量不超过1不需要计算if (kItemFS.size() <= 1) {return;}// k+1项频项集HashMap<HashSet<String>, Integer> kNextItemFS = new HashMap<HashSet<String>, Integer>();// 获得第k项频项集@SuppressWarnings("unchecked")HashSet<String>[] kItemSets = new HashSet[kItemFS.size()];kItemFS.keySet().toArray(kItemSets);/** 根据k项频项集,用两重循环获得k+1项频项集 然后计算有多少个tranction包含这个k+1项频项集* 然后支持比超过ruleMinSupportPer,就可以生成规则,放入规则向量* 然后,将k+1项频项集及其支持度放入kNextItemFS,进入下一轮计算*/for (int i = 0; i < kItemSets.length - 1; i++) {HashSet<String> set_i = kItemSets[i];for (int j = i + 1; j < kItemSets.length; j++) {HashSet<String> set_j = kItemSets[j];// k+1 item setHashSet<String> kNextSet = new HashSet<String>();kNextSet.addAll(set_i);kNextSet.addAll(set_j);if (kNextSet.size() <= set_i.size()|| kNextSet.size() <= set_j.size()) {continue;}// 计算k+1 item set在所有transaction出现了几次int count = 0;for (int k = 0; k < tdb.getSize(); k++) {if (tdb.getTransaction(k).containSubSet(kNextSet)) {count++;}}if (count <= 0) {continue;}Integer n_i = kItemFS.get(set_i);double per = 1.0 * count / n_i.intValue();if (per >= ruleMinSupportPer) {kNextItemFS.put(kNextSet, new Integer(count));HashSet<String> tmp = new HashSet<String>();tmp.addAll(kNextSet);tmp.removeAll(set_i);String s1 = "{" + getStringFromSet(set_i) + "}" + "(" + n_i+ ")" + "==>" + getStringFromSet(tmp).toString()+ "(" + count + ")";var.addElement(new AssoRule(s1, per));}}}// 进入下一轮计算buildRules(tdb, kNextItemFS, var, ruleMinSupportPer);}public void test() {// Transaction数据集TransactionDB tdb = new TransactionDB();// 添加Transaction交易记录tdb.addTransaction(new Transaction(new String[] { "a", "b", "c", "d" }));tdb.addTransaction(new Transaction(new String[] { "a", "b" }));tdb.addTransaction(new Transaction(new String[] { "b", "c" }));tdb.addTransaction(new Transaction(new String[] { "b", "c", "d", "e" }));// 规则最小支持度double minRuleConfidence = 0.5;Vector<AssoRule> vr = computeAssociationRules(tdb, minRuleConfidence);// 输出规则int i = 0;for (AssoRule ar : vr) {System.out.println("rule[" + (i++) + "]: " + ar);}}public Vector<AssoRule> computeAssociationRules(TransactionDB tdb,double ruleMinSupportPer) {// 输出关联规则Vector<AssoRule> var = new Vector<AssoRule>();// 计算最小支持度频项HashMap<String, Integer> minSupportMap = buildMinSupportFrequenceSet(tdb, 2);// 计算一项频项集HashMap<HashSet<String>, Integer> oneItemFS = new HashMap<HashSet<String>, Integer>();for (String key : minSupportMap.keySet()) {HashSet<String> oneItemSet = new HashSet<String>();oneItemSet.add(key);oneItemFS.put(oneItemSet, minSupportMap.get(key));}// 根据一项频项集合,递归计算规则buildRules(tdb, oneItemFS, var, ruleMinSupportPer);// 将规则按照可信度排序Collections.sort(var);return var;}public static void main(String[] args) {AprioriSetBasedDemo asbd = new AprioriSetBasedDemo();asbd.test();}}

运行结果如下:


rule[0]: {d }(2)==>b (2), confidence=100.0%
rule[1]: {d }(2)==>c (2), confidence=100.0%
rule[2]: {d, a }(1)==>c (1), confidence=100.0%
rule[3]: {d, a }(1)==>b (1), confidence=100.0%
rule[4]: {d, a }(1)==>b (1), confidence=100.0%
rule[5]: {d, c }(2)==>b (2), confidence=100.0%
rule[6]: {d, b, a }(1)==>c (1), confidence=100.0%
rule[7]: {d, b, a }(1)==>c (1), confidence=100.0%
rule[8]: {d, c, a }(1)==>b (1), confidence=100.0%
rule[9]: {b }(4)==>c (3), confidence=75.0%
rule[10]: {b, c }(3)==>d (2), confidence=66.66666666666666%
rule[11]: {b, c }(3)==>d (2), confidence=66.66666666666666%
rule[12]: {d }(2)==>a (1), confidence=50.0%
rule[13]: {b }(4)==>a (2), confidence=50.0%
rule[14]: {d, c }(2)==>b, a (1), confidence=50.0%
rule[15]: {d, b }(2)==>a (1), confidence=50.0%

这篇关于weka实战005:基于HashSet实现的apriori关联规则算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050850

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库