做什么网站最赚钱,五金制品东莞网站建设,建设银行普卡申请网站,手机网站开发服务文章目录 0 赛题思路1 算法介绍2 FP树表示法3 构建FP树4 实现代码 建模资料 0 赛题思路
#xff08;赛题出来以后第一时间在CSDN分享#xff09;
https://blog.csdn.net/dc_sinor?typeblog
1 算法介绍
FP-Tree算法全称是FrequentPattern Tree算法#xff0c;就是频繁模… 文章目录 0 赛题思路1 算法介绍2 FP树表示法3 构建FP树4 实现代码 建模资料 0 赛题思路
赛题出来以后第一时间在CSDN分享
https://blog.csdn.net/dc_sinor?typeblog
1 算法介绍
FP-Tree算法全称是FrequentPattern Tree算法就是频繁模式树算法他与Apriori算法一样也是用来挖掘频繁项集的不过不同的是FP-Tree算法是Apriori算法的优化处理他解决了Apriori算法在过程中会产生大量的候选集的问题而FP-Tree算法则是发现频繁模式而不产生候选集。但是频繁模式挖掘出来后产生关联规则的步骤还是和Apriori是一样的。
常见的挖掘频繁项集算法有两类一类是Apriori算法另一类是FP-growth。Apriori通过不断的构造候选集、筛选候选集挖掘出频繁项集需要多次扫描原始数据当原始数据较大时磁盘I/O次数太多效率比较低下。FPGrowth不同于Apriori的“试探”策略算法只需扫描原始数据两遍通过FP-tree数据结构对原始数据进行压缩效率较高。
FP代表频繁模式Frequent Pattern) 算法主要分为两个步骤FP-tree构建、挖掘频繁项集。
2 FP树表示法
FP树通过逐个读入事务并把事务映射到FP树中的一条路径来构造。由于不同的事务可能会有若干个相同的项因此它们的路径可能部分重叠。路径相互重叠越多使用FP树结构获得的压缩效果越好如果FP树足够小能够存放在内存中就可以直接从这个内存中的结构提取频繁项集而不必重复地扫描存放在硬盘上的数据。
一颗FP树如下图所示 通常FP树的大小比未压缩的数据小因为数据的事务常常共享一些共同项在最好的情况下所有的事务都具有相同的项集FP树只包含一条节点路径当每个事务都具有唯一项集时导致最坏情况发生由于事务不包含任何共同项FP树的大小实际上与原数据的大小一样。
FP树的根节点用φ表示其余节点包括一个数据项和该数据项在本路径上的支持度每条路径都是一条训练数据中满足最小支持度的数据项集FP树还将所有相同项连接成链表上图中用蓝色连线表示。
为了快速访问树中的相同项还需要维护一个连接具有相同项的节点的指针列表headTable每个列表元素包括数据项、该项的全局最小支持度、指向FP树中该项链表的表头的指针。
3 构建FP树
现在有如下数据 FP-growth算法需要对原始训练集扫描两遍以构建FP树。
第一次扫描过滤掉所有不满足最小支持度的项对于满足最小支持度的项按照全局最小支持度排序在此基础上为了处理方便也可以按照项的关键字再次排序。
第二次扫描构造FP树。
参与扫描的是过滤后的数据如果某个数据项是第一次遇到则创建该节点并在headTable中添加一个指向该节点的指针否则按路径找到该项对应的节点修改节点信息。具体过程如下所示 从上面可以看出headTable并不是随着FPTree一起创建而是在第一次扫描时就已经创建完毕在创建FPTree时只需要将指针指向相应节点即可。从事务004开始需要创建节点间的连接使不同路径上的相同项连接成链表。
4 实现代码
def loadSimpDat():simpDat [[r, z, h, j, p],[z, y, x, w, v, u, t, s],[z],[r, x, n, o, s],[y, r, x, z, q, t, p],[y, z, x, e, q, s, t, m]]return simpDatdef createInitSet(dataSet):retDict {}for trans in dataSet:fset frozenset(trans)retDict.setdefault(fset, 0)retDict[fset] 1return retDictclass treeNode:def __init__(self, nameValue, numOccur, parentNode):self.name nameValueself.count numOccurself.nodeLink Noneself.parent parentNodeself.children {}def inc(self, numOccur):self.count numOccurdef disp(self, ind1):print( * ind, self.name, , self.count)for child in self.children.values():child.disp(ind 1)def createTree(dataSet, minSup1):headerTable {}#此一次遍历数据集 记录每个数据项的支持度for trans in dataSet:for item in trans:headerTable[item] headerTable.get(item, 0) 1#根据最小支持度过滤lessThanMinsup list(filter(lambda k:headerTable[k] minSup, headerTable.keys()))for k in lessThanMinsup: del(headerTable[k])freqItemSet set(headerTable.keys())#如果所有数据都不满足最小支持度返回None, Noneif len(freqItemSet) 0:return None, Nonefor k in headerTable:headerTable[k] [headerTable[k], None]retTree treeNode(φ, 1, None)#第二次遍历数据集构建fp-treefor tranSet, count in dataSet.items():#根据最小支持度处理一条训练样本key:样本中的一个样例value:该样例的的全局支持度localD {}for item in tranSet:if item in freqItemSet:localD[item] headerTable[item][0]if len(localD) 0:#根据全局频繁项对每个事务中的数据进行排序,等价于 order by p[1] desc, p[0] descorderedItems [v[0] for v in sorted(localD.items(), keylambda p: (p[1],p[0]), reverseTrue)]updateTree(orderedItems, retTree, headerTable, count)return retTree, headerTabledef updateTree(items, inTree, headerTable, count):if items[0] in inTree.children: # check if orderedItems[0] in retTree.childreninTree.children[items[0]].inc(count) # incrament countelse: # add items[0] to inTree.childreninTree.children[items[0]] treeNode(items[0], count, inTree)if headerTable[items[0]][1] None: # update header tableheaderTable[items[0]][1] inTree.children[items[0]]else:updateHeader(headerTable[items[0]][1], inTree.children[items[0]])if len(items) 1: # call updateTree() with remaining ordered itemsupdateTree(items[1:], inTree.children[items[0]], headerTable, count)def updateHeader(nodeToTest, targetNode): # this version does not use recursionwhile (nodeToTest.nodeLink ! None): # Do not use recursion to traverse a linked list!nodeToTest nodeToTest.nodeLinknodeToTest.nodeLink targetNodesimpDat loadSimpDat()
dictDat createInitSet(simpDat)
myFPTree,myheader createTree(dictDat, 3)
myFPTree.disp()上面的代码在第一次扫描后并没有将每条训练数据过滤后的项排序而是将排序放在了第二次扫描时这可以简化代码的复杂度。
控制台信息 建模资料
资料分享: 最强建模资料 文章转载自: http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn