Skip to content

Commit

Permalink
add more type check and conversion for user_product
Browse files Browse the repository at this point in the history
  • Loading branch information
davies committed Sep 18, 2014
1 parent bd738ab commit 032cd62
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/pyspark/mllib/recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def predict(self, user, product):

def predictAll(self, user_product):
assert isinstance(user_product, RDD), "user_product should be RDD of (user, product)"
first = user_product.first()
if isinstance(first, list):
user_product = user_product.map(tuple)
first = tuple(first)
assert type(first) is tuple and len(first) == 2, \
"user_product should be RDD of (user, product)"
if any(isinstance(x, str) for x in first):
user_product = user_product.map(lambda (u, p): (int(x), int(p)))
first = tuple(map(int, first))
assert all(type(x) is int for x in first), "user and product in user_product shoul be int"
sc = self._context
tuplerdd = sc._jvm.SerDe.asTupleRDD(user_product._to_java_object_rdd().rdd())
jresult = self._java_model.predict(tuplerdd).toJavaRDD()
Expand Down

0 comments on commit 032cd62

Please sign in to comment.