Recommendation Systems using BigQuery ML
By: Ritesh Sawant
Tags: Machine Learning, Bigquery Ml, Google Cloud Platform, Recommendation System, Google Bigquery

Give me your best Recommendations!

In this article you will learn how to:

Process sample data into a format suitable for training a matrix factorization model

Create, train, and deploy a matrix factorization model.

Get predictions from the deployed model about what products your customers are most likely to be interested in.

Most consumers today expect personalization, but how do you create a recommendation system and use the predicted recommendations for increasing conversions? Click-through-rates? user engagement? User interaction? In-short making your user stay with your product for longer durations.

I have already developed a production-ready Recommendation system and deployed it on Google App Engine as an endpoint so it can be easily fetched on the front-end, but it took weeks of work and testing to reach the beta version. Also, processing it on a huge-dataset would mean I need enough computing power to handle the computations. I also have to worry about offline and online systems and storage for my data. There is a simple solution for that.

What if you could make recommendations directly from your Google Cloud Console?

Welcome Google BigQuery ML.

A fully managed Data Warehouse Solution on Google Cloud. It comes with outstanding features for Analytics and Storage and a special one, BigQuery ML. BigQuery ML lets you deploy Machine Model’s directly using SQL. It means I do not have to worry about computation, storage, or fetching the data on my local machine.

With Recommendation Systems, there are 2 types of features you should look for,

Explicit features — Features that can be interpreted from the user input.

Implicit features — Features created by inferring interests indirectly from user activity.

Goal: Recommend Articles to users spending more time reading similar articles on the platform. (This is specific to my use-case but can be extended to many such use-cases)

Techniques used:

Collaborative Recommender system

Matrix Factorization

Evaluation Metrics

Since I am using a Collaborative approach, how do I know what articles my users like the most?

I do not have the data about ratings for each article. I have to look for ‘implicit features’. Let’s look at the time spent on that page by a user, which gives me intuition about the reading habits of the users. I can look at the unique users and their page views and session duration to create a feature set for training an ML model. We need these 3 features to train a Recommendation system on BigQuery ML.

Features:

Unique user id

Page_id

Session_duration

To prevent the Integrity of my Company, I would use a small subset of the dataset for showing the process. You can query public data from the public Datasets on Google BigQuery and try it for yourself. Create a Training Dataset by running the query given on Google’s Solution’s Page.

Let’s start by taking a look at the training_data table that I created on BigQuery, the data contains an anonymized user_id,item_id and how long they were on the page.

Once the data is ready, to train the recommendation system let’s write a SQL query to create a Matrix Factorization model on BigQuery.

CREATE OR REPLACE MODEL `tyour_dataset.article_recommender`

OPTIONS(model_type=’matrix_factorization’,

user_col=’user_Id’,

item_col=’item_id’,

rating_col=’session_duration’,

feedback_type=’implicit’)

AS

SELECT DISTINCT(user_id), cats as item_id, SUM(session_duration) as session_duration

FROM `tegger-prod.analytics_228863360.training_data`

GROUP BY 1,2

Remember to specify the options, user_col,the item_col for the inputting the user item data to the model. The feedback_type for the query tells the model what technique to use for calculating recommendations. ‘Implicit’ feedback uses a Weighted Alternating Least Squares Approach. It trains differently for both Implicit and explicit feedback. Read more here.

As you are executing this query, you might get different pricing errors such as ‘not available for on-demand pricing, please change to a flat-rate pricing structure’ Google is just trying to save you from a costly bill. Reserve slots for your BigQuery processing and allot these to your project. I suggest you read more here.

After training is done you can either go to your model to see the evaluation metrics or run this query to see the evaluation results.

SELECT *

FROM ML.EVALUATE(MODEL `your_dataset..article_recommender`)

What Data did the model evaluate on?

When you create a model using CREATE MODEL, BigQuery ML automatically splits the data into two sets, training and test behind the scenes which allows us to evaluate the model.

The most common evaluation technique for Recommender Systems, the Average Rank gives us a good look at the behavior on the test set. The lower the average rank the more closely the predicted recommendations match the behavior in the test set. It ranges from 0 to 1, with 0.5 being random chance and 0 being the perfect recommendation. Here is a great explanation about evaluation metrics.

https://sdsawtelle.github.io/blog/output/mean-average-precision-MAP-for-recommender-systems.html

If you are fine with your results, you can start predicting. To make predictions with BigQuery ML on a single user you can run the following query along with your user id.

What about predictions?

DECLARE MY_USERID STRING DEFAULT “be8c7b47-b5fd-42b1-abdb-fe2bfcf3b574”;

SELECT

*

FROM ML.RECOMMEND(MODEL `your_dataset.article_recommender`,

(SELECT MY_USERID as user_id))

How about the Top N recommendations?

Just add an order by on the predicted confidence descending and Limit the results by 5 for the Top N recommendations for this user.

DECLARE MY_USERID STRING DEFAULT “be8c7b47-b5fd-42b1-abdb-fe2bfcf3b574”;

SELECT

*

FROM

ML.RECOMMEND(MODEL `your_dataset.article_recommender`,

(SELECT MY_USERID as user_id))

ORDER BY predicted_session_duration_confidence DESC

LIMIT 5;

Voila! You are all set. You can modify the code to get the Top-N Recommendations for all your users and then the sky is limit for using the results in production. I am using these Recommendations to increase user-engagement on a particular website by recommending users articles that they might be interested in reading and it also helps me in creating targeted email campaigns for sending out promotional offers.

There are different techniques and approaches to handle the cold-start problem and you will have to settle with one. We have settled with showing new users popular items until we gather data about them.

Thank you for reading the article and please leave some feedback and a thumbs up. Thanks to Poling Lin for a great tutorial on YouTube for deploying a recommendation system on GCP. Also, if you want to learn more about BigQuery and you want to maximize the performance of all data operations from data loading to data analytics drop a comment below.