构建隐式模型
既然所有超参数都已指定,让 Spark 构建足够多的模型以测试每一种组合。为此,这里提供了一个 for 循环。请按照下方说明自动创建这些 ALS 模型。接下来的练习中,您将把这些模型运行在测试数据集上,查看哪一个表现最佳。
ALS 算法已为您导入。您在上一个练习中创建的列表(ranks、maxIters、regParams、alphas)也已为您准备好。
本练习是课程的一部分
使用 PySpark 构建推荐引擎
练习说明
- 这里提供了一个名为
model_list的空列表。for循环会为给定的每组超参数创建一个模型,并将其放入model_list中。 - 通过引用各列表的内容来完成
for循环,其中r表示ranks列表中的元素,mi表示maxIters列表中的元素,rp表示regParams列表中的元素,a表示alphas列表中的元素。 - 打印
len(model_list)以及model_list,以确认每个模型都已创建。长度应等于上面每个超参数列表长度的乘积。您也可以运行提供的额外校验以确保无误。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# For loop will automatically create and store ALS models
for r in ____:
for mi in ____:
for rp in ____:
for a in ____:
model_list.append(ALS(userCol= "userId", itemCol= "songId", ratingCol= "num_plays", rank = r, maxIter = mi, regParam = rp, alpha = a, coldStartStrategy="drop", nonnegative = True, implicitPrefs = True))
# Print the model list, and the length of model_list
print (model_list, "Length of model_list: ", len(____))
# Validate
len(model_list) == (len(ranks)*len(maxIters)*len(regParams)*len(alphas))