Apriori算法(C#)
AprioriMethod.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///AprioriMethod 的摘要说明
/// </summary>
public class AprioriMethod
{ private readonly static int support = ; // 支持度阈值
private readonly static double confidence = 0.7; // 置信度阈值
private readonly static char[] item_Split = { ';' }; // 项之间的分隔符
private readonly static string itemSplit = ";";
private readonly static String CON = "->"; // 项之间的分隔符 private readonly static List<String> transList = new List<String>(); //所有交易 public AprioriMethod()
{
//
//TODO: 在此处添加构造函数逻辑
//
//初始化交易记录
transList.Add("移动硬盘;电脑;手机;优盘");
transList.Add("电脑;优盘;");
transList.Add("电脑;优盘;");
transList.Add("手机;电脑;移动硬盘;");
transList.Add("移动硬盘;手机;");
transList.Add("电脑;手机;");
transList.Add("移动硬盘;手机;");
transList.Add("移动硬盘;电脑;手机;优盘;");
transList.Add("移动硬盘;电脑;手机;"); } public Dictionary<String, int> getFC() //计算所有频繁项集
{
Dictionary<String, int> frequentCollections = new Dictionary<String, int>();//所有的频繁集
foreach (KeyValuePair<string, int> item in getItem1FC())
{
if (frequentCollections.ContainsKey(item.Key))
{
frequentCollections.Remove(item.Key);
}
frequentCollections.Add(item.Key, item.Value);
}
Dictionary<String, int> itemkFcMap = new Dictionary<String, int>();
foreach (KeyValuePair<string, int> item in getItem1FC())
{
itemkFcMap.Add(item.Key, item.Value);
} while (itemkFcMap != null && itemkFcMap.Count != )
{
Dictionary<String, int> candidateCollection = getCandidateCollection(itemkFcMap);
List<String> ccKeySet = candidateCollection.Keys.ToList(); //对候选集项进行累加计数
foreach (String trans in transList)
{
foreach (String candidate in ccKeySet)
{
bool flag = true; // 用来判断交易中是否出现该候选项,如果出现,计数加1
String[] candidateItems = candidate.Split(item_Split, StringSplitOptions.RemoveEmptyEntries);
foreach (String candidateItem in candidateItems)
{
if (trans.IndexOf(candidateItem + itemSplit) == -)
{
flag = false;
break;
}
}
if (flag)
{
int count = candidateCollection[candidate];
candidateCollection.Remove(candidate);
candidateCollection.Add(candidate, count + );
}
}
} //从候选集中找到符合支持度的频繁集项
itemkFcMap.Clear();
foreach (String candidate in ccKeySet)
{
int count = candidateCollection[candidate];
if (count >= support)
{
itemkFcMap.Add(candidate, count);
}
} //合并所有频繁集
foreach (KeyValuePair<string, int> item in itemkFcMap)
{
if (frequentCollections.ContainsKey(item.Key))
{
frequentCollections.Remove(item.Key);
}
frequentCollections.Add(item.Key, item.Value);
}
}
return frequentCollections;
} private Dictionary<String, int> getItem1FC() //计算所有频繁1项集
{
Dictionary<String, int> sItem1Fc = new Dictionary<String, int>();
Dictionary<String, int> rItem1Fc = new Dictionary<String, int>(); //频繁1项集
foreach (String trans in transList)
{
String[] items = trans.Split(item_Split, StringSplitOptions.RemoveEmptyEntries);
foreach (String item in items)
{
int count;
if (sItem1Fc.ContainsKey(item + itemSplit))
{
count = sItem1Fc[item + itemSplit];
sItem1Fc.Remove(item + itemSplit);
sItem1Fc.Add(item + itemSplit, count + );
}
else
{
sItem1Fc.Add(item + itemSplit, );
}
}
}
List<String> keySet = sItem1Fc.Keys.ToList();
foreach (String key in keySet)
{
int count = sItem1Fc[key];
if (count >= support)
{
rItem1Fc.Add(key, count);
}
}
return rItem1Fc;
} private Dictionary<String, int> getCandidateCollection(Dictionary<String, int> itemkFcMap) //生成候选项集
{
Dictionary<String, int> candidateCollection = new Dictionary<String, int>();
List<String> itemkSet1 = itemkFcMap.Keys.ToList();
List<String> itemkSet2 = itemkFcMap.Keys.ToList();
foreach (String itemk1 in itemkSet1)
{
foreach (String itemk2 in itemkSet2)
{
//进行连接
String[] tmp1 = itemk1.Split(item_Split, StringSplitOptions.RemoveEmptyEntries);
String[] tmp2 = itemk2.Split(item_Split, StringSplitOptions.RemoveEmptyEntries); String c = "";
if (tmp1.Length == )
{
if (tmp1[].CompareTo(tmp2[]) < )
{
c = tmp1[] + itemSplit + tmp2[] + itemSplit;
}
}
else
{
bool flag = true;
for (int i = ; i < tmp1.Length - ; i++)
{
if (!tmp1[i].Equals(tmp2[i]))
{
flag = false;
break;
}
}
if (flag && (tmp1[tmp1.Length - ].CompareTo(tmp2[tmp2.Length - ]) < ))
{
c = itemk1 + tmp2[tmp2.Length - ] + itemSplit;
}
} //进行剪枝
bool hasInfrequentSubSet = false;
if (!c.Equals(""))
{
String[] tmpC = c.Split(item_Split, StringSplitOptions.RemoveEmptyEntries);
for (int i = ; i < tmpC.Length; i++)
{
String subC = "";
for (int j = ; j < tmpC.Length; j++)
{
if (i != j)
{
subC = subC + tmpC[j] + itemSplit;
}
}
if (!itemkFcMap.ContainsKey(subC))
{
hasInfrequentSubSet = true;
break;
}
}
}
else
{
hasInfrequentSubSet = true;
} if (!hasInfrequentSubSet)
{
candidateCollection.Add(c, );
}
}
}
return candidateCollection;
} public Dictionary<String, Double> getRelationRules(Dictionary<String, int> frequentCollection) //计算关联规则
{
Dictionary<String, Double> relationRules = new Dictionary<String, Double>();
List<String> keySet = frequentCollection.Keys.ToList();
foreach (String key in keySet)
{
double countAll = frequentCollection[key];
String[] keyItems = key.Split(item_Split, StringSplitOptions.RemoveEmptyEntries);
if (keyItems.Length > )
{
List<String> source = keyItems.ToList(); //Collections.addAll(source, keyItems);
List<List<String>> result = new List<List<String>>();
buildSubSet(source, result); //获得source的所有非空子集
foreach (List<String> itemList in result)
{
if (itemList.Count < source.Count)
{ //只处理真子集
List<String> otherList = new List<String>();
foreach (String sourceItem in source)
{
if (!itemList.Contains(sourceItem))
{
otherList.Add(sourceItem);
}
}
String reasonStr = "";//前置
String resultStr = "";//结果
foreach (String item in itemList)
{
reasonStr = reasonStr + item + itemSplit;
}
foreach (String item in otherList)
{
resultStr = resultStr + item + itemSplit;
}
double countReason = frequentCollection[reasonStr];
double itemConfidence = countAll / countReason;//计算置信度
if (itemConfidence >= confidence)
{
String rule = reasonStr + CON + resultStr;
//relationRules.Remove(rule);
relationRules.Add(rule, itemConfidence);
}
}
}
}
}
return relationRules;
} private void buildSubSet(List<String> sourceSet, List<List<String>> result) //建立频繁项集的子集
{
// 仅有一个元素时,递归终止。此时非空子集仅为其自身,所以直接添加到result中
if (sourceSet.Count == )
{
List<String> set = new List<String>();
set.Add(sourceSet[]);
result.Add(set);
}
else if (sourceSet.Count > )
{
// 当有n个元素时,递归求出前n-1个子集,在于result中
buildSubSet(sourceSet.Take(sourceSet.Count - ).ToList(), result);
int size = result.Count;// 求出此时result的长度,用于后面的追加第n个元素时计数
// 把第n个元素加入到集合中
List<String> single = new List<String>();
single.Add(sourceSet[sourceSet.Count - ]);
result.Add(single);
// 在保留前面的n-1子集的情况下,把第n个元素分别加到前n个子集中,并把新的集加入到result中;
// 为保留原有n-1的子集,所以需要先对其进行复制
List<String> clone;
for (int i = ; i < size; i++)
{
clone = new List<String>();
foreach (String str in result[i])
{
clone.Add(str);
}
clone.Add(sourceSet[sourceSet.Count - ]);
result.Add(clone);
}
}
} }
Default.aspx.cs
AprioriMethod apriori = new AprioriMethod();
Dictionary<String, int> frequentCollection = apriori.getFC();
Response.Write("----------------频繁集" + "----------------");
Response.Write("<br/>");
foreach (var item in frequentCollection)
{
Response.Write(item.Key + "-----" + item.Value);
Response.Write("<br/>");
} Dictionary<String, Double> relationRules = apriori.getRelationRules(frequentCollection);
Response.Write("----------------关联规则" + "----------------");
Response.Write("<br/>");
foreach (var item in relationRules)
{
Response.Write(item.Key + "-----" + item.Value);
Response.Write("<br/>");
}
结果:
----------------频繁集----------------
移动硬盘;-----6
电脑;-----7
手机;-----7
优盘;-----4
电脑;移动硬盘;-----4
电脑;手机;-----5
电脑;优盘;-----3
手机;移动硬盘;-----6
电脑;手机;移动硬盘;-----4
----------------关联规则----------------
电脑;->手机;-----0.714285714285714
手机;->电脑;-----0.714285714285714
优盘;->电脑;-----0.75
手机;->移动硬盘;-----0.857142857142857
移动硬盘;->手机;-----1
电脑;手机;->移动硬盘;-----0.8
电脑;移动硬盘;->手机;-----1
Apriori算法(C#)的更多相关文章
- Apriori算法的原理与python 实现。
前言:这是一个老故事, 但每次看总是能从中想到点什么.在一家超市里,有一个有趣的现象:尿布和啤酒赫然摆在一起出售.但是这个奇怪的举措却使尿布和啤酒的销量双双增加了.这不是一个笑话,而是发生在美国沃尔玛 ...
- #研发解决方案#基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案
郑昀 基于杨海波的设计文档 创建于2015/8/13 最后更新于2015/8/25 关键词:异常流量.rate limiting.Nginx.Apriori.频繁项集.先验算法.Lua.ELK 本文档 ...
- 数据挖掘算法(四)Apriori算法
参考文献: 关联分析之Apriori算法
- 机器学习实战 - 读书笔记(11) - 使用Apriori算法进行关联分析
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第11章 - 使用Apriori算法进行关联分析. 基本概念 关联分析(associat ...
- 关联规则挖掘之apriori算法
前言: 众所周知,关联规则挖掘是数据挖掘中重要的一部分,如著名的啤酒和尿布的问题.今天要学习的是经典的关联规则挖掘算法--Apriori算法 一.算法的基本原理 由k项频繁集去导出k+1项频繁集. 二 ...
- 利用Apriori算法对交通路况的研究
首先简单描述一下Apriori算法:Apriori算法分为频繁项集的产生和规则的产生. Apriori算法频繁项集的产生: 令ck为候选k-项集的集合,而Fk为频繁k-项集的集合. 1.首先通过单遍扫 ...
- Apriori算法例子
1 Apriori介绍 Apriori算法使用频繁项集的先验知识,使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描事务(交易)记录,找出所有的频繁1项集,该集合记做L1,然 ...
- Apriori算法实例----Weka,R, Using Weka in my javacode
学习数据挖掘工具中,下面使用4种工具来对同一个数据集进行研究. 数据描述:下面这些数据是15个同学选修课程情况,在课程大纲中共有10门课程供学生选择,下面给出具体的选课情况,以ARFF数据文件保存,名 ...
- Apriori算法在购物篮分析中的运用
购物篮分析是一个很经典的数据挖掘案例,运用到了Apriori算法.下面从网上下载的一超市某月份的数据库,利用Apriori算法进行管理分析.例子使用Python+MongoDB 处理过程1 数据建模( ...
- 关于apriori算法的一个简单的例子
apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...
随机推荐
- 【WCF】错误处理(一):FaultException 与 FaultReason 的搭配
这里所说的错误处理主要是指服务代码中抛出的异常,即开发人员主动抛出的错误当然,由于网络问题或者配置不正确,会引发连接超时的错误,但这里老周要说的是,我们在实现服务逻辑时主动抛出的异常,尤其是对客户端传 ...
- WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置
WebStorm :是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaScript IDE ...
- wemall app商城源码中基于JAVA通过Http请求获取json字符串的代码
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.分享其中关于通过Http请求获取json字符串的代码供 ...
- 1623: [Usaco2008 Open]Cow Cars 奶牛飞车
1623: [Usaco2008 Open]Cow Cars 奶牛飞车 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 291 Solved: 201[S ...
- Raft算法,从学习到忘记
Raft算法,从学习到忘记 --Raft算法阅读笔记. --Github 概述 说到分布式一致性算法,可能大多数人的第一反应是paxos算法.但是paxos算法一直以来都被认为是难以理解,难以实现.S ...
- OnsenUI和AngularJS配合搭建混合应用的基本步骤
混合开发的热潮已经掀起,实现混合开发的方式很多.今天给大家介绍一个实现混合开发的基本方法-OnsenUI和AngularJS配合. OnsenUI是一个可以实现混合开发的前端框架,包含了很多前端设计中 ...
- vue单文件组件的构建
在很多Vue项目中,我们使用 Vue.component 来定义全局组件,这种方式在很多中小规模的项目中运作的很好. 但当在更复杂的项目中,就有了很大的弊端. 我们就可以用文件扩展名 .vue的单文件 ...
- 记 suds 模块循环依赖的坑-RuntimeError: maximum recursion depth exceeded
下面是soa接口调用的核心代码 #! /usr/bin/python # coding:utf-8 from suds.client import Clientdef SoaRequest(wsdl, ...
- HTML5学习笔记<三>: HTML5样式, 连接和表格
HTML样式 1, 标签: <style>: 样式定义 <link>: 资源引用 2. 属性: rel="stylesheet": 外部样式表 type=& ...
- [C++]现行的试卷封面并获取学生题目得分信息以及学号信息的原型系统
大二的时候写的一个CV小玩意,最终决定还是把它放出来,也许会帮助到很多人,代码写的很丑,大家多多包涵.附加实验报告主要部分. 课题背景及意义: 本项目主要目标是设计一套能自动分析我校现行的试卷封面并获 ...