

 Amazon Redshift は、パッチ 198 以降、新しい Python UDF の作成をサポートしなくなります。既存の Python UDF は、2026 年 6 月 30 日まで引き続き機能します。詳細については、[ブログ記事](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)を参照してください。

# EXPLAIN\$1MODEL関数
<a name="r_explain_model_function"></a>

EXPLAIN\$1MODEL 関数は、JSON 形式のモデルの説明可能性レポートを含む SUPER データ型を返します。説明可能性レポートには、すべてのモデル機能の Shapley 値に関する情報が含まれています。

EXPLAIN\$1MODEL 関数は、現在、XGBoost モデルのAUTO ON oまたはAUTO OFF のみをサポートしています。

説明可能性レポートが利用できない場合、関数はモデルの進行状況を示すステータスを返します。たとえば`Waiting for training job to complete`、`Waiting for processing job to complete`、および`Processing 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` を短い時間に設定すると、説明可能性レポートは利用できない場合があります。トレーニング時間はさまざまであり、モデルの複雑さ、データサイズ、その他の要因によって異なります。

## 構文
<a name="r_explain_model_function-synopsis"></a>

```
EXPLAIN_MODEL ('schema_name.model_name')
```

## 引数
<a name="r_explain_model_function-argument"></a>

 *schema\$1name*   
スキーマの名前。スキーマ名 が指定されていない場合は、現在のスキーマが選択されます。

 *model\$1name*   
モデルの名前です。スキーマ内のモデル名は一意でなければなりません。

## 戻り型
<a name="r_explain_model_function-return-type"></a>

EXPLAIN\$1MODEL関数は、次に示すように、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}}}}
```

## 例
<a name="r_explain_model_function-examples"></a>

次の例では、説明状態を返します`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\$1MODEL 関数は SUPER データ型を返すため、説明可能性レポートをクエリできます。これにより、`global_shap_values`、`expected_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)
```