这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=。=

输入大约长这样,依次是

index  userid  country  state  city  topic  category  product  sales
1    400141    3    78    3427    3    59    4967    4670.08
2 783984 1 34 9 1 5 982 5340.9
3 4945 1 47 1658 1 7 363 3065.37
4 468352 2 57 2410 2 37 3688 9561.13
5 553471 1 25 550 1 13 1476 3596.72
6 649149 1 9 234 1 12 1456 2126.29
...

输出的格式是这样,对于各个attr(用位置而不是名字表示)的各种value的搭配,输出对应group的measure的结果

<attr><attr><attr>...|<value><value>...    <measure>

mapper:

#!/usr/bin/env python
import sys
from itertools import product def seq(start, end):
return [range(start, i) for i in range(start, end + 2)] def read_input(file):
for line in file:
yield line.split() def main():
data = read_input(sys.stdin)
C = [a + b for a, b in product(seq(2, 4), seq(5, 7))]
for e in data:
for R in C:
k = [e[i] for i in R]
print "%s|%s\t%s" % (' '.join([str(i) for i in R]), ' '.join(k), e[1]) if __name__ == "__main__":
main()

reducer:

#!/usr/bin/env python

from itertools import groupby
from operator import itemgetter
import sys def read_input(file):
for line in file:
yield line.rstrip().split('\t') def main():
data = read_input(sys.stdin)
for key, group in groupby(data, itemgetter(0)):
ids = set(uid for key, uid in group)
print "%s\t%d" % (key, len(ids)) if __name__ == "__main__":
main()

课程设计选python就可以玩各种缩短代码的奇技淫巧了好嗨森……

naive cube implementation in python的更多相关文章

  1. Huffman Implementation with Python

    Huffman Implementation with Python 码表 Token Frequency a 10 e 15 i 12 s 3 t 4 space 13 n 1 生成 Huffman ...

  2. Tree Implementation with Python

    Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...

  3. [Data Structure] Stack Implementation in Python

    We can realize a Stack as an adaptation of a Python List. S.push(e)=L.append(e) S.pop()=L.pop() S.to ...

  4. 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)

    目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...

  5. 【机器学习速成宝典】模型篇05朴素贝叶斯【Naive Bayes】(Python版)

    目录 先验概率与后验概率 条件概率公式.全概率公式.贝叶斯公式 什么是朴素贝叶斯(Naive Bayes) 拉普拉斯平滑(Laplace Smoothing) 应用:遇到连续变量怎么办?(多项式分布, ...

  6. [Data Structure] Linked List Implementation in Python

    class Empty(Exception): pass class Linklist: class _Node: # Nonpublic class for storing a linked nod ...

  7. 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)

    6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python) Introduction Here’s a situation yo ...

  8. python小工具

    http://blog.csdn.net/pipisorry/article/details/46754515 python复制.删除文件代码.python代码出错重新启动 python遍历和删除指定 ...

  9. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

随机推荐

  1. 小Q与内存

    Portal --> broken qwq Description (这个描述好像怎么都精简不起来啊qwq) 大概是说你的计算机有1GB的物理内存,按照Byte寻址,其物理地址空间为\(0\si ...

  2. 配置:heartbeat+nginx+mysqld+drbd高可用笔记(OK)

    参考资料:http://www.centoscn.com/CentosServer/cluster/2015/0605/5604.html   背景需求: 使用heartbeat来做HA高可用,并且把 ...

  3. [转]C/C++作用域详解

    原文地址:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777433.html 作用域规则告诉我们一个变量的有效范围,它在哪儿创建,在 ...

  4. ASP.NET导入EXCEL方法汇总

    1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) { HttpResponse resp; ...

  5. [linux]安装code::blocks

    1.安装基本编译环境 $sudo apt-get install build-essential $sudo apt-get install gdb 2.安装codeblock $sudo apt-g ...

  6. Use of exceptionless, 作全局日志分布式记录处理

    Download latest release of exceptionless on github and deploy on Window server, by default exception ...

  7. MySql 快速去重方法

    1.复制需要去重的表 CREATE TABLE 新表 LIKE 旧表 ; 2.将需要去重的字段 设置为唯一union 索引 ALTER TABLE 表名 ADD UNIQUE(`字段`); 3.复制旧 ...

  8. 七牛云 PHP SDK服务器鉴权失败!参数解释

    昨天搞了一下午,用7牛官方的SDK demo 1.上传凭证 $policy = array( 'callbackUrl' => 'http://api.example.com/qiniu/upl ...

  9. Oracle笔记之用户管理

    1. 创建用户 创建用户使用create user语句,需要DBA权限: CREATE USER tom IDENTIFIED BY mot; 2. 更改用户密码 修改别人的密码需要DBA权限,或者a ...

  10. vue.js devtools-------调试vue.js的开发者插件

    vue.js devtools插件: 作用: 以往我们在进行测试代码的时候,直接在console进行查看,其实这个插件雷同于控制台,只不过在vue里面,将需要查看的数据存放在一个变量里面了~ 效果图: ...