当前位置: 首页>>代码示例>>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;未经允许,请勿转载。