在[10]

# Need to convert p[1] from str to int
parts = lines.map(lambda row: row.value.split(","))


print(parts.take(2))

出[11]

    [['0', '0', '0', '290'], ['1', '1', '1', '112']]

在[12]

# RDD mapped as int and float from Dataset

ratingsRDD = parts.map(lambda p: Row(userId=int(p[1]),repoId=int(p[2]),repoCount=float(p[3])))
ratings = spark.createDataFrame(ratingsRDD)
print(ratings.head(10))

出[12]

    [Row(repoCount=290.0, repoId=0, userId=0), Row(repoCount=112.0, repoId=1, userId=1), Row(repoCount=87.8, repoId=2, userId=2), Row(repoCount=69.7, repoId=3, userId=3), Row(repoCount=65.7, repoId=4, userId=4), Row(repoCount=62.0, repoId=5, userId=5), Row(repoCount=61.6, repoId=6, userId=6), Row(repoCount=60.7, repoId=7, userId=7), Row(repoCount=57.7, repoId=8, userId=8), Row(repoCount=56.2, repoId=9, userId=9)]

在[13]

(training, test) = ratings.randomSplit([0.8, 0.2])

在[14]:

# Build the recommendation model using ALS on the training data
# Cold start strategy is set to '"drop" to make sure there is no NaN evaluation metrics which would result in error.
als = ALS(maxIter=5, regParam=0.01, userCol="userId", itemCol="repoId", ratingCol="repoCount"
        ,coldStartStrategy="drop") #Cold-start is set to DROP
model = als.fit(training)

在[15]

#Evaluate the model by computing the RMSE on the test data
predictions = model.transform(test)
type(predictions)

predictions.show(3)

出[15]

    +---------+------+------+----------+
    |repoCount|repoId|userId|prediction|
    +---------+------+------+----------+
    +---------+------+------+----------+

我的模型给出了 NULL 值。我的数据集中是否存在问题,或者对训练的假设是否错误。

请注意,我ratingCol的 inALS是星数,这是一个明确的评级,而不是隐含的评级。

1个回答

我需要了解您的数据集的某些方面才能给您更好的答案,但是:

您收到 NULL 值的原因:

根据类分布,由于原始数据的两个样本中都没有类(“repoId”),您可能会收到空预测。因此,存在于测试数据中的类可能不存在于训练数据中。因此,当您将转换应用于测试数据时,它没有依据来对提供的数据进行预测。当您使用“coldStartStrategy”时,它只是完全省略了这些记录。

我建议首先将“ColdStartStrategy”设置为 False,以查看您的所有记录是否仅返回 Null 预测值。

如果是这种情况,您将需要检查数据的训练和测试样本中的类分布。这应该是“repoId”的类分布。然后,您必须以确保两个样本中都存在类的方式对数据进行采样,然后重新应用。

您的流程中的潜在缺陷:

  1. 在“randomSplit”中使用种子,这样您的样本总是可重现的,因此无论何时运行程序,您都可以检测到问题
  2. 您可能假设数据集中的类分布相等
  3. 规范化您的“Repocount”值将使算法受益,但可能与整体结果无关
其它你可能感兴趣的问题
上一篇多变量多时间序列模型的架构,其中一些特征是 TS 特定的,一些特征是全局的 下一篇Keras:在笔记本中切换后端