
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
In this lab, you use the penguin table
to create a model that predicts the weight of a penguin based on the penguin's species, island of residence, culmen length and depth, flipper length, and sex.
This lab introduces data analysts to BigQuery ML. BigQuery ML enables users to create and execute machine learning models in BigQuery using SQL queries. The goal is to democratize 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.
CREATE MODEL
statement with BigQuery ML.ML.EVALUATE
function.ML.PREDICT
function.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 first step is to create a BigQuery dataset to store your ML model. To create your dataset:
In the Cloud Console, on the Navigation menu, click BigQuery.
In the Explorer panel, click the View actions icon (three vertical dots) next to your project ID, and then click Create dataset.
On the Create dataset page:
For Dataset ID, type bqml_tutorial
(Optional) For Data location, select us (multiple regions in United States).
Currently, the public datasets are stored in the US multi-region location. For simplicity, you should place your dataset in the same location.
Next, you create a linear regression model using the penguins table for BigQuery.
CREATE MODEL
command trains the model you create.The CREATE MODEL
clause is used to create and train the model named bqml_tutorial.penguins_model
.
The OPTIONS(model_type='linear_reg', input_label_cols=['body_mass_g'])
clause indicates that you are creating a linear regression model. A linear regression is a type of regression model that generates a continuous value from a linear combination of input features. The body_mass_g
column is the input label column. For linear regression models, the label column must be real-valued (the column values must be real numbers).
This query's SELECT
statement uses all the columns in the bigquery-public-data.ml_datasets.penguins
table. This table contains the following columns that will all be used to predict a penguin's weight:
species
: Species of penguin (STRING)island
: Island that the penguin lives on (STRING)culmen_length_mm
: Length of culmen in millimeters (FLOAT64)culmen_depth_mm
: Depth of culmen in millimeters (FLOAT64)flipper_length_mm
: Length of the flipper in millimeters (FLOAT64)sex
: The sex of the penguin (STRING)The FROM
clause — bigquery-public-data.ml_datasets.penguins
— indicates that you are querying the penguins table in the ml_datasets
dataset. This dataset is in the bigquery-public-data
project.
The WHERE
clause — WHERE body_mass_g IS NOT NULL
— excludes rows where body_mass_g
is NULL.
To run the CREATE MODEL
query to create and train your model:
In the Cloud Console, click Create SQL query.
In the Query editor text area, enter the following standard SQL query:
The query takes about 30 seconds to complete, after which your model (penguins_model
) appears in the navigation panel. Because the query uses a CREATE MODEL
statement to create a table, you do not see query results.
To see the results of the model training, you can use the ML.TRAINING_INFO
function, or you can view the statistics in the Cloud Console. In this tutorial, you use the Cloud Console.
A machine learning algorithm builds a model by examining many examples and attempting to find a model that minimizes loss. This process is called empirical risk minimization.
Loss is the penalty for a bad prediction: a number indicating how bad the model's prediction was on a single example. If the model's prediction is perfect, the loss is zero; otherwise, the loss is greater. The goal of training a model is to find a set of weights and biases that have low loss, on average, across all examples.
To see the model training statistics that were generated when you ran the CREATE MODEL
query:
In the Cloud Console navigation panel, in the Explorer section, expand [PROJECT_ID] > bqml_tutorial > Models (1), and then click penguins_model.
Click the Training tab, and then click Table. The results should look like the following:
The Training Data Loss column represents the loss metric calculated after the model is trained on the training dataset. Because you performed a linear regression, this column is the mean squared error.
A "normal_equation" optimization strategy is automatically used for this training, so only one iteration is required to converge to the final model. For more details on the optimize_strategy
option, see the CREATE MODEL statement for generalized linear models.
For more details on the ML.TRAINING_INFO
function and "optimize_strategy" training option, see the BigQuery ML syntax reference.
After creating your model, you evaluate the performance of the model using the ML.EVALUATE
function. The ML.EVALUATE
function evaluates the predicted values against the actual data.
SELECT
statement retrieves the columns from your model.FROM
clause uses the ML.EVALUATE function against your model: bqml_tutorial.penguins_model
.SELECT
statement and FROM
clause are the same as those in the CREATE MODEL
query.WHERE
clause — WHERE body_mass_g IS NOT NULL
— excludes rows where body_mass_g is NULL.A proper evaluation would be on a subset of the penguins table that is separate from the data used to train the model. You can also call ML.EVALUATE
without providing the input data. ML.EVALUATE will retrieve the evaluation metrics calculated during training, which uses the automatically reserved evalution dataset:
You can also use the Cloud Console to view the evaluation metrics calculated during the training. The results should look like the following:
To run the ML.EVALUATE
query that evaluates the model:
In the Cloud Console, click Create SQL query.
In the Query editor text area, enter the following standard SQL query:
Click Run.
When the query is complete, click the Results tab below the query text area. The results should look like the following:
Because you performed a linear regression, the results include the following columns:
mean_absolute_error
mean_squared_error
mean_squared_log_error
median_absolute_error
r2_score
explained_variance
An important metric in the evaluation results is the R2 score. The R2 score is a statistical measure that determines whether the linear regression predictions approximate the actual data. 0 indicates that the model explains none of the variability of the response data around the mean. 1 indicates that the model explains all the variability of the response data around the mean.
Now that you have evaluated your model, the next step is to use it to predict an outcome. You use your model to predict the body mass in grams of all penguins that reside in Biscoe.
The first SELECT
statement retrieves the predicted_body_mass_g
column along with the columns in bigquery-public-data.ml_datasets.penguins
. This column is generated by the ML.PREDICT
function. When you use the ML.PREDICT
function, the output column name for the model is predicted_<label_column_name>
. For linear regression models, predicted_label
is the estimated value of label
. For logistic regression models, predicted_label
is one of the two input labels depending on which label has the higher predicted probability.
ML.PREDICT
function is used to predict results using your model: bqml_tutorial.penguins_model
.statement
and FROM
clause are the same as those in the CREATE MODEL
query.WHERE
clause — WHERE island = "Biscoe"
— indicates that you are limiting the prediction to the island of Biscoe.To run the query that uses the model to predict an outcome:
In the Cloud Console, click Create SQL query.
In the Query editor text area, enter the following standard SQL query:
Click Run.
When the query is complete, click the Results tab below the query text area. The results should look like the following:
To understand why your model is generating these prediction results, you can use the ML.EXPLAIN_PREDICT
function.
ML.EXPLAIN_PREDICT
is an extended version of ML.PREDICT
. ML.EXPLAIN_PREDICT
returns prediction results with additional columns that explain those results.
You can run ML.EXPLAIN_PREDICT
without ML.PREDICT
. For an in-depth explanation of Shapley values and explainable AI in BigQuery ML, see BigQuery ML explainable AI overview.
To run the ML.EXPLAIN_PREDICT
query that explains the model:
In the Cloud Console, click Create SQL query.
In the Query editor text area, enter the following standard SQL query:
Click Run.
When the query is complete, click the Results tab below the query text area. The results should look like the following:
For linear regression models, Shapley values are used to generate feature attribution values per feature in the model. ML.EXPLAIN_PREDICT
outputs the top 3 feature attributions per row of the table provided because top_k_features
was set to 3 in the query.
These attributions are sorted by the absolute value of the attribution in descending order. In all examples, the feature sex
contributed the most to the overall prediction. For detailed explanations of the output columns of the ML.EXPLAIN_PREDICT
query, see ML.EXPLAIN_PREDICT
syntax documentation
To know which features are the most important to determine the weights of the penguins in general, you can use the ML.GLOBAL_EXPLAIN
function. In order to use ML.GLOBAL_EXPLAIN
, the model must be retrained with the option ENABLE_GLOBAL_EXPLAIN=TRUE
.
To run the ML.GLOBAL_EXPLAIN
query:
In the Cloud Console, click Create SQL query.
In the Query editor text area, enter the following standard SQL query:
Click Run.
When the query is complete, click the Results tab below the query text area. The results should look like the following:
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Deleting your project removes all datasets and all tables in the project. If you prefer to reuse the project, you can delete the dataset you created in this tutorial:
If necessary, open the BigQuery page in the Cloud Console.
In the Explorer panel, click View actions () next to your dataset.
Click Delete.
In the Delete dataset dialog box, to confirm the delete command, type delete and then click Delete.
To delete the project:
In the project list, select the project that you want to delete, and then click Delete.
In the dialog, type the project ID, and then click Shut down to delete the project.
You've learned how to:
CREATE MODEL
statement with BigQuery ML.ML.EVALUATE
function.ML.PREDICT
function.To learn more about machine learning, see the Machine learning crash course.
For an overview of BigQuery ML, see Introduction to BigQuery ML.
To learn more about the Cloud Console, see Using the Cloud Console.
When you have completed your lab, click End Lab. Qwiklabs 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