Building a recommender system is easy with GraphLab Create. Simply import graphlab, load data, create a recommender model, and start making recommendations. Let's walk through this line by line.
In [1]:
import graphlab
The data is sitting on an AWS S3 bucket as a csv file. We can load it into a GraphLab SFrame with read_csv()
, specifying the "rating" column to be loaded as integers. For other ways of creating an SFrame and doing data munging, see the SFrame tutorial.
In [2]:
data = graphlab.SFrame.read_csv("http://s3.amazonaws.com/dato-datasets/movie_ratings/training_data.csv", column_type_hints={"rating":int})
data.head()
Out[2]:
We have the data. It's time to build a model. Now, have you ever tried typing "item similarity, matrix factorization, factorization machine, popularity" ten times in a row? We have, and we don't recommend it. (Harhar.) There are many good models for making recommendations, but sometimes even knowing the right names can be a challenge, much less typing them time after time.
This is why GraphLab Create provides a default recommender called ... recommender
. You can build a default recommender with recommender.create()
. It requires a dataset to use for training the model, as well as the names of the columns that contain the user IDs, the item IDs, and the ratings (if present).
In [3]:
# Build a default recommender (a Matrix Factorization model)
# The data needs to contain at least three columns: user, item, and rating.
# All other columns in the dataset are ignored by the default recommender.
model = graphlab.recommender.create(data, user_id="user", item_id="movie", target="rating")
Details (and the small devil therein):
Under the hood, the type of recommender is chosen based on the provided data and whether the desired task is ranking (default) or rating prediction. The default recommender for this type of data and the default ranking task is a matrix factorization model, implemented on top of the disk-backed SFrame data structure. The default solver is stochastic gradient descent, and the recommender model used is the RankingFactorizationModel , which balances rating prediction with a ranking objective. The default create()
function does not allow changes to the default parameters of a specific model, but it is just as easy to build a specific recommender with your own parameters using the appropriate model-specific create()
function.
The trained model can now make recommendations of new items for users. To do so, call recommend()
with an SArray of user ids. If users
is set to None, then recommend()
will make recommendations for all the users seen during training, automatically excluding the items that are observed for each user. In other words, if data
contains a row "Alice, The Great Gatsby", then recommend()
will not recommend "The Great Gatsby" for user "Alice". It will return at most k
new items for each user, sorted by their rank. It will return fewer than k
items if there are not enough items that the user has not already rated or seen.
The score
column of the output contains the unnormalized prediction scores for each user-item pair. The semantic meanings of these scores may differ between models. For the linear regression model, for instance, a higher average score for a user means that the model thinks that this user is generally more enthusiastic than others. See the Basic Recommender Functionalities tutorial for more details.
In [4]:
# You can now make recommendations for all the users you've just trained on
results = model.recommend(users=None, k=5)
results.head()
Out[4]:
The model can be saved for later use, either on the local machine or in an AWS S3 bucket. The saved model sits in its own directory, and can be loaded back in later to make more predictions.
In [5]:
# Save the model for later use
model.save("my_model")
Et voilà! You've just built your first recommender with GraphLab Create. Congratulations!
(Looking for more details about the modules and functions? Check out the API docs.)