当前位置: 首页>>代码示例>>Python>>正文


Python recommendation.Rating方法代码示例

本文整理汇总了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) 
开发者ID:hanhanwu,项目名称:Hanhan-Spark-Python,代码行数:23,代码来源:movie_recommendations.py

示例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) 
开发者ID:metabrainz,项目名称:listenbrainz-server,代码行数:20,代码来源:test_recommend.py

示例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]) 
开发者ID:hanhanwu,项目名称:Hanhan-Spark-Python,代码行数:4,代码来源:als.py

示例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) 
开发者ID:runawayhorse001,项目名称:LearningApacheSpark,代码行数:9,代码来源:tests.py

示例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))) 
开发者ID:runawayhorse001,项目名称:LearningApacheSpark,代码行数:7,代码来源:tests.py

示例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']) 
开发者ID:metabrainz,项目名称:listenbrainz-server,代码行数:9,代码来源:train_models.py


注:本文中的pyspark.mllib.recommendation.Rating方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。