SHAPリファレンス¶
SHAP(SHapley Additive exPlanations)は、精度と説明可能性のジレンマに対処するために使用されるオープンソースのアルゴリズムです。 SHAPは、ノーベル賞を受賞した経済学者Lloyd Shapleyによる協力ゲーム理論のフレームワークであるShapley値に基づきます。 これらの値は特徴量の有用性を表す統一指標であり、機械学習モデルからの予測を解釈するために使用されます。 SHAP値は、特徴量の有用性だけでなく、関係性の方向(正または負)も示します。
1つの特徴量のSHAP値の合計は、インスタンスの予測値と予想される予測値(データセット全体の平均)の差になります。 複数の入力特徴量に基づいて予測を行うモデルを考えた場合、各特徴量のSHAP値は、考えられるすべての特徴量の組み合わせにおけるその特徴量の平均限界寄与を表します。
プロジェクトの発端を理解するには:
Lloyd Shapleyはこう尋ねました。各メンバーがさまざまな貢献をした協力チーム間で支払いをどのように分割すべきでしょうか。
Shapley値は次のような答えを提供します。
- メンバーXのShapley値はメンバーが取得するクレジットの量です。
- 各サブチームにおいて、メンバーXがサブチームに参加すると、どれだけの限界価値が追加されるでしょうか。Shapley値は、この限界価値の加重平均です。
- 支払い総額は、メンバーのShapley値の合計です。
Scott Lundbergは、SHAP Pythonパッケージの主な作者であり、予測を説明するプログラミング手法を提供しています。
モデル予測のクレジットを特徴量で分割することが可能です!
特徴量の各値がゲームの「プレーヤー」であると想定すると、予測は支払いになります。 SHAPは、「支払い」を特徴量間で公平に配分する方法を説明します。
SHAPは、以下を開発したSHAPオープンソースパッケージのおかげで人気が高まっています。
- フリーアンサンブル法のための高速正確アルゴリズム「TreeExplainer」。
- ディープラーニングモデル用の高速近似アルゴリズム「DeepExplainer」。
- 任意のモデルでShapley値を推定する、モデルに依存しない複数のアルゴリズム("KernelExplainer"と"PermutationExplainer∕を含む)。
SHAPの以下の主要なプロパティは、DataRobot機械学習に特に適しています。
- ローカル精度:特徴量の属性の合計は、DataRobotが「説明」するモデルの出力に等しくなります。
- 欠損:すでに欠損している特徴量のインパクトはありません。
- 一貫性:特徴量のモデルへの有用性が大きくなるようにモデルを変更しても、その特徴量に割り当てられているSHAP属性が減少することはありません。 (たとえば、モデルAは特徴量Xを使用します。 次に、特徴量Xをより多く使用する新しいモデルBを作成します(おそらく、その特徴量の係数を2倍にし、他はすべては同じに保持して)。 SHAPの品質の一貫性により、モデルBの特徴量XのSHAP有用性は、少なくともモデルAの特徴量Xの場合と同じくらい高くなります。)
追加の資料を以下に示します。
SHAPは、モデルの説明可能性に次のように貢献します。
-
特徴量のインパクト:SHAPは、モデルの決定を促進している特徴量を高レベルで示します。 SHAPがない場合、結果はサンプルサイズの影響を受けやすく、サンプルが非常に大きい場合を除き、再計算すると変化する可能性があります。 See the deep dive.
-
予測の説明:すべての列の結果を生成するのに適さない特定のタイプのデータがあります。 これは、銀行や保険など規制の厳しい業界では特に問題になります。 SHAPは、特定の予測が平均と異なる予測において各特徴量の責任の割合を説明します。 たとえば、Xドルで販売されると予測された不動産レコードがある場合、SHAP予測の説明は、各特徴量がその価格にどれだけ貢献しているかを示します。
-
特徴量ごとの作用:
- ワークベンチでは、特徴量ごとの作用は常にPermutationベースの特徴量のインパクトを使用します。
- DataRobot Classicでは、SHAPは特徴量ごとの作用の結果を変更しません。 予測、実測値、および部分依存プロットは、SHAPをまったく使用しません。 ただし、左側の棒グラフは、通常のPermutationベースの特徴量のインパクトではなく、SHAPベースの特徴量のインパクトの順に並べられています。
特徴量のインパクト¶
特徴量のインパクトは、モデルによって使用される各特徴量(j
)に有用性を割り当てます。
SHAPベース¶
モデルといくつかの観測値(トレーニングデータで最大5000行)が提供された場合、各特徴量j
に対する特徴量のインパクトは次のように計算されます。
abs(shap_values for feature j)
のサンプル平均
上位の特徴量のインパクトが100%になるように値を正規化します。
Permutationベース¶
モデルといくつかの観測値(既定では2500、最大100,000)が提供された場合、実際のデータに基づいてモデルの指標を計算します。 各列j
に対して以下の処理を行います。
- 列
j
の値にPermutationを実行します。 - Permutationが行われたデータで指標を計算します。
- 有用性 =
metric_actual - metric_perm
(オプション)最大の結果値で正規化します。
予測の説明¶
SHAP予測の説明は加法性です。 SHAP値の合計は、次の値と完全に等しくなります。
[prediction - average(training_predictions)]
_DataRobot Classicのみ(WorkbenchはSHAPのみ)_XEMPとSHAPのどちらかを選択する場合は、精度と解釈可能性、パフォーマンスの必要性を考慮してください。 XEMPでは、すべてのブループリントがオートパイロットに含まれているので、結果の精度はわずかに高くなる可能性があります。 しかし、SHAPはすべての主要なブループリントをサポートするので、これはすべてのケースに当てはまるわけではなく、精度が同じであることがあります。 SHAPは、より高い解釈性とパフォーマンスを提供します。
- 結果は直感的です。
- SHAPはすべての機能に対して計算されます
- 多くの場合、結果は5〜20倍速く返されます。
- SHAPは加法性。
- オープンソースの特性は透明性を提供します
どのモデルにどの説明子が使用されますか?¶
ブループリント内では、各モデリングステップは、モデルタイプに最も最適化されたSHAP Explainerを使用します。
- ツリーベースのモデル(XGBoost、LightGBM、Random Forest、決定木):
TreeExplainer
- Kerasディープラーニングモデル:
DeepExplainer
- 線形モデル:
LinearExplainer
1つのブループリントに複数のモデリングタスクが含まれている場合、SHAP値は加算的に結合され、そのブループリント全体でのSHAP値が生成されます。
モデリングタスクの中には、タイプ固有の説明者がサポートしていないものもあります。 ブループリントにそのようなタスクが含まれている場合、モデルに依存しないPermutationExplainer
によって、そのブループリント全体が一つのユニットとして説明されます。
予測の説明における加法性¶
場合によっては、SHAP値が予測に加算されないことがあります。 理由は、次のような状態の一部または全部である可能性があります。
-
一部のモデルでは、リンク関数を使用して、モデリングタスクの直接出力を別のスケールに変換します。 これは、
logit
リンク関数を使用することが多い二値分類モデルで非常によく見られる現象であり、log
リンク関数を使用する歪んだターゲットを持つ連続値モデルでも発生します。 SHAPPermutationExplainer
が使用されている場合、SHAP値は、すでにを考慮されているリンク変換を備えた予測と同じ単位になります。基本値と合計すると、予測が再現されます。 ただし、TreeExplainer
やDeepExplainer
のようなタスク固有のexplainerが使用されている場合、SHAP値はリンク変換前のマージン単位になります。 予測を再現するには、SHAP値を基本値と合計してから、以下のように逆リンク変換を適用する必要があります。-
ほとんどの二値分類問題では、SHAP値は確率空間とは異なるスケール[0,1]に対応します。 これは、これらのアルゴリズムが(最も一般的にはロジスティック関数
prob = logistic(y)
のような非線形関数を使用して)直接出力y
を常に0と1の間の値にマッピングすることに起因します。 (専門用語では、モデルの「リンク関数」はlogit(p)
で、logistic(y)
の逆です。 モデルの直接出力(y
)は「log-odds」単位で表され、最終予測prob
は確率単位で表されます。この一般的な状況では、SHAP値はリンク前の「マージンスペース」で加法的に機能し、最終的な確率空間では機能しません。 これはsum(shap_values) = logit(prob) - logit(prob_0)
を意味します。ここでprob_0
はモデルの予測のトレーニング平均です。 -
歪んだターゲットを含む連続値問題は自然対数
log()
を同様のリンク関数として使用する場合があります。
-
-
リンクの前に適用されるオフセットや、リンクの後に適用されるエクスポージャーが設定されている可能性があります。 (See the Workbench configuration here and the Classic configuration here.) Offsets and exposures are not regular feature columns and do not get SHAP values; instead, they are applied to the model outputs as a post-processing step. SHAP値は、すべてのオフセットが0、エクスポージャーが1であるものとして計算されます。
-
モデルは、その予測を「制限」または「監査」します(それらを負以外の値に強制するなど)。
次の疑似コードは、これらのケースで加法性を検証するために使用できます。
この例では、SHAP値はマージン単位で表されます。
# shap_values = output from SHAP prediction explanations
# If you obtained the base_value from the UI prediction distribution chart, first transform it by the link.
base_value = api_shap_base_value or link_function(ui_shap_base_value)
pred = base_value + sum(shap_values)
if offset is not None:
pred += offset
if link_function == 'log':
pred = exp(pred)
elif link_function == 'logit:
pred = exp(pred) / (1 + exp(pred))
if exposure is not None:
pred *= exposure
pred = predictions_capping(pred)
# at this point, pred matches the prediction output from the model
この例では、SHAP値は予測と同じ単位で表されます。
if link_function == 'log':
pred = (base_value + sum(shap_values)) * exposure * exp(offset)
elif link_function == 'logit':
# here expit(x) = exp(x) / (1 + exp(x))
pred = expit(offset + logit(base_value + sum(shap_values)))
else:
pred = offset + base_value + sum(shap_values)
How SHAP values are calculated for time-aware¶
In time-aware experiments, SHAP explanations and impact are available when using Workbench (or via the API); calculations are based on input features, not the derived features that result from running time-aware modeling. The calculation method for this data is different than that used for predictive experiments. The sections below provide an overview.
Generally, the way SHAP works is to take one row of the input data at a time and make a prediction, shuffle values, make another prediction, and so on, making a grid of results. Every time DataRobot steps to a row, it performs permutations on it and then moves to the next. Then, it centralizes all the results and uses them to calculate the SHAP values. However, because time-aware experiments do not make row-by-row predictions, this calculation method does not work. Time-aware is not designed to handle permutations of the same date and series, nor can it have duplicate series. With time-aware, you have a flow of dates across series and dates.
To provide SHAP values for time-aware, DataRobot runs SHAP twice. First, it runs the data through to collect a set of prediction permutations SHAP might try. Then, instead of processing them individually, it stores them as parallel prediction caches. Each cache maintains chronological integrity and no single date exists more than once per cache. After predictions, results are reshuffled to present back to SHAP in the expected format. This creates the illusion for SHAP that it processed individual permutations, when in reality the methodology preserved time continuity.
For example, for a dataset with 10 features, SHAP might want to create 10 variations of each row with different feature values included or excluded. If you have 10 features, DataRobot is going to make 10 copies of a column or 10 copies of a row, each with different settings. The results are then stored as 10 parallel caches. DataRobot predicts each cache independently and then reshuffles the data—SHAP "thinks" it has created 10 unique things, predicted them, and they returned with values.
The number of permutations run by SHAP, and therefore the number of parallel caches, is number of features x 2 - 1
. These are stored in parallel so that each cache basically has each permutation (e.g., January 2nd, 3rd, etc.,) but does not have a single date existing more than once per cache. This feature of SHAP compatibility prevents duplicate time steps from colliding with one another, as that would break the time series model.
In predictive SHAP, values remain consistent regardless of row ordering—you can shuffle your data, calculate SHAP values, and then rearrange them back to their original order without affecting the results. Each row's SHAP values depend solely on its feature values. With time-aware SHAP, by contrast, values are position-dependent. Even if two data points have identical feature values (excluding the date itself), they will receive different SHAP values when positioned at different points in the time sequence. Moving data points earlier or later in the timeline changes their SHAP values because time series models inherently account for temporal relationships and patterns. This temporal dependency is an important consideration when interpreting time-aware SHAP values. The position of a data point within its chronological context directly impacts its importance and contribution to predictions.
その他の資料¶
以下の公開情報には、オープンソースのSHAPに関する追加情報が記載されます。