當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。