Skip to content

Commit

Permalink
Merge pull request apachecn#369 from cclauss/fix-missing-python-imports
Browse files Browse the repository at this point in the history
谢谢啦 | Fix missing Python imports
  • Loading branch information
jiangzhonglian authored Apr 26, 2018
2 parents f08d4e2 + 0a26084 commit 89c994b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/py2.x/15.BigData_MapReduce/proximalSVM.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
@author: Peter/ApacheCN-xy/片刻
《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning
'''
import base64
import pickle

import numpy


def map(key, value):
# input key= class for one training example, e.g. "-1.0"
classes = [float(item) for item in key.split(",")] # e.g. [-1.0]
Expand Down
12 changes: 8 additions & 4 deletions src/py3.x/15.BigData_MapReduce/proximalSVM.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
@author: Peter/ApacheCN-xy/片刻
《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning
'''
import base64
import pickle

import numpy


def map(key, value):
# input key= class for one training example, e.g. "-1.0"
classes = [float(item) for item in key.split(",")] # e.g. [-1.0]
Expand All @@ -19,16 +23,16 @@ def map(key, value):

# create matrix E and vector e
e = numpy.matrix(numpy.ones(len(A)).reshape(len(A), 1))
E = numpy.matrix(numpy.append(A, -e, axis=1))
E = numpy.matrix(numpy.append(A, -e, axis=1))

# create a tuple with the values to be used by reducer
# and encode it with base64 to avoid potential trouble with '\t' and '\n' used
# as default separators in Hadoop Streaming
producedvalue = base64.b64encode(pickle.dumps( (E.T*E, E.T*D*e))
producedvalue = base64.b64encode(pickle.dumps((E.T*E, E.T*D*e)))

# note: a single constant key "producedkey" sends to only one reducer
# somewhat "atypical" due to low degree of parallism on reducer side
print "producedkey\t%s" % (producedvalue)
print("producedkey\t%s" % (producedvalue))

def reduce(key, values, mu=0.1):
sumETE = None
Expand All @@ -52,4 +56,4 @@ def reduce(key, values, mu=0.1):
# note: omega = result[:-1] and gamma = result[-1]
# but printing entire vector as output
result = sumETE.I*sumETDe
print "%s\t%s" % (key, str(result.tolist()))
print("%s\t%s" % (key, str(result.tolist())))
4 changes: 3 additions & 1 deletion src/py3.x/16.RecommenderSystems/test_evaluation_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import math
import random


def SplitData(data, M, k, seed):
test = []
Expand Down Expand Up @@ -69,4 +72,3 @@ def Popularity(train, test, N):
n += 1
ret /= n * 1.0
return ret

7 changes: 4 additions & 3 deletions src/py3.x/16.RecommenderSystems/test_基于物品.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import math
from operator import itemgetter


def ItemSimilarity1(train):
#calculate co-rated users between items
Expand Down Expand Up @@ -28,7 +31,7 @@ def ItemSimilarity2(train):
N[i] += 1
for j in users:
if i == j:
continue
continue
C[i][j] += 1 / math.log(1 + len(items) * 1.0)

#calculate finial similarity matrix W
Expand Down Expand Up @@ -60,5 +63,3 @@ def Recommendation2(train, user_id, W, K):
rank[j].weight += pi * wj
rank[j].reason[i] = pi * wj
return rank


4 changes: 3 additions & 1 deletion src/py3.x/16.RecommenderSystems/test_基于用户.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import math
from operator import itemgetter


def UserSimilarity1(train):
W = dict()
Expand Down Expand Up @@ -75,4 +78,3 @@ def Recommend(user, train, W):
continue
rank[i] += wuv * rvi
return rank

2 changes: 2 additions & 0 deletions src/py3.x/8.Regression/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,5 @@ def regression5():
# regression3()
# regression4()
# regression5()
pass

0 comments on commit 89c994b

Please sign in to comment.