Feature attributions for forecasting

Stay organized with collections Save and categorize content based on your preferences.

Introduction

Vertex Explainable AI integrates feature attributions into Vertex AI. This page provides a brief conceptual overview of the feature attribution methods available with Vertex AI. For an in-depth technical discussion, refer to our AI Explanations Whitepaper.

Feature attributions for time series models indicate how much each feature in a model contributed to a prediction.

Feature attributions measure a feature's contribution to a prediction relative to an input baseline. For numerical features such as sales, the baseline input is the median sales. For categorical features such as the product name, the baseline input is the most common product name. The sum of all attributions is not the prediction, it is how much the prediction differs from the baseline prediction (i.e. all inputs are baseline inputs).

Generate global feature importance

To view global feature importance, examine the evaluation metrics of your model.

Generate local feature attributions

To generate feature attributions, set the generate_explanations parameter to true when performing a Batch Prediction job. All model preparation, such as generating baselines, is managed by the Vertex AI Forecast service.

Feature attributions are determined based on forecasts made for counterfactuals. An example forecast is as follows: What would be the forecast if the advertisement value of TRUE on 2020-11-21 was replaced with FALSE, the most common value? The required number of counterfactuals scales with the number of columns and the number of paths (service generated). The resulting number of predictions may be orders of magnitude larger than a normal batch prediction job and the expected run time scales accordingly.

Query feature attributions

Feature attributions are written to the BigQuery prediction output table as a struct. Note that CSV output is currently not supported.

To access attributions for a feature BQ_FEATURE_NAME:

  1. In the console, go to the BigQuery page.

    Go to BigQuery

  2. Run the following query:

       SELECT explanation.attributions[OFFSET(0)].featureAttributions.BQ_FEATURE_NAME FROM BQ_DATASET_NAME.BQ_PREDICTIONS_TABLE_NAME
    

Note that the nested and repeated fields are standard for batch explanation jobs and are consistent with Vertex AI Tabular.

Examples of feature attribution queries

Example 1: Determine attributions for a single prediction

Consider the following question:

How much did an advertisement for a product increase predicted sales on November 24th at a given store?

The corresponding query is as follows:

SELECT
  * EXCEPT(explanation, predicted_sales),
  ROUND(predicted_sales.value, 2) AS predicted_sales,
  ROUND(
    explanation.attributions[OFFSET(0)].featureAttributions.advertisement,
    2
  ) AS attribution_advertisement
FROM
  `project.dataset.predictions`
WHERE
  product = 'product_0'
  AND store = 'store_0'
  AND date = '2019-11-24'

Example 2: Determine global feature importance

Consider the following question:

How much did each feature contribute to predicted sales overall?

You can manually compute global feature importance by aggregating the local feature importance attributions. The corresponding query is as follows:

WITH

/*
* Aggregate from (id, date) level attributions to global feature importance.
*/
attributions_aggregated AS (
 SELECT
   SUM(ABS(attributions.featureAttributions.date)) AS date,
   SUM(ABS(attributions.featureAttributions.advertisement)) AS advertisement,
   SUM(ABS(attributions.featureAttributions.holiday)) AS holiday,
   SUM(ABS(attributions.featureAttributions.sales)) AS sales,
   SUM(ABS(attributions.featureAttributions.store)) AS store,
   SUM(ABS(attributions.featureAttributions.product)) AS product,
 FROM
   project.dataset.predictions,
   UNNEST(explanation.attributions) AS attributions

),

/*
* Calculate the normalization constant for global feature importance.
*/
attributions_aggregated_with_total AS (
 SELECT
   *,
   date + advertisement + holiday + sales + store + product AS total
 FROM
   attributions_aggregated
)

/*
* Calculate the normalized global feature importance.
*/
SELECT
 ROUND(date / total, 2) AS date,
 ROUND(advertisement / total, 2) AS advertisement,
 ROUND(holiday / total, 2) AS holiday,
 ROUND(sales / total, 2) AS sales,
 ROUND(store / total, 2) AS store,
 ROUND(product / total, 2) AS product,
FROM
 attributions_aggregated_with_total

Advantages

If you inspect specific instances, and also aggregate feature attributions across your training dataset, you can get deeper insight into how your model works. Consider the following advantages:

  • Debugging models: Feature attributions can help detect issues in the data that standard model evaluation techniques would usually miss.

  • Optimizing models: You can identify and remove features that are less important, which can result in more efficient models.

Conceptual limitations

Consider the following limitations of feature attributions:

  • Feature attributions, including local feature importance for AutoML, are specific to individual predictions. Inspecting the feature attributions for an individual prediction may provide good insight, but the insight may not be generalizable to the entire class for that individual instance, or the entire model.

    To get more generalizable insight for AutoML models, refer to the model feature importance. To get more generalizable insight for other models, aggregate attributions over subsets over your dataset, or the entire dataset.

  • Each attribution only shows how much the feature affected the prediction for that particular example. A single attribution might not reflect the overall behavior of the model. To understand approximate model behavior on an entire dataset, aggregate attributions over the entire dataset.

  • Although feature attributions can help with model debugging, they do not always indicate clearly whether an issue arises from the model or from the data that the model is trained on. Use your best judgment, and diagnose common data issues to narrow the space of potential causes.

  • The attributions depend entirely on the model and data used to train the model. They can only reveal the patterns the model found in the data, and can't detect any fundamental relationships in the data. The presence or absence of a strong attribution to a certain feature doesn't mean there is or is not a relationship between that feature and the target. The attribution merely shows that the model is or is not using the feature in its predictions.

  • Attributions alone cannot tell if your model is fair, unbiased, or of sound quality. Carefully evaluate your training data and evaluation metrics in addition to the attributions.

For more information about limitations, refer to the AI Explanations Whitepaper.

Improving feature attributions

The following factors have the highest impact on feature attributions:

  • The attribution methods approximate the Shapley value. You can increase the precision of the approximation by increasing the number of paths for the sampled Shapley method. As a result, the attributions could change dramatically.
  • The attributions only express how much the feature affected the change in prediction value, relative to the baseline value. Be sure to choose a meaningful baseline, relevant to the question you're asking of the model. Attribution values and their interpretation might change significantly as you switch baselines.

You can view the path count and the baselines in the Explanation Parameters and Metadata.

View explanation metadata and parameters

The Explanation Parameters and Metadata contain the following:

  • static_value: The baselines used to generate explanations.
  • pathCount: The number of paths, a factor in the amount of time it takes to generate feature attributions.
  • historical_values, prediction_values: Columns available at forecast.
  • historical_values: Columns unavailable at forecast.

The model can be viewed using the Vertex AI REST API and includes the explanation spec.

REST

Before using any of the request data, make the following replacements:

  • LOCATION: Region where your model is stored
  • PROJECT: Your project ID.
  • MODEL_ID: The ID of the model resource

HTTP method and URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID

To send your request, choose one of these options:

curl

Execute the following command:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID "

PowerShell

Execute the following command:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/models/MODEL_ID " | Select-Object -Expand Content

You should see output similar to the following for a trained AutoML model.

Algorithm

Vertex AI provides feature attributions using Shapley Values, a cooperative game theory algorithm that assigns credit to each player in a game for a particular outcome. Applied to machine learning models, this means that each model feature is treated as a "player" in the game and credit is assigned in proportion to the outcome of a particular prediction. For structured data models, Vertex AI uses a sampling approximation of exact Shapley Values called Sampled Shapley.

For in-depth information on how the sampled Shapley method works, read the paper Bounding the Estimation Error of Sampling-based Shapley Value Approximation.

What's next

The following resources provide further useful educational material: