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

输入大约长这样,依次是

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

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

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

mapper:

  1. #!/usr/bin/env python
  2. import sys
  3. from itertools import product
  4.  
  5. def seq(start, end):
  6. return [range(start, i) for i in range(start, end + 2)]
  7.  
  8. def read_input(file):
  9. for line in file:
  10. yield line.split()
  11.  
  12. def main():
  13. data = read_input(sys.stdin)
  14. C = [a + b for a, b in product(seq(2, 4), seq(5, 7))]
  15. for e in data:
  16. for R in C:
  17. k = [e[i] for i in R]
  18. print "%s|%s\t%s" % (' '.join([str(i) for i in R]), ' '.join(k), e[1])
  19.  
  20. if __name__ == "__main__":
  21. main()

reducer:

  1. #!/usr/bin/env python
  2.  
  3. from itertools import groupby
  4. from operator import itemgetter
  5. import sys
  6.  
  7. def read_input(file):
  8. for line in file:
  9. yield line.rstrip().split('\t')
  10.  
  11. def main():
  12. data = read_input(sys.stdin)
  13. for key, group in groupby(data, itemgetter(0)):
  14. ids = set(uid for key, uid in group)
  15. print "%s\t%d" % (key, len(ids))
  16.  
  17. if __name__ == "__main__":
  18. 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. 嘘,如何激活更新的win10

    win10更新了,所以很坑的是以前的密钥又不管用了,系统和office都要重新激活,然而微软的更新就是很有恶意的,总之成功率堪忧. 还好看到了万能的网友的办法. slmgr.vbs /upk slmg ...

  2. mysql group_concat方法用法

    目前有三张表: 课程表: course,里面有student_id 学生表:     student,里面有 name 字段 中间表(课程和学生一对多): course_student, 里面有 co ...

  3. ios 逆向

    Theos https://www.jianshu.com/p/307243ea40e4 Dumpsdecrypted https://www.cnblogs.com/wangyaoguo/p/908 ...

  4. sloop公共函数之添加信号,定时器及socket

    1:添加信号 1.1 原型:sloop_handle sloop_register_signal(int sig, sloop_signal_handler handler, void * param ...

  5. 运行python时提示:ImportError: No module named plyvel ,ImportError No module named irc 解决过程:

    (当前python版本:2.7) 1.在git下载electrum-server: cd / git clone https://github.com/spesmilo/electrum-server ...

  6. Python数据生成pdf文件

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  7. libcurl移植到android

    一.总体概览 C库:libcurl 3.7 目标平台:android 编译平台:ubuntu 12 编译工具:ndk r7 or later 二.已知方法 1. 官网上给了两种方法,第一种方法是使用a ...

  8. OpenCV---超大图像二值化和空白区域过滤

    超大图像的二值化方法 1.可以采用分块方法, 2.先缩放处理就行二值化,然后还原大小 一:分块处理超大图像的二值化问题 def big_image_binary(image): print(image ...

  9. uva 1636 Headshot

    https://vjudge.net/problem/UVA-1636 首先在手枪里随机装一些子弹,然后抠了一枪,发现没有子弹.你希望下一枪也没有子弹,是应该直接再抠一枪(输出SHOOT)呢,还是随机 ...

  10. PHP扩展--vld查看opcode代码

    vld安装 wget http://pecl.php.net/get/vld-0.13.0.tgz tar zxvf vld-0.13.0.tgz cd vld-0.13.0 /usr/local/p ...