
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
BQ Query calculate shot distance
/ 20
BQ Query calculate shot angle
/ 20
BQ Query expected goal with ML
/ 20
BQ Query model for expected goals
/ 20
BQ Query model unlikely goals
/ 20
The data used in this lab comes from the following sources:
In this lab, you will learn how to:
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 are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
The BigQuery console provides an interface to query tables, including public datasets offered by 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.
The BigQuery console opens.
Once the tables are created the display will be similar to below:
In the next section learn more about creating functions in BigQuery.
In this section, you will create some user-defined functions in BigQuery that help with shot distance and angle calculations, helping to prepare the soccer event data for eventual use in an ML model.
To motivate the need for the function, start by looking at the positions field in the events table a bit more - this is a repeated field that contains 1 or more (x, y) pairs per event.
Per Wyscout, a leading data company in the soccer industry that provided this data, these represent the origin and (if applicable) destination positions associated with the event, on a 0-100 scale representing the percentage of the field from the perspective of the attacking team. See the screenshot of the table below showing positions corresponding to a few different event types for a few example events.
The first (x, y) pair in positions for events that are shots represent the location on the field from which the shot originated.
This specifies the name of the function, the inputs and their types (2 integers), the output type (a float), and the actual logic implementing the 2-dimensional distance formula on the scaled x- and y-coordinate distances.
The Query results section should display a message saying This statement created a new function named:
Click Check my progress to verify the objective
The general process here is similar to the shot distance calculation, but applied to shot angle. In this case, the angle calculated is the one made by the location of the shot and the goal line, as shown below (image credit to Ian Dragulet).
Larger angles arise from being close to the goal and in the center, so this is somewhat correlated with the distance calculation performed above. The shot angle calculations involve using BigQuery's trigonometric functions on the (x, y) data.
Click Check my progress to verify the objective
In this section you used BigQuery to make functions to calculate shot distance and angle.
In the next section learn how to build a model to calculate expected goals using the distance and angle of each shot.
In this section, you will use BigQuery ML to create and execute machine learning models in BigQuery using standard SQL queries. In this case, you will build expected goals models on the soccer event data to predict the likelihood of a shot going in for a goal given its type, distance, and angle (these are known to be good predictors of the chance of a goal, as demonstrated in the BigQuery Soccer Data Analytical Insight lab).
Expected goals models are commonly used in soccer analytics to measure the quality of shots and finishing/saving ability given shot quality, and they have a variety of applications in both retrospective match analysis and making projections.
First, you will fit a logistic regression model to the data.
The top section is the actual model generation code, specifying the type of model and label for the outcome variable. The SELECT statement creates the isGoal outcome variable along with features of interest from the event data, including the shot distance and angle calculated using the functions created in the previous step. The joins enable the determination of which competition each shot came from, which is employed to filter out the World Cup data from this modeling (saved for use later on after the model is fit).
The Query results section should display a message saying This statement will create a new model named:
This will open up a new tab that has information about the model that was just trained.
Click Check my progress to verify the objective
Since the primary interest lies in the accuracy of goal probabilities from the model (not explicitly accuracy in terms of predicting 0s and 1s at a given threshold), log loss and ROC AUC are good metrics to focus on. The numbers themselves are most useful in comparing different models, which is done in a later section.
Now that you've fit the logistic regression model, you can use BigQuery ML's weights functionality to see the model coefficients for each predictor.
In the query EDITOR, click "+" (SQL query).
Copy and paste the following code into the query EDITOR. This is a simple call to the ML.WEIGHTS function to the logistic regression model that you fit in the previous step:
There are weights for each input - a single numerical weight for the continuous features, 1 value per potential category for the categorical features - that correspond to coefficients in a logistic regression model. It's sometimes dangerous to interpret logistic regression model coefficients directly because of correlation among the predictors, among other things.
At minimum, the sign of these seem reasonable based on general subject matter knowledge of soccer:
Seeing these results can provide some confidence that the model is doing something logical.
Next, you will fit a boosted tree model to the data using BigQuery ML's implementation of XGBoost, and compare the results to the previously fit logistic regression model. In theory, boosted trees can be more accurate because of their ability to take into account nonlinear relationships between features and the outcome as well as interactions among features.
The SQL statement is identical to the one to fit the logistic regression above, except for changes in the model type to pick a boosted tree classifier and the name of the model object created.
While you wait, check out this overview of BigQuery ML.
Once the model has been trained, verify the step has been completed successfully.This will open up a new tab that has information about the model that was just trained.
The log loss and ROC AUC of the boosted tree model is pretty similar to that from the logistic regression model, but slightly worse (higher log loss and lower ROC AUC).
Click Check my progress to verify the objective
In this section BigQuery was used to fit machine learning models using soccer data. An expected goals model can be created in a variety of ways.
In the next section learn how to apply an expected goals model to new data using BigQuery ML.
Now that you've fit an expected goals model of reasonable accuracy and explainability, you can apply it to "new" data - in this case, the 2018 World Cup (which was left out of the model fitting).
The logistic regression model created in the previous step will be used to assess the difficulty of each shot and goal in that competition, enabling identification of the most "impressive" goals in the tournament.
You can use BigQuery ML's prediction functionality with the logistic regression model fit in the previous step (or even the boosted tree model, if you prefer) to look at the probability of each shot scoring or not in the World Cup.
This calls the ML.PREDICT function on the logistic regression model that was fit previously.
The SELECT statement creates the same fields on all shots from the events data as the ones used to fit the model, but this time it filters to only World Cup matches (instead of leaving them out) since those are the shots the model is to be applied to.
The output shows a couple of prediction-related fields along with the original fields in that data of shot type, distance, angle, and success:
Building on the previous section that yielded probabilities for all shots in the 2018 World Cup, use BigQuery to extract the probability of each goal, then sort from lowest to highest to see the most unlikely goals scored in the World Cup based on the factors in the model.
Theoretically, this should yield a list of some of the most impressive goals in the tournament, at least based on the factors in the model (distance, angle, etc.).
This builds on the ML.PREDICT function call in the previous section, filtering to only shots that were goals (excluding penalties, since the goal is to get the most "impressive" goals), extracting only the probability of a goal being scored from the predictions, and ordering by that.
A few fields are added in the SELECT statement from the events data and the other tables that can be joined to it to get other information of interest on each goal like the player and team who scored it, match date and information, and minute into the match of the goal.
Click Check my progress to verify the objective
The top few lines show the 2018 World Cup goals that the model believed had the least likely chance of going in - all with fairly long shot distances (near 30 meters) and relatively tight angles (between 10° and 15°).
For some visual validation, you can watch the Antoine Griezmann goal that earned the top spot on this list - to be fair, it's more of an unlikely goal than an impressive one based on how it was played by the goalkeeper.
For more of a "wow" goal in line with what this analysis intends to uncover, check out this amazing shot by Korea Republic's Son Heung-min vs Mexico that came in second in the results.
In the next section test your understanding of what you have learned in this introduction to BigQuery ML.
Test your understanding of BigQuery by completing the short quiz on the topics covered in this lab.
You have created machine learning models with soccer data. You created user-defined functions in BigQuery to calculate shot distance and shot angle, then used BigQuery ML to build an expected goals model, and used BigQuery ML's prediction functionality on "new" data from the 2018 World Cup to determine some of the most impressive goals in the tournament.
...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 January 30, 2025
Lab Last Tested January 30, 2025
Copyright 2025 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.
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
One lab at a time
Confirm to end all existing labs and start this one