资源简介
Apriori算法是一种挖掘关联规则的频繁项集算法,其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集。而且算法已经被广泛的应用到商业、网络安全等各个领域。
代码片段和文件信息
import MySQLdb
def generate_dicts(shop_list):
customerid_set=set()
for row in shop_list:
customerid_set.add(row[0])
tempdata_list=[]
data_set=[]
for index in customerid_set:
for row in shop_list:
if(row[0]==index):
tempdata_list.append(row[1])
data_set.append(tempdata_list)
tempdata_list=[]
return data_set
def load_data_set():
user = ‘root‘
pwd = ‘123456‘
host = ‘127.0.0.1‘
db = ‘mysql‘
port =3306
try:
cnx = MySQLdb.connect(host=host port=port user=user passwd=pwd db=db )
cur = cnx.cursor()
cur.execute(“select customer_idproduct_id from shop “)
results = cur.fetchall()
shop_list = list(results)
cur.close()
cnx.close()
except MySQLdb.Errore:
print “MySQL ERROR %d:%s“ %(e.args[0]e.args[1])
data_set=generate_dicts(shop_list)
return data_set
def create_C1(data_set):
C1 = set()
for t in data_set:
for item in t:
item_set = frozenset([item])
C1.add(item_set)
return C1
def is_apriori(Ck_item Lksub1):
for item in Ck_item:
sub_Ck = Ck_item - frozenset([item])
if sub_Ck not in Lksub1:
return False
return True
def create_Ck(Lksub1 k):
Ck = set()
len_Lksub1 = len(Lksub1)
list_Lksub1 = list(Lksub1)
for i in range(len_Lksub1):
for j in range(1 len_Lksub1):
l1 = list(list_Lksub1[i])
l2 = list(list_Lksub1[j])
l1.sort()
l2.sort()
if l1[0:k-2] == l2[0:k-2]:
Ck_item = list_Lksub1[i] | list_Lksub1[j]
# pruning
if is_apriori(Ck_item Lksub1):
Ck.add(Ck_item)
return Ck
def generate_Lk_by_Ck(data_set Ck min_support support_data):
Lk = set()
item_count = {}
for t in data_set:
for item in Ck:
- 上一篇:蚁群算法在去掉回路的TSP问题中应用PY
- 下一篇:python实现AES密钥扩展
相关资源
- 西电数据挖掘作业之利用Python编程实
- python元胞自动机模拟生态环境草羊狼
- movielens(100K)数据集分析,Apriori算法
- Apriori关联性分析python实现(含数据集
- 商品关联性分析python算法
- Python——机器学习实战——Apriori算法
- Apriori算法Python实现23628
- FP-growth发现频繁项集python实现(含数
- 详解python实现FP-TREE进行关联规则挖掘
- 西电数据挖掘作业——关联规则apri
- python实现Apriori算法apriori.py和数据
- python数据挖掘分类聚类回归关联算法
- MIC数据关联性挖掘算法Python源码
- Apriori关联性分析python实现(含数据集
- python查询百度关联词(相关搜索结果
- Apriori算法代码-Python
- 数据挖掘 Apriori算法 python版
- 关联规则挖掘之FP-growth算法实现
- FP-Growth及关联规则python代码
评论
共有 条评论