
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create a new dataset
/ 5
Create a model and specify model options
/ 5
Evaluate classification model performance
/ 5
Improve model performance with Feature Engineering(Create second model)
/ 5
Improve model performance with Feature Engineering(Better predictive power)
/ 5
Predict which new visitors will come back and purchase
/ 5
BigQuery is Google's fully managed, NoOps, low cost analytics database. With BigQuery you can query terabytes and terabytes of data without having any infrastructure to manage or needing a database administrator. BigQuery uses SQL and can take advantage of the pay-as-you-go model. BigQuery allows you to focus on analyzing data to find meaningful insights.
BigQuery Machine Learning (BQML, product in beta) is a new feature in BigQuery where data analysts can create, train, evaluate, and predict with machine learning models with minimal coding.
There is a newly available ecommerce dataset that has millions of Google Analytics records for the Google Merchandise Store loaded into BigQuery. In this lab you will use this data to run some typical queries that businesses would want to know about their customers' purchasing habits.
In this lab, you learn to perform the following tasks:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
The Welcome to BigQuery in the Cloud Console message box opens.
BigQuery public datasets are not displayed by default in the BigQuery web UI.
To open the public datasets project:
Click + ADD.
Select Star a project by name.
For Project name, enter data-to-insights
.
Click STAR.
In the left pane, under Show starred only you will see the data-to-insights project pinned.
console.cloud.google.com/bigquery?p=data-to-insights&d=ecommerce&t=web_analytics&page=table
The field definitions for the data-to-insights ecommerce dataset are in the [UA] BigQuery Export schema document. Keep the link open in a new tab for reference.
Scenario: Your data analyst team exported the Google Analytics logs for an ecommerce website into BigQuery and created a new table of all the raw ecommerce visitor session data for you to explore. Using this data, you'll try to answer a few questions.
Question: Out of the total visitors who visited our website, what % made a purchase?
The result: 2.69%
Question: What are the top 5 selling products?
The result:
Row | v2ProductName | v2ProductCategory | units_sold | revenue |
---|---|---|---|---|
1 | Nest® Learning Thermostat 3rd Gen-USA - Stainless Steel | Nest-USA | 17651 | 870976.95 |
2 | Nest® Cam Outdoor Security Camera - USA | Nest-USA | 16930 | 684034.55 |
3 | Nest® Cam Indoor Security Camera - USA | Nest-USA | 14155 | 548104.47 |
4 | Nest® Protect Smoke + CO White Wired Alarm-USA | Nest-USA | 6394 | 178937.6 |
5 | Nest® Protect Smoke + CO White Battery Alarm-USA | Nest-USA | 6340 | 178572.4 |
Question: How many visitors bought on subsequent visits to the website?
The results:
Row | total_visitors | will_buy_on_return_visit |
---|---|---|
1 | 729848 | 0 |
2 | 11873 | 1 |
Analyzing the results, you can see that (11873 / 729848) = 1.6% of total visitors will return and purchase from the website. This includes the subset of visitors who bought on their very first session and then came back and bought again.
Question: What are some of the reasons a typical ecommerce customer will browse but not buy until a later visit?
Answer: Although there is no one right answer, one popular reason is comparison shopping between different ecommerce sites before ultimately making a purchase decision. This is very common for luxury goods where significant up-front research and comparison is required by the customer before deciding (think car purchases) but also true to a lesser extent for the merchandise on this site (t-shirts, accessories, etc).
In the world of online marketing, identifying and marketing to these future customers based on the characteristics of their first visit will increase conversion rates and reduce the outflow to competitor sites.
Now you will create a Machine Learning model in BigQuery to predict whether or not a new user is likely to purchase in the future. Identifying these high-value users can help your marketing team target them with special promotions and ad campaigns to ensure a conversion while they comparison shop between visits to your ecommerce site.
Google Analytics captures a wide variety of dimensions and measures about a user's visit on this ecommerce website. Browse the complete list of fields and then preview the demo dataset to find useful features that will help a machine learning model understand the relationship between data about a visitor's first time on your website and whether they will return and make a purchase.
Your team decides to test whether these two fields are good inputs for your classification model:
totals.bounces
(whether the visitor left the website immediately)totals.timeOnSite
(how long the visitor was on our website)Question: What are the risks of only using the above two fields?
Answer: Machine learning is only as good as the training data that is fed into it. If there isn't enough information for the model to determine and learn the relationship between your input features and your label (in this case, whether the visitor bought in the future) then you will not have an accurate model. While training a model on just these two fields is a start, you will see if they're good enough to produce an accurate model.
Results:
Row | bounces | time_on_site | will_buy_on_return_visit |
---|---|---|---|
1 | 0 | 15047 | 0 |
2 | 0 | 12136 | 0 |
3 | 0 | 11201 | 0 |
4 | 0 | 10046 | 0 |
5 | 0 | 9974 | 0 |
6 | 0 | 9564 | 0 |
7 | 0 | 9520 | 0 |
8 | 0 | 9275 | 1 |
9 | 0 | 9138 | 0 |
10 | 0 | 8872 | 0 |
Which fields are the model features? What is the label (correct answer)?
The inputs are bounces and time_on_site. The label is will_buy_on_return_visit.
Question: Which two fields are known after a visitor's first session?
Answer: bounces and time_on_site are known after a visitor's first session.
Question: Which field isn't known until later in the future?
Answer: will_buy_on_return_visit is not known after the first visit. Again, you're predicting for a subset of users who returned to your website and purchased. Since you don't know the future at prediction time, you cannot say with certainty whether a new visitor come back and purchase. The value of building a ML model is to get the probability of future purchase based on the data gleaned about their first session.
Question: Looking at the initial data results, do you think time_on_site and bounces will be a good indicator of whether the user will return and purchase or not?
Answer: It's often too early to tell before training and evaluating the model, but at first glance out of the top 10 time_on_site
, only 1 customer returned to buy, which isn't very promising. Let's see how well the model does.
Next, create a new BigQuery dataset which will also store your ML models.
For Dataset ID enter ecommerce. Leave the other options at their default values (Data Location, Default table Expiration).
Click Create dataset.
You'll now see the ecommerce dataset under your project name.
Click Check my progress to verify the objective.
Now that you have your initial features selected, you are now ready to create your first ML model in BigQuery.
There are two model types to choose from:
Model | Model Type | Label Data type | Example |
---|---|---|---|
Forecasting | linear_reg | Numeric value (typically an integer or floating point) | Forecast sales figures for next year given historical sales data. |
Classification | logistic_reg | 0 or 1 for binary classification | Classify an email as spam or not spam given the context. |
Which model type should you choose?
Since you are bucketing visitors into "will buy in future" or "won't buy in future", use logistic_reg
in a classification model.
Wait for the model to train (5 - 10 minutes).
Click Check my progress to verify the objective.
After your model is trained, you will see a message similar to the following: "This statement will create a new model named qwiklabs-gcp-01-00af1a3268df:ecommerce.classification_model. Depending on the type of model, this may take several hours to complete." This indicates that your model has been successfully trained.
Next, you will evaluate the performance of the model against new unseen evaluation data.
For classification problems in ML, you want to minimize the False Positive Rate (predict that the user will return and purchase and they don't) and maximize the True Positive Rate (predict that the user will return and purchase and they do).
This relationship is visualized with a ROC (Receiver Operating Characteristic) curve like the one shown here, where you try to maximize the area under the curve or AUC:
In BQML, roc_auc is simply a queryable field when evaluating your trained ML model.
Now that training is complete, you can evaluate how well the model performs with this query using ML.EVALUATE
.
You should see the following result:
Row | roc_auc | model_quality |
---|---|---|
1 | 0.724588 | decent |
After evaluating your model you get a roc_auc of 0.72, which shows the model has decent, but not great, predictive power. Since the goal is to get the area under the curve as close to 1.0 as possible, there is room for improvement.
Click Check my progress to verify the objective.
As was hinted at earlier, there are many more features in the dataset that may help the model better understand the relationship between a visitor's first session and the likelihood that they will purchase on a subsequent visit.
Let's add some new features and create a second machine learning model which will be called classification_model_2:
A new key feature that was added to the training dataset query is the maximum checkout progress each visitor reached in their session, which is recorded in the field hits.eCommerceAction.action_type
. If you search for that field in the field definitions you will see the field mapping of 6 = Completed Purchase.
As an aside, the web analytics dataset has nested and repeated fields like ARRAYS which need to broken apart into separate rows in your dataset. This is accomplished by using the UNNEST() function, which you can see in the above query.
Wait for the new model to finish training (5-10 minutes).
Click Check my progress to verify the objective.
Evaluate this new model to see if there is better predictive power.
Output:
Row | roc_auc | model_quality |
---|---|---|
1 | 0.910382 | good |
With this new model you now get a roc_auc of 0.91 which is significantly better than the first model.
Now that you have a trained model, time to make some predictions.
Click Check my progress to verify the objective.
Next you will write a query to predict which new visitors will come back and make a purchase. The prediction query below uses the improved classification model we trained above to predict the probability that a first-time visitor to the Google Merchandise Store will make a purchase in a later visit. The predictions are made on the last 1 month (out of 12 months) of the dataset.
Click Check my progress to verify the objective.
Your model will now output the predictions it has for those July 2017 ecommerce sessions. You can see three newly added fields:
Tip: Add warm_start = true
to your model options if you are retraining new data on an existing model for faster training times. Note that you cannot change the feature columns (this would necessitate a new model).
roc_auc is just one of the performance metrics available during model evaluation. Also available are accuracy, precision, and recall. Knowing which performance metric to rely on is highly dependent on what your overall objective or goal is.
You can use this below link to bring in the bigquery-public-data project if you want to explore modeling on other datasets like forecasting fares for taxi trips:
Test your knowledge about Google cloud Platform by taking our quiz.
You've successfully built a ML model in BigQuery to classify ecommerce visitors.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one