EXPLAIN_MODEL 函数 - Amazon Redshift

EXPLAIN_MODEL 函数

EXPLAIN_MODEL 函数返回一个 SUPER 数据类型,其中以 JSON 格式提供了模型可解释性报告。可解释性报告中包含有关所有模型功能的 Shapley 值的信息。

EXPLAIN_MODEL 函数目前仅支持 AUTO ON 或 AUTO OFF XGBoost 模型。

如果未提供可解释性报告,函数将返回显示模型进度的状态。这包括 Waiting for training job to completeWaiting for processing job to completeProcessing job failed

运行 CREATE MODEL 语句时,解释状态将变为 Waiting for training job to complete。当模型经过训练并发送解释请求后,解释状态变为 Waiting for processing job to complete。成功完成模型解释后,即可获得完整的可解释性报告。否则,状态将变为 Processing job failed

运行 CREATE MODEL 语句时,可以使用可选的 MAX_RUNTIME 参数,以指定训练应花费的最大时间量。一旦模型创建时间达到该时间,Amazon Redshift 就会停止创建模型。如果您在创建自动驾驶模型时达到该时间限制,Amazon Redshift 将返回到该时间为止最好的模型。一旦模型训练完成,模型可解释性就变为可用,因此,如果 MAX_RUNTIME 设置为较短的时间,可解释性报告可能不可用。训练时间各不相同,具体取决于模型复杂度、数据大小和其他因素。

语法

EXPLAIN_MODEL ('schema_name.model_name')

参数

schema_name

架构的名称。如果未指定 schema_name,则会选择当前架构。

model_name

模型的名称。schema 中的模型名称必须是唯一的。

返回类型

EXPLAIN_MODEL 函数会返回 SUPER 数据类型,如下所示。

{"version":"1.0","explanations":{"kernel_shap":{"label0":{"global_shap_values":{"x0":0.05,"x1":0.10,"x2":0.30,"x3":0.15},"expected_value":0.50}}}}

示例

以下示例返回了解释状态 waiting for training job to complete

select explain_model('customer_churn_auto_model'); explain_model -------------------------------------------------------- {"explanations":"waiting for training job to complete"} (1 row)

成功完成模型解释后,即可获得完整的可解释性报告,如下所示。

select explain_model('customer_churn_auto_model'); explain_model ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ {"version":"1.0","explanations":{"kernel_shap":{"label0":{"global_shap_values":{"x0":0.05386043365892927,"x1":0.10801289723274592,"x2":0.23227865827017378,"x3":0.0676685133940455,"x4":0.0897097667672375,"x5":0.08502141653270926,"x6":0.07581993936077065,"x7":0.16462880604578135},"expected_value":0.8492974042892456}}}} (1 row)

由于 EXPLAIN_MODEL 函数返回了 SUPER 数据类型,因此您可以查询可解释性报告。这样,您可以提取 global_shap_valuesexpected_value,或特定于功能的 Shapley 值。

以下示例提取了模型的 global_shap_values

select json_table.report.explanations.kernel_shap.label0.global_shap_values from (select explain_model('customer_churn_auto_model') as report) as json_table; global_shap_values -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- {"state":0.10983770427197151,"account_length":0.1772441398408543,"area_code":0.08626823968639591,"phone":0.0736669595282712,"intl_plan":3.344907436910987,"vmail_plan":0.09646600597854467,"vmail_message":0.2064922655089351,"day_mins":2.015038015251777,"day_calls":0.13179511076780168,"day_charge":0.4941091720480879,"eve_mins":0.46081379198626105,"eve_calls":0.16913440417758477,"eve_charge":0.09651014369401761,"night_mins":0.44218153640050845,"night_calls":0.15311640089218997,"night_charge":0.13850366104495426,"intl_mins":0.7583662464883899,"intl_calls":0.47144468610485685,"intl_charge":0.10945894673611875,"cust_serv_calls":0.31822051038387733} (1 row)

以下示例提取了功能 x0 的 global_shap_values

select json_table.report.explanations.kernel_shap.label0.global_shap_values.x0 from (select explain_model('customer_churn_auto_model') as report) as json_table; x0 ------------------------ 0.05386043365892927 (1 row)

如果模型是在特定架构中创建的,且您有权访问所创建的模型,则可以查询模型解释,如下所示。

-- Check the current schema SHOW search_path; search_path ------------------ $user, public (1 row) -- If you have the privilege to access the model explanation -- in `test_schema` SELECT explain_model('test_schema.test_model_name'); explain_model --------------------------------------------------------- {"explanations":"waiting for training job to complete"} (1 row)