MySQL HeatWave User Guide

6.7.2.4 Query Model Explanation and Generate Prediction Explanations for a Regression Model

After training a regression model, you can query the default model explanation or query new model explanations. You can also generate prediction explanations. Explanations help you understand which features had the most influence on generating predictions.

Feature importance is presented as an attribution value. A positive value indicates that a feature contributed toward the prediction. A negative value can have different interpretations depending on the specific model explainer used for the model. For example, a negative value for the permutation importance explainer means that the feature is not important.

Before You Begin

Complete the following tasks:

Generate the Model Explanation

After training a model, you can query the default model explanation with the Premutation Importance explainer.

To generate explanations for other model explainers, see Generate Model Explanations and ML_EXPLAIN.

Query the model_explanation column from the model catalog and define the model handle previously created. Update user1 with your own user name. Use JSON_PRETTY to view the output in an easily readable format.

mysql> SELECT JSON_PRETTY(model_explanation) FROM ML_SCHEMA_user1.MODEL_CATALOG 
        WHERE model_handle='regression_use_case';
+------------------------------------------------------------------------------------------------------------------------+
| JSON_PRETTY(model_explanation)                                                                                         |
+------------------------------------------------------------------------------------------------------------------------+
| {
  "permutation_importance": {
    "id": 0.0,
    "state": 0.0814,
    "address": 0.0,
    "house_size": 1.8123
  }
} |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.0530 sec)

Feature importance values display for each column.

Generate Prediction Explanations for a Table

After training a model, you can generate a table of prediction explanations on the house_price_testing dataset by using the default Permutation Importance prediction explainer.

To generate explanations for other model explainers, see Generate Prediction Explanations and ML_EXPLAIN_TABLE.

  1. If not already done, load the model. You can use the session variable for the model that is valid for the duration of the connection. Alternatively, you can use the model handle previously set. For the option to set the user name, you can set it to NULL.

    The following example uses the session variable.

    mysql> CALL sys.ML_MODEL_LOAD(@model, NULL);
    

    The following example uses the model handle.

    mysql> CALL sys.ML_MODEL_LOAD('regression_use_case', NULL);
    
  2. Use the ML_EXPLAIN_TABLE routine to generate explanations for predictions made in the test dataset.

    mysql> CALL sys.ML_EXPLAIN_TABLE(table_name, model_handle, output_table_name, [options]);
    

    Replace table_name, model_handle, and output_table_name with your own values. Add options as needed.

    The following example runs ML_EXPLAIN_TABLE on the testing dataset previously created.

    mysql> CALL sys.ML_EXPLAIN_TABLE('regression_data.house_price_testing', 'regression_use_case', 'regression_data.regression_explanations', 
            JSON_OBJECT('prediction_explainer', 'permutation_importance'));
    Query OK, 0 rows affected (7.3776 sec)

    Where:

    • regression_data.house_price_testing is the fully qualified name of the test dataset.

    • regression_use_case is the model handle for the trained table.

    • regression_data.regression_explanations is the fully qualified name of the output table with explanations.

    • permutation_importance is the selected prediction explainer to use to generate explanations.

  3. Query Notes and ml_results from the output table to review which column contributed the most against or had the largest impact towards the prediction. You can also review individual attribution values for each column. Use \G to view the output in an easily readable format.

    mysql> SELECT Notes, ml_results FROM regression_data.regression_explanations\G
    *************************** 1. row ***************************
         Notes: house_size (1400) increased the value the model predicted the most, whereas state (Nevada) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 64000.0, "state": -4053.33}, 
                 "predictions": {"price": 490000.0}, "notes": "house_size (1400) increased the value the model predicted the most, whereas state (Nevada) reduced the value the model predicted the most"}
    *************************** 2. row ***************************
         Notes: house_size (1900) increased the value the model predicted the most, whereas state (California) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 234000.0, "state": -1760.0}, 
                 "predictions": {"price": 660000.0}, "notes": "house_size (1900) increased the value the model predicted the most, whereas state (California) reduced the value the model predicted the most"}
    *************************** 3. row ***************************
         Notes: house_size (1600) increased the value the model predicted the most, whereas state (Colorado) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 126000.0, "state": -1200.0}, 
                 "predictions": {"price": 552000.0}, "notes": "house_size (1600) increased the value the model predicted the most, whereas state (Colorado) reduced the value the model predicted the most"}
    *************************** 4. row ***************************
         Notes: house_size (2200) increased the value the model predicted the most
    ml_results: {"attributions": {"house_size": 320000.0, "state": 50000.0}, 
                 "predictions": {"price": 810000.0}, "notes": "house_size (2200) increased the value the model predicted the most"}
    *************************** 5. row ***************************
         Notes: house_size (1300) increased the value the model predicted the most, whereas state (Texas) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 52000.0, "state": -5826.67}, 
                 "predictions": {"price": 478000.0}, "notes": "house_size (1300) increased the value the model predicted the most, whereas state (Texas) reduced the value the model predicted the most"}
    *************************** 6. row ***************************
         Notes: house_size (1700) increased the value the model predicted the most, whereas state (Florida) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 168000.0, "state": -1813.33}, 
                 "predictions": {"price": 594000.0}, "notes": "house_size (1700) increased the value the model predicted the most, whereas state (Florida) reduced the value the model predicted the most"}
    *************************** 7. row ***************************
         Notes: house_size (1500) increased the value the model predicted the most, whereas state (Washington) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 92000.0, "state": -3626.67}, 
                 "predictions": {"price": 518000.0}, "notes": "house_size (1500) increased the value the model predicted the most, whereas state (Washington) reduced the value the model predicted the most"}
    *************************** 8. row ***************************
         Notes: house_size (1800) increased the value the model predicted the most, whereas state (Oregon) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 274000.0, "state": -720.0}, 
                 "predictions": {"price": 700000.0}, "notes": "house_size (1800) increased the value the model predicted the most, whereas state (Oregon) reduced the value the model predicted the most"}
    *************************** 9. row ***************************
         Notes: state (Illinois) reduced the value the model predicted the most
    ml_results: {"attributions": {"house_size": 0.0, "state": -7680.0}, 
                 "predictions": {"price": 426000.0}, "notes": "state (Illinois) reduced the value the model predicted the most"}
    *************************** 10. row ***************************
         Notes: house_size (2100) increased the value the model predicted the most
    ml_results: {"attributions": {"house_size": 300000.0, "state": 31200.0}, 
                 "predictions": {"price": 790000.0}, "notes": "house_size (2100) increased the value the model predicted the most"}
    10 rows in set (0.0427 sec)
    

To generate prediction explanations for one or more rows of data, see Generate Prediction Explanations for a Row of Data.

What's Next