forked from apachecn/ailearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrMean.py
More file actions
60 lines (51 loc) · 1.99 KB
/
Copy pathmrMean.py
File metadata and controls
60 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# coding:utf8
'''
Created on 2017-04-07
Update on 2017-06-20
@author: Peter/ApacheCN-xy/片刻
《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning
'''
from mrjob.job import MRJob
class MRmean(MRJob):
def __init__(self, *args, **kwargs): # 对数据初始化
super(MRmean, self).__init__(*args, **kwargs)
self.inCount = 0
self.inSum = 0
self.inSqSum = 0
# 接受输入数据流
def map(self, key, val): # 需要 2 个参数,求数据的和与平方和
if False:
yield
inVal = float(val)
self.inCount += 1
self.inSum += inVal
self.inSqSum += inVal*inVal
# 所有输入到达后开始处理
def map_final(self): # 计算数据的平均值,平方的均值,并返回
mn = self.inSum/self.inCount
mnSq = self.inSqSum/self.inCount
yield (1, [self.inCount, mn, mnSq])
def reduce(self, key, packedValues):
cumN, cumVal, cumSumSq = 0.0, 0.0, 0.0
for valArr in packedValues: # 从输入流中获取值
nj = float(valArr[0])
cumN += nj
cumVal += nj*float(valArr[1])
cumSumSq += nj*float(valArr[2])
mean = cumVal/cumN
var = (cumSumSq - 2*mean*cumVal + cumN*mean*mean)/cumN
yield (mean, var) # 发出平均值和方差
def steps(self):
"""
step方法定义执行的步骤。
执行顺序不必完全遵循map-reduce模式。
例如:
1. map-reduce-reduce-reduce
2. map-reduce-map-reduce-map-reduce
在step方法里,需要为mrjob指定mapper和reducer的名称。如果没有,它将默认调用mapper和reducer方法。
在mapper 和 mapper_final中还可以共享状态,mapper 或 mapper_final 不能 reducer之间共享状态。
"""
return ([self.mr(mapper=self.map, mapper_final=self.map_final, reducer=self.reduce,)])
if __name__ == '__main__':
MRmean.run()