Explaining a BigQuery ML model. How to obtain and interpret... | by Lak Lakshmanan | Towards Data Science

BigQuery ML is an easy-to-use way to invoke machine learning models on structured data using just SQL. Although it started with only linear regression, more sophisticated models like Deep Neural Networks and AutoML Tables have been added by connecting BigQuery ML with TensorFlow and Vertex AI as its backend. In other words, although we write SQL, what gets executed is TensorFlow.
BigQuery ML is picking up more features from the Vertex AI backend*. In an earlier post, I showed you hyperparameter tuning. In this post, I’ll show you explainability.
Explainability is a way to understand what a machine learning model is doing. There are two types of explainability. In local explainability, we ask the ML model how it came up with a result for an individual prediction. Why does it say this rental will last 2000 seconds? Why does it suggest that this transaction is fraudulent?
In global explainability, we ask about feature importance. How important is the time-of-day in predicting the duration of a rental? How important is the transaction amount in predicting whether a transaction is fraudulent?
BigQuery ML supports both types of explanations, but in my experience, the local explainability is what end-users want, so I’ll focus on that.
I’ll take the simple model to predict the duration of a bicycle rental that I use throughout my book (BigQuery: The Definitive Guide):
CREATE OR REPLACE MODEL ch09eu.bicycle_linearTRANSFORM(  * EXCEPT(start_date),  CAST(EXTRACT(dayofweek from start_date) AS STRING)         as dayofweek,  CAST(EXTRACT(hour from start_date) AS STRING)         as hourofday)OPTIONS(model_type='linear_reg', input_label_cols=['duration'])ASSELECT  duration  , start_station_name  , start_date    FROM   `bigquery-public-data`.london_bicycles.cycle_hire
Explaining a prediction
Instead of calling ML.PREDICT, we call ML.EXPLAIN_PREDICT to get explanations alongside the predictions:
SELECT * FROM ML.EXPLAIN_PREDICT(    MODEL `ai-analytics-solutions.ch09eu.bicycle_linear`,     (        SELECT 'Park Street, Bankside' AS start_station_name,        CURRENT_TIMESTAMP() AS start_date    )    )
The result:
The predicted duration is 1929 seconds.
The model starts with a baseline prediction value of 4598 seconds — this is the average prediction value over the entire training dataset. From the second input (the timestamp), the model has pulled out the hour-of-day and day-of-week.
The day-of-week being Wednesday, the model adds 247 seconds to the baseline value. In other words, people rent bicycles for slightly longer than average on Wednesdays.
But then, we are looking at 4am. And that causes the rental duration to be adjusted dramatically downwards, by 2800 seconds!
Finally, Parkside is a station where rentals are typically on the lower side. So, the model applies another correction, this time of 116 seconds.
4598 + 247–2800–116 = 1929
As our second example, let’s take a fraud prediction model:
CREATE OR REPLACE MODEL advdata.ulb_fraud_detection TRANSFORM(    * EXCEPT(Amount),    SAFE.LOG(Amount) AS log_amount)OPTIONS(    INPUT_LABEL_COLS=['class'],    AUTO_CLASS_WEIGHTS = TRUE,    DATA_SPLIT_METHOD='seq',    DATA_SPLIT_COL='Time',    MODEL_TYPE='logistic_reg') ASSELECT  *FROM `bigquery-public-data.ml_datasets.ulb_fraud_detection`
Again, we use ML.EXPLAIN_PREDICT:
SELECT * FROM ML.EXPLAIN_PREDICT(    MODEL `ai-analytics-solutions.advdata.ulb_fraud_detection`,     (        SELECT *        FROM `bigquery-public-data.ml_datasets.ulb_fraud_detection`        WHERE Amount BETWEEN 300 AND 305        LIMIT 1    )    )
The result has a fraud predicted_class of 0 (no fraud) with a probability of 0.946. This corresponds to a logits value of -2.87 (the probability is the sigmoid of the logits).
Unfortunately, this dataset doesn’t tell us what the columns are, but the baseline is -2.70 indicating that there is not much unusual about the data values.
Let’s try a different prediction, this time picking a very high-valued transaction:
SELECT * FROM ML.EXPLAIN_PREDICT(    MODEL `ai-analytics-solutions.advdata.ulb_fraud_detection`,     (        SELECT *        FROM `bigquery-public-data.ml_datasets.ulb_fraud_detection`        WHERE Amount > 10000        LIMIT 1    )    )
Now, the model predicts that the transaction is likely to be fraudulent. The logits value is 12.665 whereas the baseline value is -2.7. The key factors behind the dramatically increased probability for this transaction are V4 (which adds 10.67) and V14 (which reduces it by 4). Of course, this would mean more if we knew V4 and V14 were ... but the publicly released financial dataset doesn’t have this information.
As you can see, you can use instance explanations to explain how different inputs caused the resulting value to change. This can help build confidence that the model is working properly.
Enjoy!
* Explainability is implemented in the most efficient way for each model type. In some cases, Vertex AI is called and in other cases, it is done within BigQuery itself. But that’s an implementation detail — regardless of how it’s implemented, you’d call it the same way.