當前位置: 首頁>>代碼示例>>Python>>正文


Python ALS.train方法代碼示例

本文整理匯總了Python中pyspark.mllib.recommendation.ALS.train方法的典型用法代碼示例。如果您正苦於以下問題:Python ALS.train方法的具體用法?Python ALS.train怎麽用?Python ALS.train使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyspark.mllib.recommendation.ALS的用法示例。


在下文中一共展示了ALS.train方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: main

# 需要導入模塊: from pyspark.mllib.recommendation import ALS [as 別名]
# 或者: from pyspark.mllib.recommendation.ALS import train [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: main

# 需要導入模塊: from pyspark.mllib.recommendation import ALS [as 別名]
# 或者: from pyspark.mllib.recommendation.ALS import train [as 別名]
def main():
    training_data = sc.textFile(training_inputs)
    testing_data = sc.textFile(testing_inputs)

    training_ratings = training_data.map(get_tuple).cache()
    testing_ratings = testing_data.map(get_tuple).cache()
    testing_all = testing_ratings.map(lambda (uid, mid, rating): (uid, mid)).cache()
    ratings = testing_ratings.map(to_Rating)


    ranks = [2, 4, 8, 16, 32, 64, 128, 256]
    reg_params = [0.1, 0.01]

    for i in range(len(reg_params)):
        RMSES = []
        for rank in ranks:
            model = ALS.train(training_ratings, rank=rank, lambda_=reg_params[i], seed=10)
            predictions = model.predictAll(testing_all).map(lambda r: ((r[0], r[1]), r[2]))
            ratesAndPreds = ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions)
            MSE = ratesAndPreds.map(lambda r: (r[1][0] - r[1][1])**2).mean()
            RMSE = math.sqrt(MSE)
            RMSES.append(RMSE)
        plt.plot(range(len(ranks)), RMSES, label=str(reg_params[i]))

    plt.xticks(range(len(ranks)), ranks, size='small')
    plt.legend()
    plt.show() 
開發者ID:hanhanwu,項目名稱:Hanhan-Spark-Python,代碼行數:29,代碼來源:als.py


注:本文中的pyspark.mllib.recommendation.ALS.train方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。