def loadDataSet():
return [[1, 2, 5],[2, 4],[2, 3],[1, 2, 4],[1, 3],[2, 3],[1, 3],[1, 2, 3, 5],[1, 2, 3]]
D=loadDataSet()
print(D)
def createC1(dataSet):
C = []
for transaction in dataSet:
for item in transaction:
if [ item ] not in C:
C.append([item])
C.sort()
return list(map(frozenset,C))
C1=createC1(D)
print(C1)
def scanD(D, Ck, minSupport):
ssCnt = {}
for tid in D:
for can in Ck:
if can.issubset(tid):
ssCnt[can] = ssCnt.get(can, 0) + 1
numItems = float(len(D))
retList = []
supportData = {}
for key in ssCnt:
support = ssCnt[key] / numItems
if support >= minSupport:
retList.insert(0, key)
supportData[key] = support
return retList, supportData
ret1,suD = scanD(loadDataSet(),createC1(loadDataSet()),0.22)
print(ret1)
print(suD)
def aprioriGen(Ck, k):
retList = []
lenCk = len(Ck)
for i in range( lenCk ):
for j in range( i + 1, lenCk ):
L1 = Ck[i]|Ck[j]
if(len(L1)==k):
if L1 not in retList:
retList.append( L1 )
return retList
ret2=aprioriGen(C1, 2)
print(ret2)
ret3=aprioriGen(ret2,3)
print(ret3)
def apriori(D, minSupport):
C1=createC1(D)
L1, suppData = 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)
suppData.update(supK)
L.append(Lk)
print("L:%s" %L)
k += 1
return L, suppData
L1,suD2=apriori(D,0.22)
print("The final support rate is %s" %suD2)
def calcConf(freqSet, H, supportData, brl, minConf=0.7):
prunedH = []
for conseq in H:
conf = supportData[freqSet] / supportData[freqSet - conseq]
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:
Hmp1 = aprioriGen(H, m+1)
Hmp1 = calcConf(freqSet, Hmp1, supportData, brl, minConf)
if len(Hmp1) > 1:
rulesFromConseq(freqSet, Hmp1, supportData, brl, minConf)
def generateRules(L, supportData, minConf=0.7):
bigRuleList = []
for i in range(1, len(L)):
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
bRlist = generateRules(L1,suD2,0.1)
第八章复习思考题2
import pandas as pd
import numpy as np
origData = pd.read_csv("basketdata.txt")
goodsArray=[]
for indexs in origData.index:
goodsSeries=[]
if (origData.loc[indexs,'fruitveg']=="T"):
goodsSeries.append(1)
if (origData.loc[indexs,'freshmeat']=="T"):
goodsSeries.append(2)
if (origData.loc[indexs,'dairy']=="T"):
goodsSeries.append(3)
if (origData.loc[indexs,'cannedveg']=="T"):
goodsSeries.append(4)
if (origData.loc[indexs,'cannedmeat']=="T"):
goodsSeries.append(5)
if (origData.loc[indexs,'frozenmeal']=="T"):
goodsSeries.append(6)
if (origData.loc[indexs,'beer']=="T"):
goodsSeries.append(7)
if (origData.loc[indexs,'wine']=="T"):
goodsSeries.append(8)
if (origData.loc[indexs,'softdrink']=="T"):
goodsSeries.append(9)
if (origData.loc[indexs,'fish']=="T"):
goodsSeries.append(10)
if (origData.loc[indexs,'confectionery']=="T"):
goodsSeries.append(11)
goodsArray.append(goodsSeries)
if(origData.loc[indexs,'age']<30):
origData.loc[indexs,'ageGroup']=1
elif(origData.loc[indexs,'age']<50):
origData.loc[indexs,'ageGroup']=2
else:
origData.loc[indexs,'ageGroup']=3
L1, suppData = apriori(goodsArray, 0.15)
print("频繁项集L:", L1)
print("所有候选项集的支持度信息:", suppData)
print("所有规则的置信度信息:")
rules1 = generateRules(L1,suppData,0.15)
#简单分析不同年龄段的啤酒购买比例
beerData = origData[['beer','ageGroup']]
print(beerData.groupby([beerData['ageGroup'],beerData['beer']]).size())