我正在按照文档示例构建一个推荐系统,我创建ALS构造函数一切正常
val als = new ALS() .setMaxIter(maxIterations) .setRegParam(regressionParam) .setUserCol("user_id") .setItemCol("item_id") .setRatingCol("rating")val model = als.fit(trainData)val predictions = model.transform(testData)println("predictions"+predictions.show(80))val evaluator = new RegressionEvaluator() .setMetricName("rmse") .setLabelCol("rating") .setPredictionCol("prediction")val rmse = evaluator.evaluate(predictions)println(s"Root-mean-square error = $rmse")predictions
我训练了模型,设置了一切,但我的主要问题是,我可以用测试数据进行预测,这会为我传入的每个项目ID返回预测因子,但我实际上需要的是模型能够根据用户ID向我推荐项目,而不是通过添加预测列来预测某个项目ID的适合程度。org.apache.spark.ml.recommendation.ALS能够做到这一点吗?
回答:
目前,要为用户ID为1的用户推荐10个顶级项目,您必须做类似以下的事情:
val predictionsForUser1 = predictions .where(col("user_id") === 1) .orderBy(col("predictions").desc) .limit(10)
在未来的Spark版本中,这将以更用户友好的方式实现