Skip to content

Instantly share code, notes, and snippets.

@pacarvalho
Created February 19, 2021 16:45
Show Gist options
  • Save pacarvalho/4ca8192f94a103de253e5005eed72822 to your computer and use it in GitHub Desktop.
Save pacarvalho/4ca8192f94a103de253e5005eed72822 to your computer and use it in GitHub Desktop.
Collaborative Filtering Annex to Make Predictions for an Unseen User
(...)
print('Making predictions for an unseen user...')
items_to_rate = [trainset.to_raw_iid(i) for i in trainset.all_items()]
# We will consider this user to be ID 0 since that is not a valid ID for our real users
user_id = 0
# Make predictions
predictions = []
for iid in items_to_rate:
prediction = algo.predict(user_id, iid)
predictions.append([int(prediction.iid), prediction.est])
# Sort the prediction from best match to worst match
predictions.sort(key=lambda x: x[1], reverse=True)
# Save the results to S3
bucket.put_object(Body=str(predictions), Key='predictions/' + str(user_id))
(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment