【发布时间】:2016-09-06 23:11:07
【问题描述】:
我正在使用 Spark MLib ALS 并尝试使用 trainImplicit() 接口将用户购买的商品数量作为隐式偏好提供给它。我不知道如何验证我的模型。我的输入在域 [1, inf) 中,但输出预测似乎在 (0, 1) 中浮动。
常用的代码:
from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating
from pyspark.sql import HiveContext
from pyspark import SparkContext
sc = SparkContext(appName="Quantity Prediction Model")
hive = HiveContext(sc)
d = hive.sql("select o.user_id as user, l.product_id as product, sum(l.quantity) as qty from order_line l join order_order o ON l.order_id = o.id group by o.user_id, l.product_id")
d.write.save('user_product_qty')
ratings = d.rdd.map(tuple)
testdata = ratings.map(lambda t: (t[0], t[1]))
for rank in (4, 8, 12):
model = ALS.trainImplicit(ratings, rank, 10, alpha=0.01)
predictions = model.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2]))
ratesAndPreds = ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions)
# Error is pretty bad because output raitings aren't in the same domain as quantity
ratesAndPreds = ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions)
MSE = ratesAndPreds.map(lambda r: (r[1][0] - r[1][1])**2).mean()
print("Rank: {} MSE: {}".format(rank, MSE))
额外功劳:当使用train() 时,输入/输出域是什么? “收视率”是否会达到五分制?这在任何地方都没有记录。
【问题讨论】:
标签: pyspark apache-spark-mllib