Build out an ALS model
Let's specify your first ALS model. Complete the code below to build your first ALS model.
Recall that you can use the .columns method on the ratings data frame to see what the names of the columns are that contain user, movie, and ratings data. Spark needs to know the names of these columns in order to perform ALS correctly.
Cet exercice fait partie du cours
Building Recommendation Engines with PySpark
Instructions
- Before building our ALS model, we need to split the data into training data and test data. Use the
randomSplit()method to split theratingsdataframe intotraining_dataandtest_datausing an 0.8/0.2 split respectively and aseedfor the random number generator of42. - Tell Spark which columns contain the
userCol,itemColandratingCol. Use the.columnsmethod if needed. Complete the hyperparameters. Set therankto 10, themaxIterto 15, theregParamor lambda to .1, thecoldStartStrategyto"drop", thenonnegativeargument should be set toTrue, and since our data contains explicit ratings, set theimplicitPrefsargument toFalse. - Now fit the
alsmodel to thetraining_dataportion of theratingsdata by calling theals.fit()method on thetraining_dataprovided. Call the fitted modelmodel. - Generate predictions on the
test_dataportion of theratingsdata by calling themodel.transform()method on thetest_dataprovided. Call the predictionstest_predictions. Feel free to view the predictions by calling the.show()method on thetest_predictions
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Split the ratings dataframe into training and test data
(training_data, test_data) = ratings.____([____, ____], seed=42)
# Set the ALS hyperparameters
from pyspark.ml.recommendation import ALS
als = ALS(userCol="____", itemCol="____", ratingCol="____", rank =____, maxIter =____, regParam =____,
coldStartStrategy="____", nonnegative =____, implicitPrefs = ____)
# Fit the mdoel to the training_data
____ = ____.fit(____)
# Generate predictions on the test_data
____ = ____.transform(____)
test_predictions.show()