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)