模型訓練與預測
在將資料分成訓練與測試資料後,這題的第二部分要用訓練資料來訓練 ALS 演算法。PySpark MLlib 的 ALS 演算法有以下必填參數:rank(模型中的潛在因子數)與 iterations(執行的迭代次數)。訓練完成後,你可以用模型來對測試資料中的評分進行預測。為此,你需要從測試資料集中提供使用者與項目兩個欄位,最後回傳 predictAll() 輸出的 2 列清單。
請記得,你的工作空間中已經有 SparkContext sc、training_data 與 test_data 可以使用。
本練習屬於課程
使用 PySpark 的 Big Data 基礎
練習說明
- 以訓練資料與設定參數(
rank= 10、iterations= 10)訓練 ALS 演算法。 - 在測試資料中移除第 3 欄的
rating欄位。 - 使用測試資料來預測評分,驗證模型。
- 回傳包含兩列預測評分的清單。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the ALS model on the training data
model = ALS.____(____, rank=10, iterations=10)
# Drop the ratings column
testdata_no_rating = test_data.___(lambda p: (p[0], ____))
# Predict the model
predictions = model.____(testdata_no_rating)
# Return the first 2 rows of the RDD
predictions.____(2)