6
6
@author: Peter/ApacheCN-xy/片刻
7
7
《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning
8
8
'''
9
+ import base64
10
+ import pickle
11
+
9
12
import numpy
10
13
14
+
11
15
def map (key , value ):
12
16
# input key= class for one training example, e.g. "-1.0"
13
17
classes = [float (item ) for item in key .split ("," )] # e.g. [-1.0]
@@ -19,16 +23,16 @@ def map(key, value):
19
23
20
24
# create matrix E and vector e
21
25
e = numpy .matrix (numpy .ones (len (A )).reshape (len (A ), 1 ))
22
- E = numpy .matrix (numpy .append (A , - e , axis = 1 ))
26
+ E = numpy .matrix (numpy .append (A , - e , axis = 1 ))
23
27
24
28
# create a tuple with the values to be used by reducer
25
29
# and encode it with base64 to avoid potential trouble with '\t' and '\n' used
26
30
# as default separators in Hadoop Streaming
27
- producedvalue = base64 .b64encode (pickle .dumps ( (E .T * E , E .T * D * e ))
31
+ producedvalue = base64 .b64encode (pickle .dumps ((E .T * E , E .T * D * e )))
28
32
29
33
# note: a single constant key "producedkey" sends to only one reducer
30
34
# somewhat "atypical" due to low degree of parallism on reducer side
31
- print "producedkey\t %s" % (producedvalue )
35
+ print ( "producedkey\t %s" % (producedvalue ) )
32
36
33
37
def reduce (key , values , mu = 0.1 ):
34
38
sumETE = None
@@ -52,4 +56,4 @@ def reduce(key, values, mu=0.1):
52
56
# note: omega = result[:-1] and gamma = result[-1]
53
57
# but printing entire vector as output
54
58
result = sumETE .I * sumETDe
55
- print "%s\t %s" % (key , str (result .tolist ()))
59
+ print ( "%s\t %s" % (key , str (result .tolist () )))
0 commit comments