shap
できること
SHAPは、AIモデルがどのように判断しているかを理解するためのツールです。最終的な予測に、入力情報のどの部分が特に重要だったのかを示します。
使う場面
- ✓モデルがなぜ特定の予測をしたのかを知りたいとき。
- ✓予測に影響する複数の要因の重要度を比べたいとき。
- ✓モデルが正しい情報を使っているかを確認したいとき。
スキルのリンク
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/shap/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“このお客さんが商品Xを購入すると予測した理由を説明してください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
SHAPは、平均的な値と比較しながら、各特徴量(入力情報)が予測に与えた影響を計算します。それぞれの特徴量にスコアを割り当て、予測の確率を上げたのか下げたのかを示します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
SHAP
Use SHAP to describe how a fitted predictive model maps inputs to outputs. Work from the modern shap.Explanation API, make the explained output and background distribution explicit, and validate every explanation before interpreting it.
This skill is aligned with SHAP 0.52.0 (released 2026-05-28). That release requires Python 3.12 or newer.
The maintained examples were checked on Python 3.12.10 with SHAP 0.52.0, NumPy 2.5.3, pandas 3.0.6, scikit-learn 1.9.1, and matplotlib 3.11.2. Native tests cover small tree, exact, permutation, partition, linear, additive, kernel, text, and constant-image games; additional numeric XGBoost 3.4.1 smoke checks cover raw, probability, loss, and interaction outputs. Reference snippets that require a project model, framework, or data are adaptation templates; optional pretrained/deep, GPU, and distributed integrations remain illustrative and require their own runtime validation.
Operating Rules
- Explain a fixed, evaluated model; do not use SHAP as a substitute for predictive validation.
- Use held-out or clearly labeled analysis rows for explanations. Choose background rows only from an appropriate training or reference population.
- State the explained output: regression value, raw margin, probability, log loss, logit, or another model method.
- Prefer
shap.Explanationobjects andexplainer(X). Some specialized options still require.shap_values(X), including deep ranked outputs, gradient sampling budgets, and Kernel SHAPnsamples. Preserve their output indexes and baselines explicitly. - For multi-output models, select one output before using tabular plots:
explanation[..., output_index]. - Check
base_values + values.sum(...)against the exact model output being explained. - Treat SHAP as a description of model behavior under a masking/background choice. It does not establish causality, fairness, recourse, or scientific mechanism.
- Never silence an additivity failure until input shape, preprocessing, model version, output space, and row ordering have been checked.
- Do not load untrusted pickle, joblib, model, or explainer artifacts; those formats can execute code during deserialization.
Install
Create an isolated environment and pin the documented release:
uv venv --python 3.12
source .venv/bin/activate
uv pip install "shap[plots]==0.52.0"
shap[plots] installs the plotting dependencies. Add the fitted model's package at a version compatible with the project. For older Python compatibility, read references/migration.md instead of silently installing a different SHAP release.
Confirm the environment before debugging an API mismatch:
import platform
import shap
print("Python:", platform.python_version())
print("SHAP:", shap.__version__)
Standard Workflow
1. Define the explanation target
Record:
- model and preprocessing version;
- exact callable or model method being explained;
- output name/index and units;
- evaluation rows;
- background/reference population;
- masker and explainer algorithm;
- SHAP and model-library versions.
For classifiers, decide whether the task needs raw margins or probabilities. Defaults differ by model family; never infer units from the plot color or sign.
2. Select an explainer and masker
Start with shap.Explainer(model, masker) when automatic dispatch is sufficient. Instantiate a specialized explainer when its assumptions or output controls matter.
| Situation | Preferred choice | Important constraint |
|---|---|---|
| Supported tree ensemble | TreeExplainer |
model_output="probability" and "log_loss" require interventional masking and background data |
| Linear model | LinearExplainer |
The masker determines interventional versus correlation-aware behavior |
| Small feature space | ExactExplainer |
Cost grows quickly with unconstrained feature count |
| General tabular callable | PermutationExplainer |
Budget at least one full forward/reverse permutation |
| Hierarchical feature groups, text, or image | PartitionExplainer |
The partition tree changes the cooperative game |
| Differentiable neural network | DeepExplainer or GradientExplainer |
Framework support, output shape, and background choice require testing |
| Legacy Kernel SHAP workflow | KernelExplainer |
Usually much slower than model-specific methods |
…
よくある質問
SHAPは、モデルが正しいことを証明してくれますか?
いいえ。SHAPはモデルの振る舞いを説明するものですが、予測そのものが正しいかどうかを検証したり、因果関係を証明したりするものではありません。
SHAPはどんなAIモデルでも使えますか?
多くの種類のモデル、特に機械学習モデルで使えます。ただし、設定方法はモデルによって異なる場合があります。
「explainer」と「masker」とは何ですか?
「explainer」は重要度を計算するための手法のことです。「masker」は、比較の基準となる「背景」や「参照データ」を定義する役割を持ちます。
詳細
作者による元の説明
Explain and audit machine-learning predictions with SHAP. Use for selecting SHAP explainers and maskers, computing and validating feature attributions, handling multi-output explanations, and producing local or global SHAP visualizations.
人気
- GitHubのスター
- 51k
- ランキング
- #3573
- ライセンス
- MIT
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
13c-metabolic-flux
51k炭素13同位体トレーシングのデータを使って代謝フラックスを推定します。細胞の代謝や生化学経路の分析に役立ちます。
adaptyv
51kAdaptyv APIを使ってタンパク質の特性評価実験を設計します。コストの見積もり、配列の提出、ラボの結果の監視を支援します。
aeon
51k時系列データに対して機械学習のタスクを実行します。逐次データの分類、予測、異常検知、パターンの発見に役立ちます。
alphagenome
51k遺伝的変異に対して、事前に計算されたゲノム上の効果を検索します。スコアと説明を提供し、変異がゲノムに与える影響を理解するのに役立ちます。