查看: 2730|回复: 2

[原创] 关于数据挖掘关联规则的Oracle实现

[复制链接]
论坛徽章:
181
慢羊羊
日期:2015-03-04 14:19:442015年新春福章
日期:2015-03-06 11:57:31
跳转到指定楼层
1#
发表于 2010-7-8 16:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
关于数据挖掘关联规则的Oracle实现

呵呵,前几天拿到了数据挖掘基础教程一书,感觉部分算法是基于统计学的原理的,而统计学是可以通过Oracle来实现
其次是为了观看德国vs西班牙的世界杯比赛,来了一点小小的兴致,动手写点小脚本。
不过本文只是为了实现而实现的,没有做任何优化,有兴趣的话,大家可以玩一玩


关于数据挖掘关联规则的材料
http://baike.baidu.com/view/1076817.htm?fr=ala0_1
关联规则是形如X→Y的蕴涵式,
其中且, X和Y分别称为关联规则的先导(antecedent或left-hand-side, LHS)和后继(consequent或 right-hand-side, RHS) 。
关联规则在D中的支持度(support)是D中事务同时包含X、Y的百分比,即概率; =X^Y/D
置信度(confidence)是包含X的事务中同时又包含Y的百分比,即条件概率。  =(X^Y)/X
关联规则是有趣的,如果满足最小支持度阈值和最小置信度阈值。
若给定最小支持度α = n,最小置信度β = m,则分别通过以上的X^Y/D和(X^Y)/X,可获知是否存在关联

使用的原始数据


反范式后的数据


带统计项


--创建各个购买单元项视图
create view distinct_trans as select distinct tranobject from purchase;
--创建各个事务内部的购买单元项
create view all_trans as
--用wm_concat函数
SELECT tranid,MAX(tranobjects) tranobjects
  FROM (select tranid,WMSYS.WM_CONCAT(tranobject) OVER(PARTITION BY tranid ORDER BY tranobject) tranobjects
         from purchase
       )
group by tranid;
--或者使用sys_connect_by_path
create view all_trans as
select tranid,substr(tranobjects,2) tranobjects from  --格式化前面的逗号和空格
(
  select distinct tranid,FIRST_VALUE(tranobjects) OVER(PARTITION BY tranid ORDER BY levels desc ) AS tranobjects  --保留最大的那个
   from
   (
     select tranid,sys_connect_by_path(tranobject,',') tranobjects,level levels --各购买事务的内部排列组合
       from purchase
    connect by tranid=prior tranid and tranobject<prior tranobject
   )
)
--对所有购买单元项进行排列组合,即数据挖掘的X^Y项
create view all_zuhe as
select substr(sys_connect_by_path(tranobject,','),2) zuhe
  from (select distinct tranobject from purchase)
connect by nocycle tranobject<prior tranobject;

select * from all_zuhe
--筛选出符合要求的排列组合,即数据挖掘的X项和Y项
create view full_zuhe as
select a.zuhe X,b.zuhe Y from all_zuhe a,all_zuhe b
where instr(a.zuhe,b.zuhe)=0 and instr(b.zuhe,a.zuhe)=0
and not exists(select 1 from distinct_trans c
                 where instr(a.zuhe,c.tranobject)>0 and instr(b.zuhe,c.tranobject)>0)

select * from full_zuhe   

create or replace view tongji as
select xy,xy_total,x,x_total,y,y_total,transtotal from
(
  select y||','||x xy,
         (select count(*) from all_trans a where instr(a.tranobjects,c.x||','||c.y)>0 or instr(a.tranobjects,c.y||','||c.x)>0) xy_total, --包含xy的事务数
         y,
         (select count(*) from all_trans b where instr(b.tranobjects,c.y)>0) y_total, --包含y的事务数
         x,
         (select count(*) from all_trans b where instr(b.tranobjects,c.x)>0) x_total, --包含x的事务数
         d.transtotal  --总事务数
   from full_zuhe c,(select count(distinct tranid) transtotal from purchase) d
  order by xy_total desc,x_total desc
)

select * from tongji where xy_total>=3 and y_total>=3
原始数据.JPG (12.67 KB)
2010-7-8 01:46
反范式后数据.JPG (10.66 KB)
2010-7-8 01:46
待统计项.JPG (52.19 KB)
2010-7-8 01:46
论坛徽章:
0
2#
发表于 2010-7-9 17:52 | 只看该作者
概率没算?

使用道具 举报

回复
论坛徽章:
181
慢羊羊
日期:2015-03-04 14:19:442015年新春福章
日期:2015-03-06 11:57:31
3#
 楼主| 发表于 2010-7-12 07:37 | 只看该作者
原帖由 sinshan 于 2010-7-9 17:52 发表
概率没算?


自己组合各种概率
create or replace view tongji as
select xy,xy_total,x,x_total,y,y_total,transtotal from
(
  select y||','||x xy,
         (select count(*) from all_trans a where instr(a.tranobjects,c.x||','||c.y)>0 or instr(a.tranobjects,c.y||','||c.x)>0) xy_total, --包含xy的事务数
         y,
         (select count(*) from all_trans b where instr(b.tranobjects,c.y)>0) y_total, --包含y的事务数
         x,
         (select count(*) from all_trans b where instr(b.tranobjects,c.x)>0) x_total, --包含x的事务数
         d.transtotal  --总事务数
   from full_zuhe c,(select count(distinct tranid) transtotal from purchase) d
  order by xy_total desc,x_total desc
)

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表