VOOZH about

URL: https://www.analyticsvidhya.com/blog/2022/06/movie-recommendation-with-sql-using-google-cloud-platform/

⇱ Movie Recommendation with SQL Using Google Cloud Platform


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Movie Recommendation with SQL Using Google Cloud Platform

Himanshu Last Updated : 12 Jul, 2022
5 min read

This article was published as a part of the Data Science Blogathon.

Introduction

Ever Wondered how Netflix gets to know your choice and shows the movie of your interest? Or have you ever think how Amazon shows your the recommended product based on your search item? So the magic behind these technologies is called theRecommendation Engine“. 

👁 Movie recommendation
Photo by Peter Herrmann on Unsplash

So in the article let’s build one using GCP (Google Cloud Platform).

What is BigQuery ML?

https://www.cloudskillsboost.google/

Google BigQuery is a serve-less, cost-effective, and highly scalable data warehouse system, BigQuery ML allows you to create and execute machine learning models in BigQuery using only standard SQL queries.

BigQuery ML allows data scientists, ML Engineers, and Data Engineers to quickly build and analyze Machine learning models directly using SQL

What kind of models BigQuery ML Supports?

  • Regression Models

  • Linear Regression, Binary Logistic Regression, Multiclass Logistic Regression Clustering

  • K-means clustering

  • Matrix Factorization for creating products like Recommendation Systems

  • Time Series model for forecasting

  • Boosted Tree model -> XGBoost (Classification and Regression)

  • Deep Neural Network (DNN) -> Classification and Regression

https://cloud.google.com/bigquery-ml/docs/introduction

What big query does Behind the scene?

  • Leverage BigQuery’s processing power to build a model

  • Auto-tunes learning rate

  • Auto-split data into training and test

  • L1/L2 regularization

  • Data Splitting training/test split: Random, Sequential, Custom

  • Set Learning Rate

Setup GCP BigQuery ML

Go to your GCP account, Navigation to -> BigQuery, accept the terms and conditions and click Done.

Load Dataset

We have 2 options to create and load our dataset

  1. We can use UI to create and load our dataset

  2. We can use simple BigQuery Commands in order to create and load our dataset

I’ll be showing you the 2nd way only because I prefer the second one.

  1. Run this command to create a BigQuery dataset named movies:

bq - - location=EU mk - - dataset movies

What we are doing here is that we’re choosing a location and creating a dataset

  1. Run the following command to load the dataset in the CSV format, We can upload our own dataset too for model creation but I’ll be going to use open datasets provided by BigQuery.

# for loading ratings

bq load --source_format=CSV 
 --location=EU 
 --autodetect movies.movielens_ratings 
 gs://dataeng-movielens/ratings.csv

# for loading movies

bq load --source_format=CSV 
 --location=EU 
 --autodetect movies.movielens_movies_raw 
 gs://dataeng-movielens/movies.csv

After running all the commands on the GCP console you should see movie data

Explore Dataset (EDA)

Let’s Have a quick the look at our dataset:

So we can see that we have a folder called “movies” which contains “movielens_movies” and  “movielens_ratings” ignore “movielens_movies_raw” for now

Let’s check what kind of data we have in our “movielens_movies” dataset, as you can see we have “movieId”, “title” and “genres” which we are going to use in our model building part.

Let’s see our “movielens_ratings” dataset

In which we have “userId”, “movieId” , “rating” and timestamp

Check total data size

On BigQuery Query Editor write this query and execute :

```
SELECT
 COUNT(DISTINCT userId) numUsers,
 COUNT(DISTINCT movieId) numMovies,
 COUNT(*) totalRatings
FROM
 movies.movielens_ratings
  • Examine the movies with ratings < 3:

```
SELECT
 *
FROM
 movies.movielens_movies_raw
WHERE
 movieId < 3
  • We Can also visualize our ratings using Google Data Studio, click on “Explore Data” and then “Visualize on Data Studio”

  • Examine the movies with ratings < 5:

```
SELECT
 *
FROM
 movies.movielens_movies_raw
WHERE
 movieId < 5

“`

As you can see the column name ‘genres’ have a formatted string, so let’s split on (|) and save it into the new table.

```
CREATE OR REPLACE TABLE
 movies.movielens_movies AS
SELECT
 * REPLACE(SPLIT(genres, "|") AS genres)
FROM
 movies.movielens_movies_raw
```

We can do other additional data argumentation to make our model good.

Collaborative Filtering Model Creation (Matrix Factorization)

To build our recommendation system in BigQueryML we need to pass model_type and we need to identify which columns are important for collaborative filtering.

Create Model:

```
CREATE OR REPLACE MODEL movies.movie_recommender 
OPTIONS (model_type='matrix_factorization',
 user_col='userId', item_col='movieId', rating_col='rating', l2_reg=0.2, num_factors=16) AS 
SELECT userId, movieId, rating FROM movies.movielens_ratings
```

To View our trained model. Run this query into the BigQuery editor

SELECT * FROM ML.EVALUATE(
MODEL `path_to_your_model.movies.movie_recommender`)

Making Predictions

  • Let’s find the best Romance movie for the userID 102.

Execute this query into the BigQuery editor

```
SELECT
 *
FROM
 ML.PREDICT(MODEL `path_to_your_model.movie_recommender`,
 (
 SELECT
 movieId,
 title,
 102 AS userId
 FROM
 `movies.movielens_movies`,
 UNNEST(genres) g
 WHERE
 g = "Romance" ))
ORDER BY
 predicted_rating DESC
LIMIT
 5 
```

– Let’s find the best Romance movie for the userID 402.

Execute this query into the BigQuery editor

```
SELECT
 *
FROM
 ML.PREDICT(MODEL `path_to_your_model.movie_recommender`,
 (
 SELECT
 movieId,
 title,
 402 AS userId
 FROM
 `movies.movielens_movies`,
 UNNEST(genres) g
 WHERE
 g = "Comedy" ))
ORDER BY
 predicted_rating DESC
LIMIT
 5 
```

Conclusion

In this article, we learned how to use BigQueryML to create ML Models using SQL. The key takeaways from the article are:

  • How to load the data into your GCP storge.
  • How to preprocess the data using SQL commands.
  • How to Visualization data using Google Data Studio.
  • How to use BigQueryML for creating models directly into Cloud and make a recommendation on it.

That’s it for now I hope you learned something from this article, see you in the next article.

If you want to know how to build and run a Machine Learning model with SQL check out my Machine Learning with SQL blog:- https://iamhimanshutripathi0.medium.com/machine-learning-with-sql-30e942c75240

I’ve provided the image links for those images which are not mine.

Let’s get connected on Linkedin, Twitter, Instagram, Github, and Facebook.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

I'm passionated about Machine learning, NLP HealthCare, AI, Audio processing, Maths, Cloud, JavaScript, UI/UX, and Android development and i love to play guitar and i also have your channel where is upload videos releted to my pojects and also releted to guitar songs..

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
👁 Av Logo White

Continue your learning for FREE

Forgot your password?
👁 Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

👁 Popup Banner
👁 AI Popup Banner