python实现关联规则

  代码中Ci表示候选频繁i项集,Li表示符合条件的频繁i项集
  
  # coding=utf-8
  
  def createC1(dataSet): # 构建所有1项候选项集的集合
  
  C1 = []
  
  for transaction in dataSet:
  
  for item in transaction:
  
  if [item] not in C1:
  
  C1.append([item]) # C1添加的是列表,对于每一项进行添加,[[1], [2], [3], [4], [5]]
  
  #print('C1:',C1)
  
  return list(map(frozenset, C1)) # 使用frozenset,被“冰冻”的集合,为后续建立字典key-value使用。
  
  ###由候选项集生成符合最小支持度的项集L。参数分别为数据集、候选项集列表,最小支持度
  
  ###如
  
  ###C3: [frozenset({1, 2, 3}), frozenset({1, 3, 5}), frozenset({2, 3, 5})]
  
  ###L3: [frozenset({2, 3, 5})]
  
  def scanD(D, Ck, minSupport):
  
  ssCnt = {}
  
  for tid in D: # 对于数据集里的每一条记录
  
  for can in Ck: # 每个候选项集can
  
  if can.issubset(tid): # 若是候选集can是作为记录的子集,那么其值+1,对其计数
  
  if not ssCnt.__contains__(can): # ssCnt[can] = ssCnt.get(can,0)+1一句可破,没有的时候为0,加上1,有的时候用get取出,加1
  
  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(0, key) # 超过最小支持度的项集,将其记录下来。
  
  supportData[key] = support
  
  return retList, supportData
  
  ###由Lk生成K项候选集Ck
  
  ###如由L2: [frozenset({3, 5}), frozenset({2, 5}), frozenset({2, 3}), frozenset({1, 3})]
  
  ###生成
  
  ###C3: [frozenset({1, 2, 3}), frozenset({1, 3, 5}), frozenset({2, 3, 5})]
  
  def aprioriGen(Lk, k):
  
  retList = []
  
  lenLk = len(Lk)
  
  for i in range(lenLk):
  
  for j in range(i + 1,lenLk):
  
  if len(Lk[i] | Lk[j])==k:
  
  retList.append(Lk[i] | Lk[j])
  
  return list(set(retList))
  
  ####生成所有频繁子集
  
  def apriori(dataSet, minSupport=0.5):
  
  C1 = createC1(dataSet)
  
  D = list(map(set, dataSet))
  
  L1, supportData = scanD(D, C1, minSupport)
  
  L = [L1] # L将包含满足最小支持度,即经过筛选的所有频繁n项集,这里添加频繁1项集
  
  k = 2
  
  while (len(L[k - 2]) > 0): # k=2开始,由频繁1项集生成频繁2项集,直到下一个打的项集为空
  
  Ck = aprioriGen(L[k - 2], k)
  
  Lk, supK = scanD(D, Ck, minSupport)
  
  supportData.update(supK) # supportData为字典,存放每个项集的支持度,并以更新的方式加入新的supK
  
  L.append(Lk)
  
  k += 1
  
  return L, supportData
  
  if __name__ == "__main__":
  
  dataSet = [[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]
  
  D = list(map(set, dataSet))
  
  L,suppData = apriori(dataSet)
  
  print('L:',L)
  
  print('suppData:',suppData)
  
  '''
  
  C1 = createC1(dataSet)
  
  L1, supportData1 = scanD(D, C1, 0.5)
  
  print('C1:',C1)
  
  print('L1:',L1)
  
  print('supportData1:',supportData1)
  
  C2 = aprioriGen(L1, 2)
  
  L2, supportData2 = scanD(D, C2, 0.5)
  
  print('C2:',C2)
  
  print('L2:',L2)
  
  print('supportData2:www.gcyl152.com/',supportData2)
  
  C3 = aprioriGen(L2, 3)
  
  L3, supportData3 www.michenggw.com= scanD(D, C3, 0.5)
  
  print('C3:',C3)
  
  print('L3:',L3)
  
  print('supportData3:',supportData3)
  
  '''
  
  最终得到的所有支持度大于0.5的频繁子集及其支持度如下:
  
          frozenset({1})www.mcyllpt.com/ : 0.5, 
  
          frozenset({3}): 0.75, 
  
          frozenset({4}): 0.25, 
  
          frozenset({2}): 0.75, 
  
          frozenset({5}): 0.75, 
  
          frozenset({1, 3}): 0.5, 
  
          frozenset({2, 3}): 0.5, 
  
          frozenset({2, 5}): 0.75, 
  
          frozenset({3, 5}): 0.5, 
  
          frozenset({1, 2}): 0.25, 
  
          frozenset({1, 5}): 0.25, 
  
          frozenset({2, 3, 5}): 0.5, 
  
          frozenset({1, 2, 3}): 0.25, 
  
          frozenset({1, 3, 5}): 0.25

转载于:https://www.cnblogs.com/qwangxiao/p/10121889.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278677.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Progressive Web App(PWA)

Progressive Web App一、 PWA 宣传 : Reliable ( 可靠的 )、Fast( 快速的 )、Engaging( 可参与的 )二、什么是Progressive三、为什么我们需要Progressive Web App一、 PWA 宣传 : Re…

travis-cli 使用

1. 添加项目登录 travis 选择对应项目即可 2. 添加持续集成文件.travis.ymllanguage: node_js node_js:- "node" before_install: - npm install -g jspm - jspm install script: - jspm bundle lib/main --inject备注:这是一个jspm 项目 3. 构建travis 是…

在Windows Media Center中收听超过100,000个广播电台

A cool feature in Windows 7 Media Center is the ability to listen to local FM radio. But what if you don’t have a tuner card that supports a connected radio antenna? The RadioTime plugin solves the problem by allowing access to thousands of online radio …

vue项目中按需引入viewUI

viewUI一、按需引入二、忽略eslint编译器检测和编译检测1.忽略编译器检测2.编译器中忽略一、按需引入 npm install babel-plugin-import --save-dev // .babelrc1 { “plugins”: [[“import”, { “libraryName”: “view-design”, “libraryDirectory”: “src/components”…

IntelliJ IDEA——数据库集成工具(Database)的使用

idea集成了一个数据库管理工具,可以可视化管理很多种类的数据库,意外的十分方便又好用。这里以oracle为例配置。 1、配置 在窗口的右边有个Database按钮,点击。 如果没有,请点击上方的View(视图)-Tool Windows(工具窗口)-Database…

为什么VC经常输出烫烫烫烫烫烫烫烫

在Debug 模式下, VC 会把未初始化的栈内存全部填成0xcc,当字符串看就是 烫烫烫烫……会把未初始化的堆内存全部填成0xcd,当字符串看就是 屯屯屯屯……可以让我们方便地看出那些内存没初始化但是Release 模式下不会有这种附加动作,…

代码评审会议_如何将电话会议(和访问代码)另存为联系人

代码评审会议Dialing a conference call doesn’t have to be a tedious process. Your iPhone or Android phone can automatically dial into the call and enter a confirmation code for you. You just have to create a special type of contact. 拨打电话会议不一定是一个…

Vuex使用总结

Vuex综合使用一、仓库1.主仓库2.子仓库二、使用1.全局(index.js和未开启命名空间的子仓库)2.子仓库(子仓库定义了namespaced: true),仓库名:home3.使用strict严格模式(建议)三、批量…

好未来提前批

好未来提前批(注:转载于牛客网) 一面(25minutes) 1.创建对象的几种方式2.Jsp九大隐式对象3.自己封装的持久层框架用过么4.Spring ioc让你实现怎么实现呢(工厂反射,我半年前写过,忘记了)5.Aop的实…

Nginx服务学习(6)-日志模块

日志模块的说明 日志的默认路径:error_log /var/log/nginx/error.log warn; warn是指日志的等级,一般有debug, info, notice, warn, error, crit。access_log /var/log/nginx/access.log main; main是指访问日志记录的格式信息,在…

vue mock模拟后台接口数据

vue mock一、Json server二、Mock 服务1.安装2.创建 Mock3.main.js引入4.组件中axure请求一、Json server 轻量级,将已有的json文件跑在服务器上供前端调用 npm install -g json-server 启动JSON数据服务器: json-server --watch json文件名 或 json-se…

个人站立会议-----20181216

继续阅读测量程序设计这本书,并根据测量平差基础中的知识编写多个已知点水准网的间接平差,结果总是差些,询问过老师之后,才知道在程序中要增加检索闭合欢或闭合线段的条件,正在改进中 转载于:https://www.cnblogs.com/…

使用iOS 4越狱iPhone或iPod Touch

In case you haven’t heard the news over the past couple of days, there is now an incredibly easy way to jailbreak your iPod Touch or iPhone running iOS 4. Here we will take a look at how easy the process is. 如果您在过去的几天里没有听到这个消息&#xff0c…

[转] 深入理解React 组件状态(State)

React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据。 一. 如何定义State 定义一个合适的State,是正…

vue-router query,parmas,meta传参

1.query,显示在导航栏?后,相当于get请求传参 this.router.push({path:/login,query:{ redirect:/home}}) this.router.push({name:Login,query:{ redirect:/home}})2.parmas,不会显示,相当于post请求传参,…

Keras 获取中间某一层输出

1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict. 1 #codingutf-82 import seaborn as sbn3 import pylab as plt4 import theano5 from keras.models import Sequential6 fr…

在Windows 7中禁用或修改Aero Peek的“延迟时间”

Are you looking for an easy way to modify the “delay time” for Aero Peek in Windows 7 or perhaps want to disable the feature altogether? Then see how simple it is to do either with the Desktop Peek Tweak. 您是否正在寻找一种简便的方法来修改Windows 7中Aer…

php内核分析(六)-opcode

这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux 查看opcode php是先把源码解析成opcode,然后再把opcode传递给zend_vm进行执行的。 // 一个opcode的结构 struct _zend_op {const void *handler; // opcode对应的执行函数,每个opcod…

vue 监听路由变化

一.watch监听$route($router的对象) // 监听,当路由发生变化的时候执行 watch:{$route(to,from){console.log(to.path);} },// 监听,当路由发生变化的时候执行 watch: {$route: {handler: function(val, oldVal){console.log(val);},// 深度观察监听dee…

音频剪切_音频编辑入门指南:剪切,修剪和排列

音频剪切Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Knowing how to cut and trim tracks is basic audio editing and is a fundamental starting point for making more elaborate arrangements. 大胆的新手通常从崇高的项…