Last active
August 14, 2024 12:12
-
-
Save jeanmidevacc/1faf525a7c145f1b5feb20014f1ff854 to your computer and use it in GitHub Desktop.
mf_pyspark_als.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark.ml.recommendations import ALS | |
# Context , there is dfs_actions is spark dataframe that looks like the pandas dataframe for surprise example https://gist.github.com/jeanmidevacc/a00c9cf8fe9379cd8a818b1d842dbaa1 | |
# Setup the model parameters | |
als = ALS( | |
seed=12, | |
userCol="user_id", | |
itemCol="item_id", | |
ratingCol="rating", | |
implicitPrefs=False,# to run the model for implicit rating and leverage | |
nonnegative=False, | |
maxIter=10, | |
rank=10) | |
model = als.fit(dfs_actions)#Build the factors matrices | |
dfs_predictions = model.transform(dfs_actions)#Compute the prediction between the pair user_id and item_id | |
user_factors, item_factors = model.userFactors, model.itemFactors #To extract the factors from the model (careful they are only defined by id and not your column atribute in this user_id, item_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment