在Apache Spark中使用ALS的结果不一致

Ric*_*sel 5 python bigdata apache-spark pyspark

我对Apache Spark和一般的大数据都很陌生.我正在使用ALS方法根据用户,项目和评级矩阵创建评级预测.令人困惑的部分是,当我运行脚本来计算预测时,结果每次都不同,没有输入或请求的预测发生变化.这是预期的行为,还是结果应该相同?以下是Python代码供参考.

from pyspark import SparkContext
from pyspark.mllib.recommendation import ALS

sc = SparkContext("local", "CF")

# get ratings from text
def parseRating(line):
  fields = line.split(',')
  return (int(fields[0]), int(fields[1]), float(fields[2]))

# define input and output files
ratingsFile = 's3n://weburito/data/weburito_ratings.dat'
unratedFile = 's3n://weburito/data/weburito_unrated.dat'
predictionsFile = '/root/weburito/data/weburito_predictions.dat'

# read training set
training = sc.textFile(ratingsFile).map(parseRating).cache()

# get unknown ratings set
predictions = sc.textFile(unratedFile).map(parseRating)

# define model
model = ALS.train(training, rank = 5, iterations = 20)

# generate predictions
predictions = model.predictAll(predictions.map(lambda x: (x[0], x[1]))).collect()
Run Code Online (Sandbox Code Playgroud)

小智 3

这是预期的行为。ALS 中的因子矩阵是随机初始化的(实际上其中一个是随机初始化的,另一个是基于第一步中的初始化求解的)。

因此不同的运行会产生略有不同的结果。