Apriori算法在购物篮分析中的运用
购物篮分析是一个很经典的数据挖掘案例,运用到了Apriori算法。下面从网上下载的一超市某月份的数据库,利用Apriori算法进行管理分析。例子使用Python+MongoDB
处理过程1 数据建模(将Excel中的数据写入到MongoDB数据库), 2 从数据库中读取数据进行分析。
Excel文件http://download.csdn.net/detail/artscrafts/6805689
案例配置文件 setting.py
data_source = 'supermarket.xls'
host = 'localhost'
port = 27017
db_name = 'shopping_basket'
items_name = 'goods_items'
record_name = 'transaction_record'
读取Excel数据到MongoDB中 load_basket.py
from xlrd import open_workbook
from pymongo import MongoClient
import setting wb = open_workbook(setting.data_source, encoding_override='utf-8')
client = MongoClient(setting.host, setting.port)
db = client[setting.db_name]
items = [] #read xls
def read_one_line(workbook, sheet_index=0, row_index=0, start_col_index=0):
sheet = workbook.sheets()[0]
max_row = sheet.nrows
max_col = sheet.ncols
start_col_index = (start_col_index if (start_col_index > 0 and start_col_index <= max_col) else max_col)
if row_index < 0 or row_index >= max_row:
raise IndexError()
for col_index in xrange(start_col_index, max_col):
yield sheet.cell(row_index, col_index).value #read xls
def readlines(workbook, sheet_index=0, start_row_index=0, end_row_index=None, start_col_index=0, end_col_index=None):
sheet = workbook.sheets()[sheet_index]
max_row = sheet.nrows
max_col = sheet.ncols
end_row_index = (end_row_index if end_row_index else max_row)
end_col_index = (end_col_index if end_col_index else max_col)
for row_index in xrange(start_row_index, end_row_index):
yield [sheet.cell(row_index, col_index).value for col_index in xrange(start_col_index, end_col_index)] #from xls to mongodb
def load_items():
collection = db[setting.items_name]
items_line = read_one_line(wb, row_index=1, start_col_index=1)
id = 1
tmp = []
for item in items_line:
if id % 100 == 0:
collection.insert(tmp)
tmp = []
tmp.append({'id':id, 'name':item})
items.append(item)
id += 1 # from xls to mongodb
def load_record():
collection = db[setting.record_name]
lines = readlines(wb,start_row_index=2, start_col_index = 1)
tmp = []
id = 1
for line in lines:
if id % 100 == 0:
collection.insert(tmp)
tmp = []
tmp.append({'id':id, 'items':[items[i] for i in xrange(len(line)) if line[i] == 'T']})
id += 1 def main():
print '........start loading........'
load_items()
load_record()
client.close()
print '.........end loading.........' if __name__ == '__main__':
main()
进行数据分析 analysis_basket.py
#Apriori
from pymongo import MongoClient
import setting client = MongoClient(setting.host, setting.port)
db = client[setting.db_name]
data = [] #from mongodb to items
def filldata():
collection = db[setting.record_name]
cur = collection.find()
for row in cur:
data.append(row['items']) def connect(items):
result = {}
keys = items.keys()
length = len(keys)
for i in range(length):
prev = keys[i][:len(keys[i]) - 1]
for j in range(i + 1, length):
tmp = keys[j][:len(keys[j]) - 1]
if prev == tmp:
key = keys[i] + (keys[j][len(keys[i]) - 1],)
result[key] = getsupp(key)
else:
break
return result def pruning(items, minsupp):
result = {}
for key in items.keys():
if items[key] >= minsupp:
result[key] = items[key]
return result def contain(par, sub):
for v in sub:
if not v in par:
return False
return True def getsupp(item):
supp = 0
for row in data:
if contain(row, item):
supp+=1
return supp def apriori(data, minsupp, k):
candidate_set = {}
for row in data:
for i in row:
key = (i,)
candidate_set[key] = candidate_set.get(key, 0) + 1
frequently_set = pruning(candidate_set, minsupp)
result = {}
result['k=1'] = frequently_set
for n in range(2, k):
candidate_set = connect(frequently_set)
frequently_set = pruning(candidate_set, minsupp)
if len(frequently_set) <= 1:
return result
result['K=' + str(n)] = frequently_set
return result def main():
filldata()
client.close()
res = apriori(data, 30, 8) if __name__ == '__main__':
main()
Apriori算法在购物篮分析中的运用的更多相关文章
- 数据挖掘算法之-关联规则挖掘(Association Rule)(购物篮分析)
在各种数据挖掘算法中,关联规则挖掘算是比較重要的一种,尤其是受购物篮分析的影响,关联规则被应用到非常多实际业务中,本文对关联规则挖掘做一个小的总结. 首先,和聚类算法一样,关联规则挖掘属于无监督学习方 ...
- 数据算法 --hadoop/spark数据处理技巧 --(5.移动平均 6. 数据挖掘之购物篮分析MBA)
五.移动平均 多个连续周期的时间序列数据平均值(按相同时间间隔得到的观察值,如每小时一次或每天一次)称为移动平均.之所以称之为移动,是因为随着新的时间序列数据的到来,要不断重新计算这个平均值,由于会删 ...
- R语言和数据分析十大:购物篮分析
提到数据挖掘,我们的第一个反应是之前的啤酒和尿布的故事听说过,这个故事是一个典型的数据挖掘关联规则.篮分析的传统线性回归之间的主要差别的差别,对于离散数据的相关性分析: 常见的关联规则: 关联规则:牛 ...
- 108_Power Pivot购物篮分析分组GENERATE之笛卡尔积、排列、组合
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 1.背景 昨天在看论坛帖子时候(帖子),看到一个关于SKU组合的问题,有很多M大佬都给出了处理方案,于是想用dax也写一个 ...
- 关联规则之Aprior算法(购物篮分析)
0.支持度与置信度 <mahout实战>与<机器学习实战>一起该买的记录数占所有商品记录总数的比例——支持度(整体) 买了<mahout实战>与<机器学习实战 ...
- Apriori算法第二篇----详细分析和代码实现
1 Apriori介绍 Apriori算法使用频繁项集的先验知识,使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描事务(交易)记录,找出所有的频繁1项集,该集合记做L1,然 ...
- 016 Spark中关于购物篮的设计,以及优化(两个点)
一:介绍 1.购物篮的定义 2.适用场景 3.相关概念 4.步骤 5.编程实现 6.步骤 二:程序 1.程序 package com.ibeifeng.senior.mba.association i ...
- Apriori算法第一篇
摘要: Apriori算法是产生k项高频项目组的一般手段.算法概要:首先产生k项高频项目集合Lk,自身链接形成k+1项的项目结合C(k+1),然后剪枝(去掉以前去掉的不满足支持度的高频),生成K=1项 ...
- Apriori算法的C++实现
Apriori是经典的购物篮分析算法.该算法用SQL实现难度较大,所以考虑用C++实现. 花了两天,代码例如以下.原创转载请注明出处 //Apriori.c #include<iostream& ...
随机推荐
- 【C++自我精讲】基础系列二 const
[C++自我精讲]基础系列二 const 0 前言 分三部分:const用法.const和#define比较.const作用. 1 const用法 const常量:const可以用来定义常量,不可改变 ...
- android常见错误-Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
在andorid进行调试的过程中,出现下面的错误: 解决方法: 修改一下应用程序描述符: <manifest xmlns:android="http://schemas.android ...
- 让Tomcat支持中文路径名和中文文件名
http://hdwangyi.iteye.com/blog/107709 Tomcat是Java开发者使用得较多的一个Web服务器,因为它占用资源小,运行速度快等特点,深受Java Web程序员的喜 ...
- android开发之bitmap使用
bitmap是android中重要的图像处理工具类,通过bitmap可以对图像进行剪切.旋转.缩放等操作,同时还可以指定格式和压缩质量保存图像文件. 一.拿到一个Bitmap对象 查看源码我们知道,B ...
- sqlite数据库修改及升级
今天是上班的第二天,听说我最近的任务就是改bug,唉,权当学习了,遇到的一些问题都记录下来. sqlite数据库是android中非常常用的数据库,今天帮别人改bug,遇到一些问题记录下来. 1.修改 ...
- Google Map API v2 (四)----- 导航路径
仍然是建议个异步小任务 private GetPathTask mGetPathTask = null; private void getGuidePath(LatLng origin){ if(mG ...
- windows向ubuntu过渡之常用软件安装
好久没有写博客了,介于最近上操作系统实验课,好多同学装上了ubuntu,网上的教程比较杂乱,下面我就总结分享一些安装完ubuntu要安装的常用软件,会持续更新... 1.搜狗拼音安装 (1)在安装输入 ...
- Linux网络相关命令小结
# ifconfig # ifup/ifdown # route -n # ip link show //显示本机所有接口信息 # traceroute # netstat //查看本机网络连接与后门 ...
- [原创]ie6,7中td和img之间有间隙
情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...
- ASP.NET MVC 自我总结的便捷开发实例
前言 工作了这么久了,接触ASP.NET MVC已经很久了,一直都想总结一下它的一些实用的,经常使用的一些技巧,但是因为一直都很懒,也不想总结,所以一直都没有好好写出来,趁着现在有这种冲劲,那么就先把 ...