Points de contrôle
Create a BigQuery dataset
/ 20
Create a model to predict visitor transaction
/ 20
Evaluate the model
/ 20
Predict purchases per country
/ 20
Predict purchases per user
/ 20
Getting Started with BigQuery ML
GSP247
Overview
BigQuery ML enables users to create and execute machine learning models in BigQuery using SQL queries. The goal is to democratise machine learning by enabling SQL practitioners to build models using their existing tools and to increase development speed by eliminating the need for data movement.
There is an available ecommerce dataset that has millions of Google Analytics records for the Google Merchandise Store loaded into BigQuery. In this lab, you use this data to create a model that predicts whether a visitor will make a transaction.
What you'll learn
In this lab, you learn how to:
- Create BigQuery datasets
- Create, evaluate, and use machine learning models in BigQuery
Prerequisite
To maximize your learning you should have a basic knowledge of SQL or BigQuery.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Open the BigQuery console
- In the Google Cloud Console, select Navigation menu > BigQuery.
The Welcome to BigQuery in the Cloud Console message box opens. This message box provides a link to the quickstart guide and the release notes.
- Click Done.
The BigQuery console opens.
Task 1. Create a dataset
- To create a dataset, click on the View actions icon next to your project ID and select Create dataset.
- Next, name your Dataset ID
bqml_lab
and click Create dataset.
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted an assessment score.
Task 2. Create a model
Now, move on to your task!
- Go to BigQuery EDITOR, type or paste the following query to create a model that predicts whether a visitor will make a transaction:
- Click RUN.
Here the visitor's device's operating system is used, whether said device is a mobile device, the visitor's country and the number of page views as the criteria for whether a transaction has been made.
In this case, bqml_lab
is the name of the dataset and sample_model
is the name of the model. The model type specified is binary logistic regression. In this case, label
is what you're trying to fit to.
input_label_cols
.
The training data is being limited to those collected from 1 August 2016 to 30 June 2017. This is done to save the last month of data for "prediction". It is further limited to 100,000 data points to save some time.
Running the CREATE MODEL
command creates a Query Job that will run asynchronously so you can, for example, close or refresh the BigQuery UI window.
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted an assessment score.
(Optional) Model information & training statistics
If interested, you can get information about the model by expanding bqml_lab
dataset and then clicking the sample_model
model in the UI. Under the Details tab you should find some basic model info and training options used to produce the model. Under Training, you should see a table either a table or graphs, depending on your View as settings:
Task 3. Evaluate the model
- Replace the previous query with the following and then click Run:
If used with a linear regression model, the above query returns the following columns:
-
mean_absolute_error
,mean_squared_error
,mean_squared_log_error
, -
median_absolute_error
,r2_score
,explained_variance
.
If used with a logistic regression model, the above query returns the following columns:
-
precision
,recall
-
accuracy
,f1_score
-
log_loss
,roc_auc
Please consult the machine learning glossary or run a Google search to understand how each of these metrics are calculated and what they mean.
You'll realize the SELECT
and FROM
portions of the query are identical to that used during training. The WHERE
portion reflects the change in time frame and the FROM
portion shows that you're calling ml.EVALUATE
.
You should see a table similar to this:
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted an assessment score.
Task 4. Use the model
Predict purchases per country
With this query you will try to predict the number of transactions made by visitors of each country, sort the results, and select the top 10 countries by purchases:
- Replace the previous query with the following and then click Run:
This query is very similar to the evaluation query demonstrated in the previous section. Instead of ml.EVALUATE
, you're using ml.PREDICT
and the BigQuery ML portion of the query is wrapped with standard SQL commands. For this lab you're interested in the country and the sum of purchases for each country, so that's why SELECT
, GROUP BY
and ORDER BY
. LIMIT
is used to ensure you only get the top 10 results.
You should see a table similar to this:
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted an assessment score.
Predict purchases per user
Here is another example. This time you will try to predict the number of transactions each visitor makes, sort the results, and select the top 10 visitors by transactions:
- Replace the previous query with the following and then click Run:
You should see a table similar to this:
Test completed task
Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted an assessment score.
Task 5. Test your understanding
Below are multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.
Congratulations!
You used BigQuery ML to create a binary logistic regression model, evaluate the model, and use the model to make predictions.
Next steps / Learn more
- For more information on BigQuery ML, see the documentation.
- Have a Google Analytics account and want to query your own datasets in BigQuery? Follow this export guide.
- The complete BigQuery SQL reference guide is here as an additional resource: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 22, 2024
Lab Last Tested February 22, 2024
Copyright 2024 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.