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三-6:完整性约束
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性 主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KE ...
- jsp 的 4 种基本语法
1.JSP 注释 2.JSP 声明 3.JSP 表达式 4.JSP 脚本 JSP 注释: 注释格式: <%-- 注释内容 --%> 需要注意的是,JSP 的注释不会输出到 HTML 中. ...
- mybatis中的resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...
- python学习(九) 网络编程学习--简易网站服务器
python `网络编程`和其他语言都是一样的,服务器这块步骤为:`1. 创建套接字``2. 绑定地址``3. 监听该描述符的所有请求``4. 有新的请求到了调用accept处理请求` Python ...
- 手脱nSPack 2.1 - 2.5
1.载入PEID 使用核心扫描出的结果 nSPack 2.1 - 2.5 -> North Star/Liu Xing Ping 2.载入OD,一进来就是一个大跳转,F8跟着走 >- E9 ...
- Maven仓库--Nexus的配置使用
一.Nexus的作用 指定私服的中央地址.将自己的Maven项目指定到私服地址.从私服下载中央库的项目索引.从私服仓库下载依赖组件.将第三方项目jar上传到私服供其他项目组使用. 二.Nexus仓库 ...
- leetcode 刷题日志 2018-03-26
58. 最后一个单词的长度 分析:找最后一个非空格,向前找 int lengthOfLastWord(string s) { int i = s.find_last_not_of(' '); int ...
- Digging(DP)
ZOJ Problem Set - 3689 Digging Time Limit: 2 Seconds Memory Limit: 65536 KB When it comes to th ...
- <LC刷题二>回文字符串判断之leetcode125&234
其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...
- 【leetcode 简单】第十五题 加一
给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: ...