资源简介
Python——机器学习实战——Apriori算法进行关联分析
代码片段和文件信息
‘‘‘
Created on Mar 24 2011
Ch 11 code
@author: Peter
‘‘‘
from numpy import *
def loadDataSet():
return [[1 3 4] [2 3 5] [1 2 3 5] [2 5]]
def createC1(dataSet):
C1 = []
for transaction in dataSet:
for item in transaction:
if not [item] in C1:
C1.append([item])
C1.sort()
return map(frozenset C1)#use frozen set so we
#can use it as a key in a dict
def scanD(D Ck minSupport):
ssCnt = {}
for tid in D:
for can in Ck:
if can.issubset(tid):
if not ssCnt.has_key(can): ssCnt[can]=1
else: ssCnt[can] += 1
numItems = float(len(D))
retList = []
supportData = {}
for key in ssCnt:
support = ssCnt[key]/numItems
if support >= minSupport:
retList.insert(0key)
supportData[key] = support
return retList supportData
def aprioriGen(Lk k): #creates Ck
retList = []
lenLk = len(Lk)
for i in range(lenLk):
for j in range(i+1 lenLk):
L1 = list(Lk[i])[:k-2]; L2 = list(Lk[j])[:k-2]
L1.sort(); L2.sort()
if L1==L2: #if first k-2 elements are equal
retList.append(Lk[i] | Lk[j]) #set union
return retList
def apriori(dataSet minSupport = 0.5):
C1 = createC1(dataSet)
D = map(set dataSet)
L1 supportData = scanD(D C1 minSupport)
L = [L1]
k = 2
while (len(L[k-2]) > 0):
Ck = aprioriGen(L[k-2] k)
Lk supK = scanD(D Ck minSupport)#scan DB to get Lk
supportData.update(supK)
L.append(Lk)
k += 1
return L supportData
def generateRules(L supportData minConf=0.7): #supportData is a dict coming from scanD
bigRuleList = []
for i in range(1 len(L)):#only get the sets with two or more items
for freqSet in L[i]:
H1 = [frozenset([item]) for item in freqSet]
if (i > 1):
rulesFromConseq(freqSet H1 supportData bigRuleList minConf)
else:
calcConf(freqSet H1 supportData bigRuleList minConf)
return bigRuleList
def calcConf(freqSet H supportData brl minConf=0.7):
prunedH = [] #create new list to return
for conseq in H:
conf = supportData[freqSet]/supportData[freqSet-conseq] #calc confidence
if conf >= minConf:
print freqSet-conseq‘-->‘conseq‘conf:‘conf
brl.append((freqSet-conseq conseq conf))
prunedH.append(conseq)
return prunedH
def rulesFromConseq(freqSet H supportData brl minConf=0.7):
m = len(H[0])
if (len(freqSet) > (m + 1)): #try further merging
Hmp1 = aprioriGen(H m+1)#create Hm+1 new candidates
Hmp1 = calcConf(freqSet Hmp1 supportData brl minConf)
if (len(Hmp1) > 1): #need at least two sets to m
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5897 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\apriori.py
文件 5236 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\apriori.pyc
文件 38906 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\bills20DataSet.txt
文件 137426 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\lawAssnRules.txt
文件 1806 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\meaning20.txt
文件 570408 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\mushroom.dat
文件 5585 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\recent100bills.txt
文件 1050 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析\recent20bills.txt
目录 0 2018-08-27 18:26 Python——机器学习实战——Apriori算法进行关联分析
----------- --------- ---------- ----- ----
766314 9
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论