[MUSIC PLAYING]
POLONG LIN: In this video, I'll show you
how to build and deploy a recommendation
system with BigQuery ML.
My name is Polong Lin and I'm a developer advocate
for Google Cloud.
From finding the right Halloween costume,
getting notified of relevant spooky
decorations for your home, the majority of consumers
today expect personalization, recommendations
of products and services, or content
tailored to their interests.
And so, in order to better serve the customers,
businesses need to be able to train and deploy recommendation
systems quickly and effectively to improve conversions,
click-through rates, and build customer satisfaction, loyalty,
and brand affinity.
But to build recommendation systems,
you need a place for your data.
Google BigQuery is a fully managed, highly scalable data
warehouse solution on Google Cloud.
It comes with lots of great features,
especially around storage and analytics.
Without needing to move your data out of BigQuery,
with BigQuery ML, you can train and deploy machine
learning models directly using SQL.
That means you've got data storage, analytics, and machine
learning all within BigQuery.
So how can businesses create recommendation systems quickly
and deploy them into their online stores?
In this video, I'll show you how to prepare your training
data in BigQuery, train a recommendation
system with BigQuery ML, use the predicted recommendations
in production, and tips and best practices.
Let's start with the data.
To recommend something to someone,
you kind of need to know what they like already.
So how do you know what a user likes?
In general, there are two ways.
If your users directly give you ratings of 4 and 1/2
stars out of 5 on a particular product or piece of content,
then this is called explicit feedback.
The second way is to infer interest indirectly.
For example, maybe the more time a user
spends viewing a product, the more they're interested in it.
Since you're inferring interest indirectly or implicitly,
this is called implicit feedback.
As implicit feedback tends to be more
common in retail businesses, let's
build a recommendation system using some sample
implicit feedback data from a real online store.
The Google Merchandise Store is an actual e-commerce store
where people can shop for Google-branded shirts, hats,
and other products.
And a sample of the anonymized Google Analytics data
is available as one of the BigQuery public data sets.
Based on the store's Google Analytics data,
I've cleaned the data so that we can see each anonymized visitor
ID, what product they were looking at,
and how long they spent viewing the product, which
is in milliseconds here.
We need these three columns to train a recommendation system
in BigQuery ML--
the user, the product, and the ratings.
So the visitor in the first row, for example,
viewed a particular item for 59 milliseconds.
And in the second row, a different visitor
looked at this item for 5,787 milliseconds, and so on.
So essentially, the more time they spend viewing the product,
the higher the implicit rating.
With the data ready, to trade the recommendation system,
we'll be using the matrix factorization algorithm
in BigQuery ML.
So selecting the data is straightforward here.
Select star from the view or table.
With the training data, we can now create the model
by specifying a model name like retail recommender
in our data set BQML.
Then we specify the options.
Model type, which in this case is matrix factorization
for recommendation systems, the user column, the item column,
and the rating column.
I'm setting feedback type to implicit
because the model is trained differently
for explicit and implicit feedback.
And then within AS, we select the training data.
Now as you execute this query, if you
get an error that says training matrix factorization models is
not available for on-demand usage,
it's because matrix factorization can
be quite computationally expensive,
so BigQuery it's trying to protect you
from a potentially hefty bill.
To rectify this, you'll need to use flat-rate pricing.
And to learn more about this, follow the guide in the link
here.
After training is done, you can evaluate your model
using select star from ML dot evaluate.
But you might be wondering what data
it's evaluating the model on.
When you create a model, BigQuery ML automatically
splits the training data into its own training and testing
data behind the scenes for you.
So this allows you to evaluate your model immediately
after the model has finished training.
Now of these evaluation metrics, average rank,
also known as mean percentile rank,
is perhaps the most widely used metric for implicit matrix
factorization.
In general, the lower the average rank,
the more closely the predicted recommendations
match the behavior in the test set,
with 0.5 being random chance and 0 being a perfect prediction.
If you're happy with the evaluation metrics,
you can now make predictions.
To make predictions with BigQuery ML on a single user
or a set of users to start, you can run select star
from ML dot recommend, specifying
the model you created, and the user ID or IDs
that you want to make predictions for.
To get the top N recommendations,
you can order by the predicted confidence descending
and limit N. You can also make predictions on all the users
so you have the top N recommendations
for every single user.
For example, here we're looking at the top five recommendations
for all the users.
Note that because the output for predicting the top N
recommendations for every single user
can actually get quite large.
And because we may want to use the results multiple times,
it may be better to store the outputs in a separate table
that you can reference more easily later, like here, prod
recommendations under the BQML data set.
Now that we have our predicted recommendations,
what can we do to use the recommendations in production?
One way is to export recommendations
for add retargeting campaigns with Google Analytics.
First, by focusing on specific products at a time,
you can create a new column for likelihood
to purchase this product based on
the predicted recommendations.
Then you can import your predictions back
into Google Analytics to create new campaigns
for those products.
Another way is by connecting the predicted recommendations
with your CRM, like Salesforce.
And by doing so, you can create targeted email campaigns
to provide relevant products directly
to your customers' inboxes.
There are even integrations between Google Analytics
and Salesforce that you can leverage.
You can also export the recommendations
to Cloud Storage, a separate database,
or simply read from BigQuery to use in a separate service
to serve the recommendations.
Now, at this point, I want to cover some tips and best
practices, as you might be thinking
how you can potentially improve your recommendation
systems in production.
One question you might have is, what happens to new users?
In other words, since matrix factorization can only
make predictions on users who belong to the training data,
how do you make predictions on users who
you don't have any data for?
Well, this is actually a common problem
called the cold-start problem.
There are many ways to deal with this,
which is outside the scope of this video.
But as a simple suggestion the easiest way
would be to show users items that are sorted by popularity
or what products have been trending
for the past timed interval.
Another question you may ask is, how do you recommend items
to new users if you know some of their demographic data?
One way is to find lookalikes to those new users
based on your pool of existing users.
For example, given some demographic data for User X,
first compare User X to all of the existing users
that you already have recommendations for.
Then find the five most similar users,
we'll call these lookalikes, to User X based on whatever
data you may have--
demographic data or otherwise.
Then make recommendations to User X
based on what these lookalikes would themselves
be recommended.
To find lookalikes, one way is to use
cosine similarity between User X and the other users.
Another way that you can find lookalikes
is to find the nearest cluster of users
and use them as your lookalikes.
And you can do this using a clustering algorithm
like k-means, which is also available with BigQuery ML.
For example, given four clusters of users,
we may find that User X is closest
in distance to the centroid of these yellow squared data
points.
And so we can recommend to User X products
that a user in this cluster would themselves
be recommended.
Another question you may have is that BigQuery ML does
batch predictions very well, but your business
may need predictions on a single user at a time,
and you need the predictions to be fast.
In these situations, you can export
your matrix factorization models from BigQuery ML
and host it for real-time predictions using
AI Platform on Google Cloud.
To learn more about how to host these models
for real-time predictions, you can follow this link.
So now you should know how to build and deploy
a recommendation system with BigQuery ML.
Thanks for watching.
[MUSIC PLAYING]