这篇论文中提到的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. python基础----__next__和__iter__实现迭代器协议

    #_*_coding:utf-8_*_ __author__ = 'Linhaifeng' class Foo: def __init__(self,x): self.x=x def __iter__ ...

  2. 《剑指offer》— JavaScript(4)重建二叉树

    重建二叉树 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序 ...

  3. Calculating and saving space in PostgreSQL

    Q: I have a table in pg like so: CREATE TABLE t ( a BIGSERIAL NOT NULL, -- 8 b b SMALLINT, -- 2 b c ...

  4. hibernate、mybatis、spring data 的对比

    转: 1.概念: Hibernate :Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.着力 ...

  5. 新式类 VS 经典类

    一.概述 Python中支持多继承,也就是一个子类可以继承多个父类/基类.当一个调用一个自身没有定义的属性时,它是按照何种顺序去父类中寻找的呢?尤其是当众多父类中都包含有同名的属性,这就涉及到新式类 ...

  6. 前端PHP入门-012-回调函数[慎入]

    尽力而为,对于WEB前端和美工同学,比较难了!但是你们都学过JS的闭包等操作,那么这里也一定没有问题! 回调函数,可以配合匿名函数和变量函数实现更加优美.复杂的一种函数结构. 回调函数,就是在处理一个 ...

  7. ubuntu环境下添加中文输入法

    1.下载软件包 打开终端,输入命令 sudo apt-get install fcitx-table-wbpy 2.打开 system settings-> language support-& ...

  8. Spring使用注解方式就行事务管理

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  9. NOIP模拟1

    期望得分:100+100+100=300 实际得分:94+96+97=287 T1  #6090. 「Codeforces Round #418」尘封思绪 #include<cstdio> ...

  10. 【Atcoder】AGC 020 D - Min Max Repetition 二分+构造

    [题意]定义f(A,B)为一个字符串,满足: 1.长度为A+B,含有A个‘A',B个'B'. 2.最长的相同字符子串最短. 3.在满足以上2条的情况下,字典序最小. 例如, f(2,3) = BABA ...