Pi *_* Pi 5 scala machine-learning apache-spark
我使用 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 data
val 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")
Run Code Online (Sandbox Code Playgroud)
但得到一些负分,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
Run Code Online (Sandbox Code Playgroud)
怎样才能得到好的结果呢?
负值并不重要,因为 RMSE 首先对这些值进行平方。可能您的预测值是空的。你可以删除它们:
predictions.na().drop(["prediction"])
Run Code Online (Sandbox Code Playgroud)
虽然,这可能有点误导,但您也可以用您的最低/最高/平均评级填充这些值。
我还建议四舍五入x < min_rating
至x > max_rating
最低/最高评级,这将提高您的 RMSE。
编辑:
这里有一些额外的信息:https ://issues.apache.org/jira/browse/SPARK-14489
从 Spark 版本 2.2.0 开始,您可以将coldStartStrategy
参数设置为drop
,以便删除预测 DataFrame 中包含 NaN 值的任何行。然后,评估指标将根据非 NaN 数据进行计算,并且是有效的。
model.setColdStartStrategy("drop");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4070 次 |
最近记录: |