Skip to content

Commit 032cd62

Browse files
committed
add more type check and conversion for user_product
1 parent bd738ab commit 032cd62

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

python/pyspark/mllib/recommendation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def predict(self, user, product):
6666

6767
def predictAll(self, user_product):
6868
assert isinstance(user_product, RDD), "user_product should be RDD of (user, product)"
69+
first = user_product.first()
70+
if isinstance(first, list):
71+
user_product = user_product.map(tuple)
72+
first = tuple(first)
73+
assert type(first) is tuple and len(first) == 2, \
74+
"user_product should be RDD of (user, product)"
75+
if any(isinstance(x, str) for x in first):
76+
user_product = user_product.map(lambda (u, p): (int(x), int(p)))
77+
first = tuple(map(int, first))
78+
assert all(type(x) is int for x in first), "user and product in user_product shoul be int"
6979
sc = self._context
7080
tuplerdd = sc._jvm.SerDe.asTupleRDD(user_product._to_java_object_rdd().rdd())
7181
jresult = self._java_model.predict(tuplerdd).toJavaRDD()

0 commit comments

Comments
 (0)