Building Powerful Machine Learning Models Directly in BigQuery
Machine learning has become an indispensable tool for gleaning insights from data and making accurate predictions. However, the process of building and deploying ML models can be complex, often requiring specialized skills and tools. Enter BigQuery – Google Cloud‘s fully managed, petabyte-scale data warehouse. BigQuery not only provides lightning-fast SQL querying over massive datasets, but also offers built-in machine learning capabilities that allow you to create and execute ML models directly where your data lives.
In this in-depth guide, we‘ll walk through the process of building a machine learning model step-by-step in BigQuery. Whether you‘re a data analyst, software engineer, or business user, you‘ll learn how BigQuery makes machine learning more accessible with intuitive SQL commands. Let‘s dive in.
The Power of BigQuery for Machine Learning
Before we jump into the tutorial, let‘s discuss what makes BigQuery an excellent choice for machine learning workloads:
Unmatched Scalability: BigQuery is built to handle petabyte-scale data, allowing you to train ML models on datasets of massive size. There‘s no need to down-sample your data or work with a subset. BigQuery can handle it all.
Cost-Effective: With BigQuery, you only pay for the data you store and query. There are no upfront costs for infrastructure. Even as you scale to building complex models on terabytes of data, BigQuery remains affordable.
SQL Interface: BigQuery provides a familiar SQL interface for building machine learning models. Data analysts who are comfortable with SQL can now create ML models without needing to learn new programming languages or frameworks.
Integration: BigQuery integrates seamlessly with other Google Cloud services like Cloud Storage, Cloud AI Platform, and Data Studio. You can easily leverage BigQuery ML models as part of an end-to-end AI/ML workflow.
Built-in Models: In addition to custom models, BigQuery provides pre-trained models and APIs for common ML tasks such as sentiment analysis, entity recognition in text, and image classification. These allow you to add ML capabilities to your applications without starting from scratch.
Now that we‘ve covered why BigQuery is a powerful tool for machine learning, let‘s step through the process of building a model.
Step 1: ETL Data into BigQuery
Machine learning models are only as good as the data they‘re trained on. The first step is to extract data from source systems, transform it into an ML-friendly format, and load it into BigQuery. BigQuery supports loading data from various sources including:
- CSV or JSON files in Cloud Storage
- Avro or Parquet files
- Spreadsheets and on-premises databases
- Pub/Sub streams for real-time data
- Other Google services like Analytics, Ads, YouTube
For this tutorial, let‘s imagine we have sales data in a Cloud Storage bucket that we want to use to build a model predicting future sales. The data is in CSV format with columns for date, store, product category, and sales amount.
To load this data into a BigQuery table, we can use the bq command line:
bq load --source_format=CSV --autodetect sales.transactions gs://my-bucket/sales.csv
This tells BigQuery to load the CSV data from Cloud Storage, auto-detect the schema, and create a table called transactions in the sales dataset.
Step 2: Select Features and Preprocess Data
With our training data loaded into BigQuery, the next step is to select the features we want to use in our ML model and do any necessary preprocessing. Feature engineering is a key part of building accurate models.
In our sales prediction example, we may want to use historical sales data to forecast future sales. We can create a features table with columns for:
- Store ID
- Product category
- Date
- Day of week
- Month
- Sales amount
To create this table with SQL:
CREATE TABLE sales.features AS
SELECT
store_id,
category,
date,
EXTRACT(DAYOFWEEK FROM date) AS dayofweek,
EXTRACT(MONTH FROM date) AS month,
sales_amount
FROM sales.transactions;
We‘re using BigQuery‘s date functions to extract the day of week and month from the date column. These may provide additional signals to our model.
We may also want to normalize the sales amount, a common preprocessing step:
SELECT
store_id,
category,
date,
dayofweek,
month,
CAST(sales_amount AS FLOAT64) / AVG(sales_amount) OVER() AS normalized_sales
FROM sales.features;
By dividing each sales amount by the overall average, we rescale the values to be centered around 1. This normalization helps many ML algorithms converge faster.
Step 3: Create ML Model
Now we‘re ready for the exciting part – actually building our machine learning model in BigQuery. Let‘s create a linear regression model to predict sales:
CREATE MODEL sales.regression
OPTIONS(model_type=‘linear_reg‘, input_label_cols=[‘normalized_sales‘]) AS
SELECT
store_id,
category,
dayofweek,
month,
normalized_sales
FROM sales.features;
With this one SQL statement, we‘ve instructed BigQuery to train a linear regression model named sales.regression. We specify the target label column and select the feature columns to train on.
Under the hood, BigQuery distributes this model training across multiple nodes for faster processing. For a dataset of millions of rows, model training completes in seconds to minutes.
BigQuery supports many other model types besides linear regression:
- Logistic regression for binary classification
- K-means clustering for customer segmentation
- Time series models for forecasting
- Deep neural networks for unstructured data
- Boosted trees for feature-rich datasets
The model building syntax remains very similar across model types. Simply specify the model type in the OPTIONS clause and provide a SQL query selecting the training features and label.
Step 4: Evaluate Model Performance
Once our sales forecasting model is built, the next step is to evaluate how well it performs. We can gauge our regression model‘s accuracy by measuring how closely its predictions match the actual sales values.
With the ML.EVALUATE function, BigQuery makes it easy to assess model performance:
SELECT *
FROM ML.EVALUATE(MODEL sales.regression, (
SELECT
store_id,
category,
dayofweek,
month,
normalized_sales
FROM sales.features
));
This returns an evaluation metrics table with values like:
- Mean absolute error
- Mean squared error
- R-squared
- Explained variance
The mean absolute error tells us on average how many sales dollars our model‘s predictions are off by. R-squared represents the percentage of variance in sales that can be explained by our chosen features.
These metrics help us gauge if our model is sufficiently accurate for our business need. If not, we may want to iterate by selecting different features, doing more preprocessing, or even trying a different model type altogether. The beauty of BigQuery is that we can do all of this model building and evaluation with simple SQL statements.
Step 5: Make Predictions with Trained Model
If we‘re satisfied with our model‘s performance, we‘re ready to use it to make predictions on new data. With the ML.PREDICT function, we can apply our trained model to forecast sales:
SELECT
store_id,
category,
month,
dayofweek,
ML.PREDICT(sales.regression,
store_id, category, dayofweek, month) AS predicted_normalized_sales
FROM new_data;
This applies our linear regression model to each row in the table new_data, returning the predicted normalized sales value. We can easily join this with other tables to translate the predictions into dollar amounts.
By creating a view with this prediction query, we can power BI dashboards to visualize projected sales in Data Studio. Or we could operationalize our model‘s predictions in a custom application using the BigQuery API. The possibilities are endless.
Beyond Custom Models: BigQuery‘s Pre-Trained ML Offerings
We‘ve focused on building custom models so far, but it‘s worth noting that BigQuery also provides pre-trained models and APIs for common ML use cases. These allow you to leverage the power of machine learning without having to gather training data or design model architectures from scratch.
Some of the pre-trained BigQuery ML offerings include:
-
Sentiment analysis for measuring the emotion of text as positive, negative or neutral. Useful for analyzing social media comments, product reviews, or support tickets.
-
Entity extraction for identifying names, organizations, locations, and events mentioned in text. Can help with automatically tagging articles, contracts, or web pages.
-
Image classification for categorizing images based on their content, like identifying product types or brand logos in photos.
-
Product recommendations for e-commerce, to surface products a user is likely to be interested in based on past viewing or purchase history.
To use these pre-trained models, you simply provide your input data in the specified format and call the prediction API. No model training required. Here‘s an example of using the Natural Language API to gauge sentiment on some text data:
SELECT
review_text,
score
FROM reviews,
UNNEST(ML.SENTIMENT(review_text)) score;
This returns a table with the review text and sentiment score between -1 and 1, where negative values indicate negative sentiment and positive values map to positive sentiment.
BigQuery is constantly expanding its selection of pre-trained models, so it‘s worth checking the documentation to see if one fits your use case before building a model from the ground up.
Conclusion and Key Takeaways
In this guide, we‘ve walked through the entire lifecycle of building and using a machine learning model in BigQuery:
- ETL data from source systems into BigQuery tables
- Select features and preprocess data for ML
- Create a model with a SQL command
- Evaluate the model‘s performance on key metrics
- Make predictions on new data using the trained model
We‘ve seen how BigQuery enables machine learning at scale, even for datasets with billions of rows. By providing a familiar SQL interface and fully managed infrastructure, BigQuery empowers data analysts and software developers alike to build sophisticated ML models.
Some key advantages of the BigQuery ML approach:
- Build models where your data already lives, no complex ETL pipelines
- Leverage massive parallelization to train models quickly on huge datasets
- Automatically scale resources up or down without managing clusters
- Use SQL skills you already have, no need to learn new programming languages
- Seamlessly integrate with other Google Cloud services for end-to-end ML workflows
Whether you use the SQL interface, call the REST API from an application, or leverage BigQuery ML in notebooks, you have the flexibility to infuse machine learning into your business in a way that works for you.
I encourage you to try it out with your own data and see how BigQuery can accelerate your machine learning projects. With its ease of use and powerful performance, BigQuery is truly a game changer for making the most of your data with ML.