zor*_*ork 16 scala machine-learning collaborative-filtering apache-spark
我试图使用Spark MLib ALS与隐式反馈进行协同过滤.输入数据只有两个字段userId和productId.我没有产品评级,只是关于用户购买的产品的信息,这就是全部.所以为了训练ALS,我使用:
def trainImplicit(ratings: RDD[Rating], rank: Int, iterations: Int): MatrixFactorizationModel
Run Code Online (Sandbox Code Playgroud)
(http://spark.apache.org/docs/1.0.0/api/scala/index.html#org.apache.spark.mllib.recommendation.ALS $)
此API需要Rating对象:
Rating(user: Int, product: Int, rating: Double)
Run Code Online (Sandbox Code Playgroud)
另一方面,文档trainImplicit说明:给出用户对某些产品给出的"隐含偏好"评级的RDD,以(用户ID,产品ID,偏好)对的形式训练矩阵分解模型.
当我将评级/偏好设置1为:
val ratings = sc.textFile(new File(dir, file).toString).map { line =>
val fields = line.split(",")
// format: (randomNumber, Rating(userId, productId, rating))
(rnd.nextInt(100), Rating(fields(0).toInt, fields(1).toInt, 1.0))
}
val training = ratings.filter(x => x._1 < 60)
.values
.repartition(numPartitions)
.cache()
val validation = ratings.filter(x => x._1 >= 60 && x._1 < 80)
.values
.repartition(numPartitions)
.cache()
val test = ratings.filter(x => x._1 >= 80).values.cache()
Run Code Online (Sandbox Code Playgroud)
然后训练ALSL:
val model = ALS.trainImplicit(ratings, rank, numIter)
Run Code Online (Sandbox Code Playgroud)
我得到RMSE 0.9,如果首选项取0或1值,这是一个很大的错误:
val validationRmse = computeRmse(model, validation, numValidation)
/** Compute RMSE (Root Mean Squared Error). */
def computeRmse(model: MatrixFactorizationModel, data: RDD[Rating], n: Long): Double = {
val predictions: RDD[Rating] = model.predict(data.map(x => (x.user, x.product)))
val predictionsAndRatings = predictions.map(x => ((x.user, x.product), x.rating))
.join(data.map(x => ((x.user, x.product), x.rating)))
.values
math.sqrt(predictionsAndRatings.map(x => (x._1 - x._2) * (x._1 - x._2)).reduce(_ + _) / n)
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:我应该设置什么价值rating:
Rating(user: Int, product: Int, rating: Double)
Run Code Online (Sandbox Code Playgroud)
用于隐式训练(在ALS.trainImplicit方法中)?
更新
附:
val alpha = 40
val lambda = 0.01
Run Code Online (Sandbox Code Playgroud)
我明白了:
Got 1895593 ratings from 17471 users on 462685 products.
Training: 1136079, validation: 380495, test: 379019
RMSE (validation) = 0.7537217888106758 for the model trained with rank = 8 and numIter = 10.
RMSE (validation) = 0.7489005441881798 for the model trained with rank = 8 and numIter = 20.
RMSE (validation) = 0.7387672873747732 for the model trained with rank = 12 and numIter = 10.
RMSE (validation) = 0.7310003522283959 for the model trained with rank = 12 and numIter = 20.
The best model was trained with rank = 12, and numIter = 20, and its RMSE on the test set is 0.7302343904091481.
baselineRmse: 0.0 testRmse: 0.7302343904091481
The best model improves the baseline by -Infinity%.
Run Code Online (Sandbox Code Playgroud)
我猜,这仍然是一个很大的错误.我也得到奇怪的基线改进,其中基线模型只是意味着(1).
您可以指定 alpha 置信水平。默认值为 1.0:但尝试更低。
val alpha = 0.01
val model = ALS.trainImplicit(ratings, rank, numIterations, alpha)
Run Code Online (Sandbox Code Playgroud)
让我们知道事情进展如何。
| 归档时间: |
|
| 查看次数: |
5076 次 |
| 最近记录: |