naive cube implementation in python
这篇论文中提到的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的更多相关文章
- 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 ...
- Tree Implementation with Python
Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...
- [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 ...
- 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)
目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...
- 【机器学习速成宝典】模型篇05朴素贝叶斯【Naive Bayes】(Python版)
目录 先验概率与后验概率 条件概率公式.全概率公式.贝叶斯公式 什么是朴素贝叶斯(Naive Bayes) 拉普拉斯平滑(Laplace Smoothing) 应用:遇到连续变量怎么办?(多项式分布, ...
- [Data Structure] Linked List Implementation in Python
class Empty(Exception): pass class Linklist: class _Node: # Nonpublic class for storing a linked nod ...
- 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 ...
- python小工具
http://blog.csdn.net/pipisorry/article/details/46754515 python复制.删除文件代码.python代码出错重新启动 python遍历和删除指定 ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
随机推荐
- mysql三-1:存储引擎
一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...
- 《剑指offer》— JavaScript(6)旋转数组的最小数字
旋转数组的最小数字 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2, ...
- maven使用ss代理
把maven安装目录下的conf/settings.xml复制一份到~/.m2/下 在<proxies>标签中添加 <proxy> <id>socks5</i ...
- mysql ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)错误解决办法
我的电脑是win10,所用的是mysql5.7.14 近期在学习mysql数据库的时候,遇到了这个错误,我的密码错误了.突如其来的问题,很是蒙蔽,因为我没对数据库设置过密码.通过网上查询,可以通过进入 ...
- vmvare-centos7-vim
centos7网络连不上问题 http://geekflare.com/no-internet-connection-from-vmware-with-centos-7/ http://www.jia ...
- 使用rabbitmq消息队列
一.前言 在python中本身就是存在队列queue.一个是线程队列queue,另一个是进程multiprocessing中的队列Queue. 线程queue:只用于线程之间的数据交互 进程Queue ...
- git入门篇shell
什么是shell 在计算机科学中,Shell俗称壳,用来区别于Kernel(核),是指“提供使用者使用界面”的软件(命令解析器),它类似于windows系统下的cmd.exe, 它接收用户命令,然后调 ...
- 新Linux系统配置yum源
新的Linux系统安装好以后,yum的源还是需要配置一下的,我使用的是redhat6.6版本,同时为了不注册而使用更多的yum源的资源,也需要做一下的修改. 1. 删除redhat原有的yum源 # ...
- 【洛谷 P2553】 [AHOI2001]多项式乘法(FFT)
题目链接 简单处理一下输入,\(fft\)模板题. #include <cstdio> #include <cmath> #include <algorithm> ...
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...