python3 aproiri算法(涉及字典来统计项集tupl作为key,int转化为tuple)
来源:互联网 发布:吉林大学网络自助中心 编辑:程序博客网 时间:2024/06/03 19:06
直接上代码,代码中都有注释:
# coding =utf-8import reimport sysimport bisectimport mathdef loadData(): Data = [[1,1,0,0,1],[1,1,0,0,0],[0,1,0,1,0],[1,1,0,1,0],[1,0,1,0,0],[1,1,1,0,1], [1,1,1,0,0],[0,1,0,0,1],[0,1,1,1,0],[0,0,1,1,0]] return Datadef frequent_1_itemSet(D,threshold): index = set( range(len(D[0]))) #生成属性的索引 dict_index={}#属性索引与该属性的统计次数 形成字典 for i in index: #列扫描 for item in D: #行扫描 if 1==item[i]: a=(i,) #int转化为tuple dict_index[a]=dict_index.get(a,0)+1 A=[] for key in dict_index.keys(): if dict_index[key]<threshold: A.append(key) for key in A: del dict_index[key] #删除非频繁项及对应的索引 #index.remove(key) return dict_index #也可以返回字典,或集合,后面来看 还是返回字典好些def aproiri_gen(D,IDset,theshold,k=1): if 0!=len(IDset): #这个条件多余的,因为后面有一个判断空集 item =[] if (type(IDset) is dict): for i in IDset.keys(): for j in IDset.keys(): #字典的key对应为项集的索引tuple,字典key不能改,所以不能为list(w3c中有这句话) a = len(set(i) & set(j)) #集合交集的个数 b = len(set(i) | set(j)) #集合并集的个数 if a==k-1 and b==k+1: #这两个条件其实是等价的 item.append(tuple(set(i)|set(j))) #所有二项的组合,i,j为索引, #统计 dict_count={} for i1 in D: #行 for j1 in item: #列 result = 1 for k in range(len(j1)): result *= i1[j1[k]] if result==1: #因为数据是0,1,所有乘积为1则表明所有项都为1 dict_count[j1] = dict_count.get(j1,0)+1 A=[] for key in dict_count.keys(): if dict_count[key]<theshold: a.append(key) for key in A: #统一删除 del dict_count[key] if 0!=len(dict_count): #结束条件,如果新的频繁集为空,则不进行迭代,返回旧的频繁项 print(k) print(dict_count) #打印每一次的项集 aproiri_gen(D,dict_count,theshold,k+1) else: print('error')def main(): data = loadData() threshold = 2 IDset=frequent_1_itemSet(data,threshold) max_item=aproiri_gen(data,IDset,threshold) print(max_item)if __name__=='__main__': main()
0 0
- python3 aproiri算法(涉及字典来统计项集tupl作为key,int转化为tuple)
- python3判断字典、列表、元组为空以及字典是否存在某个key的方法
- 使用自定义配置节,使配置项转化为字典集(.NET)
- 列表转化为字典
- 将String转化为int(android)
- char* 转化为int
- 字符串转化为int
- int转化为long
- int 转化为byte
- int 转化为byte
- int转化为string
- string转化为int
- Python3 元组(tuple)
- python如何将字典中的值转化为list结构,以词频统计为例
- python3中字典key取值
- python2代码转化为python3
- Python 2转化为Python3
- 炉石传说 (转化为匈牙利算法来做的)
- (第二讲)从服务器下载图片
- HTML5 les1
- wamp的最新apache2.4.9配置httpd.conf允许外部访问
- java中类加载路径和项目根路径获取的几种方式
- 位图管理、图片下载缓存、管理图片内存(一) 高效展示图片系列概述
- python3 aproiri算法(涉及字典来统计项集tupl作为key,int转化为tuple)
- Letter Combinations of a Phone number
- hibernate中各个jar包的含义和关系
- [转] 关于EJB分析
- 《c程序设计语言》读书笔记-4.2-扩充atof函数
- SVN基础命令手册
- 自定义下拉刷新控件-CBStoreHouseRefreshControl
- Xtrabackup 安装使用
- SVN基础命令使用