Pyspark-collaborative Filtration

Source: Internet
Author: User
Tags pyspark

Reference Address:

1, http://spark.apache.org/docs/latest/ml-guide.html

2, https://github.com/apache/spark/tree/v2.2.0

3, http://spark.apache.org/docs/latest/ml-collaborative-filtering.html



From pyspark.ml.evaluation import Regressionevaluator to pyspark.ml.recommendation import ALS from pyspark.sql import R ow lines = Spark.read.text ("Data/mllib/als/sample_movielens_ratings.txt"). Rdd parts = Lines.map (lambda row:
                                     Row.value.split ("::")) Ratingsrdd = Parts.map (Lambda p:row (Userid=int (p[0)), Movieid=int (P[1]), Rating=float (p[2]), Timestamp=long (P[3]))) ratings = Spark.createdataframe (Ratingsrdd) (training, test) = Rati Ngs.randomsplit ([0.8, 0.2]) # Build the recommendation model using ALS on the training data # Note We set Cold start stra Tegy to ' drop ' to ensure we don ' t get NaN evaluation Metrics als = ALS (maxiter=5, regparam=0.01, usercol= "UserId", Itemcol = "MovieID", ratingcol= "rating", coldstartstrategy= "drop") model = Als.fit (Training) # Evaluate the model by COM Puting the RMSE on the test data predictions = model.transform (test) evaluator = Regressionevaluator (metricname= "RMSE", LA
                     Belcol= "rating",           Predictioncol= "Prediction") Rmse = evaluator.evaluate (predictions) print ("Root-mean-square error =" + str (RMSE  ) # Generate Top movie recommendations for each user Userrecs = Model.recommendforallusers (Ten) # Generate top User Recommendations for each movie movierecs = Model.recommendforallitems (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.