本文整理汇总了Python中pyspark.mllib.recommendation.Rating方法的典型用法代码示例。如果您正苦于以下问题:Python recommendation.Rating方法的具体用法?Python recommendation.Rating怎么用?Python recommendation.Rating使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyspark.mllib.recommendation
的用法示例。
在下文中一共展示了recommendation.Rating方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def main():
movies = movies_original.map(parse_movies).cache()
ratings = ratings_original.map(parse_ratings).cache()
user_ratings = user_ratings_original.map(parse_user_ratings).filter(lambda x: x is not None).cache().coalesce(1)
user_ratings_list = user_ratings.collect()
standard_user_ratings = register_user_movies(user_ratings_list, movies).cache()
user_r = standard_user_ratings.map(lambda (m_id, m, rating): Rating(40000, int(m_id), float(rating)))
u_movies = standard_user_ratings.map(lambda (m_id, m, rating): (m_id, m)).collect()
r = ratings.map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2])))
r = r.union(user_r)
rank = 7
numIterations = 10
model = ALS.train(r, rank, numIterations)
testdata = sc.parallelize(list(set(movies.collect())-set(u_movies))).map(lambda x: (40000, int(x[0])))
predictions = model.predictAll(testdata).map(lambda x: (x[1], x[2])) # movie_id, score
results = movies.map(lambda (movie_id, m): (int(movie_id), m)).join(predictions)
outdata = results.map(lambda x: (x[0], x[1][0], x[1][1])).sortBy(lambda l: l[2], ascending=False)
outdata.saveAsTextFile(output)
示例2: test_get_recommended_mbids
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def test_get_recommended_mbids(self, mock_gen_rec, mock_get_mbids):
candidate_set = self.get_candidate_set()
params = self.get_recommendation_params()
limit = 2
rdd = candidate_set.rdd.map(lambda r: (r['user_id'], r['recording_id']))
mock_gen_rec.return_value = [Rating(user=2, product=2, rating=1.2)]
mock_get_mbids.return_value = utils.create_dataframe(Row(mb_recording_mbid='xxxx'), schema=None)
recommended_mbids = recommend.get_recommended_mbids(rdd, params, limit)
mock_gen_rec.assert_called_once_with(rdd, params, limit)
mock_get_mbids.assert_called_once_with(params, [2])
self.assertEqual(recommended_mbids, ['xxxx'])
mock_gen_rec.return_value = []
with self.assertRaises(RecommendationsNotGeneratedException):
_ = recommend.get_recommended_mbids(rdd, params, limit)
示例3: to_Rating
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def to_Rating(t):
return Rating(t[0], t[1], t[2])
示例4: test_als_ratings_serialize
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def test_als_ratings_serialize(self):
r = Rating(7, 1123, 3.14)
jr = self.sc._jvm.org.apache.spark.mllib.api.python.SerDe.loads(bytearray(ser.dumps(r)))
nr = ser.loads(bytes(self.sc._jvm.org.apache.spark.mllib.api.python.SerDe.dumps(jr)))
self.assertEqual(r.user, nr.user)
self.assertEqual(r.product, nr.product)
self.assertAlmostEqual(r.rating, nr.rating, 2)
示例5: test_als_ratings_id_long_error
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def test_als_ratings_id_long_error(self):
r = Rating(1205640308657491975, 50233468418, 1.0)
# rating user id exceeds max int value, should fail when pickled
self.assertRaises(Py4JJavaError, self.sc._jvm.org.apache.spark.mllib.api.python.SerDe.loads,
bytearray(ser.dumps(r)))
示例6: parse_dataset
# 需要导入模块: from pyspark.mllib import recommendation [as 别名]
# 或者: from pyspark.mllib.recommendation import Rating [as 别名]
def parse_dataset(row):
""" Convert each RDD element to object of class Rating.
Args:
row: An RDD row or element.
"""
return Rating(row['user_id'], row['recording_id'], row['count'])