我使用ALS来预测评分,这是我的代码:
val als = new ALS() .setMaxIter(5) .setRegParam(0.01) .setUserCol("user_id") .setItemCol("business_id") .setRatingCol("stars")val model = als.fit(training)// Evaluate the model by computing the RMSE on the test dataval predictions = model.transform(testing)predictions.sort("user_id").show(1000)val evaluator = new RegressionEvaluator() .setMetricName("rmse") .setLabelCol("stars") .setPredictionCol("prediction")val rmse = evaluator.evaluate(predictions)println(s"Root-mean-square error = $rmse")
但是得到了一些负分,并且RMSE是NaN:
+-------+-----------+---------+------------+|user_id|business_id| stars| prediction|+-------+-----------+---------+------------+| 0| 2175| 4.0| 4.0388923|| 0| 5753| 3.0| 2.6875196|| 0| 9199| 4.0| 4.1753435|| 0| 16416| 2.0| -2.710618|| 0| 6063| 3.0| NaN|| 0| 23076| 2.0| -0.8930751|Root-mean-square error = NaN
如何获得好的结果?
回答:
负值并不重要,因为RMSE首先对值进行平方。可能你的预测值中有空值。你可以删除它们:
predictions.na().drop(["prediction"])
虽然这样做可能有点误导,另一种方法是你可以用最低/最高/平均评分来填充这些值。
我还建议将x < min_rating
和x > max_rating
四舍五入到最低/最高评分,这样可以改善你的RMSE。
编辑:
这里有一些额外的信息:https://issues.apache.org/jira/browse/SPARK-14489