资源简介

代码片段和文件信息
‘‘‘
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试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论