我正在尝试从我的数据集中读取有三个颜色的数据集。(用户、储存库和星号)
在10中
lines = spark.read.text("Dataset.csv").rdd
print(lines.take(10))Out10
[Row(value='0,0,0,290'), Row(value='1,1,1,112'), Row(value='2,2,2,87.8'), Row(value='3,3,3,69.7'), Row(value='4,4,4,65.7'), Row(value='5,5,5,62'), Row(value='6,6,6,61.6'), Row(value='7,7,7,60.7'), Row(value='8,8,8,57.7'), Row(value='9,9,9,56.2')]在10中
# Need to convert p[1] from str to int
parts = lines.map(lambda row: row.value.split(","))
print(parts.take(2))Out11
[['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))Out12
[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)Out15
+---------+------+------+----------+
|repoCount|repoId|userId|prediction|
+---------+------+------+----------+
+---------+------+------+----------+我的模型给出了空值。我的数据集中是否有问题,或对培训的错误假设。
请注意,我的ratingCol在ALS是星号,这是一个显式的评级,而不是隐性评级。
发布于 2019-04-29 23:43:26
我需要了解数据集的某些方面,才能给出更好的答案,但是:
您接收空值的原因是:
根据类分布的不同,由于在原始数据的两个样本中没有类(“repoId”),您可能会收到空预测。因此,测试数据中存在的类可能不存在于培训数据中。因此,当您将转换应用于测试数据时,它没有根据对所提供的数据进行预测。当您使用"coldStartStrategy“时,它只是完全忽略了这些记录。
我建议首先将"ColdStartStrategy“设置为False,以查看您的所有记录是否只是返回Null预测值。
如果是这样,则需要检查数据的培训和测试样本中的类分布。这应该是"repoId“的类发行版。然后,您必须以确保两个样本中都存在类的方式对数据进行采样,然后重新应用。
在您的过程中潜在的缺陷:
https://datascience.stackexchange.com/questions/46996
复制相似问题