Skip to content

Explain

Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/explain/index.md to help me query Explain with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

See which features drive a model’s predictions

Example output · captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
Result
feature rank method
signal 1 mean_abs_shap
noise_b 2 mean_abs_shap
noise_a 3 mean_abs_shap

Quick start

Choose where to run Explain, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'explain' AS explain (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-explain'
);

3. Run a query

query.sql
SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'explain' AS explain (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-explain'
);

Overview

SHAP explanations for scikit-learn / XGBoost models as SQL functions.

Catalog

Everything explain exposes once attached — read straight from the worker's released build.

explain 1 schema
main 1 view · 3 functions

SHAP explanations for scikit-learn / XGBoost models as SQL functions

Views
supported_models 3 examples5 cols

Model families this worker can explain and the SHAP explainer chosen for each.

Columns
model_family VARCHAR
Family of fitted estimator (tree ensemble, linear, or fallback).
estimator_examples VARCHAR
Example scikit-learn / XGBoost estimator classes in this family.
shap_explainer VARCHAR
The SHAP explainer this worker selects for the family.
exactness VARCHAR
Whether the explanation is `exact` or an `approximate` (sampled) estimate.
notes VARCHAR
How the explainer works and any caveats for the family.
Examples (3)

List every supported model family and the SHAP explainer chosen for it.

SELECT model_family, shap_explainer, exactness
FROM explain.main.supported_models
ORDER BY model_family;

Find which families get exact (rather than approximate) SHAP explanations.

SELECT model_family, shap_explainer
FROM explain.main.supported_models
WHERE exactness = 'exact'
ORDER BY model_family;

Count the supported model families grouped by their SHAP explainer.

SELECT shap_explainer, count(*) AS family_count
FROM explain.main.supported_models
GROUP BY shap_explainer
ORDER BY shap_explainer;
Link
Table functions
shap_base_value (model := BLOB) 2 examples

The SHAP base (expected) value of a model -- one row, or one row per class

Named arguments
model := BLOB
The fitted model to explain, as packed by the vgi-sklearn / vgi-xgboost workers; typically held in a session VARIABLE and passed as model := getvariable('m'). · default "b''"
Returns columns
class VARCHAR
Class label for the base value; NULL for single-output models (regression, single-margin binary classifiers) — one row.
base_value DOUBLE
The explainer expected value — the anchor that a row's SHAP contributions add onto to reach its prediction.
Examples (2)

Read the model's expected (base) output — one row per class ('0' and '1') for this binary RandomForest classifier.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT class, base_value
FROM explain.main.shap_base_value(model := getvariable('m'))
ORDER BY class;

Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba
FROM contrib AS c
JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class
WHERE c.class = '1'
ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id
LIMIT 6;
Link
Table in-out functions
feature_importance (data TABLE, id := VARCHAR, model := BLOB) 3 examples

Global feature importance: mean(|SHAP|) over the relation (or native importances if empty)

Positional arguments
data TABLE
Feature relation to average SHAP magnitudes over.
Named arguments
id := VARCHAR
Optional id column to exclude from features. · default ""
model := BLOB
The fitted model to explain, as packed by the vgi-sklearn / vgi-xgboost workers; typically held in a session VARIABLE and passed as model := getvariable('m'). · default "b''"
Returns columns
feature VARCHAR
Feature (column) name.
importance DOUBLE
Global importance: mean(|SHAP|) over the passed rows, or the model's native importance when no rows are given.
rank INTEGER
1-based rank by importance (1 = most important).
method VARCHAR
How importance was computed: 'mean_abs_shap' or 'native'.
Examples (3)

Rank features by their average SHAP magnitude across the dataset (model in session VARIABLE m).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT feature, rank, method
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY rank;
Result
feature rank method
signal 1 mean_abs_shap
noise_b 2 mean_abs_shap
noise_a 3 mean_abs_shap

Compare each feature's global importance with the number of rows in which it is the dominant (largest |SHAP|) driver of the class-1 prediction, joining feature_importance to per-row shap_values.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH dominant AS (SELECT id, arg_max(feature, abs(shap_value)) AS feature FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') WHERE class = '1' GROUP BY id) SELECT g.feature, g.rank, round(g.importance, 4) AS importance, count(d.id) AS rows_dominated
FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g
LEFT JOIN dominant AS d ON d.feature = g.feature
GROUP BY g.feature, g.rank, g.importance
ORDER BY g.rank;

Drill into the globally top-ranked feature: the rows where its SHAP contribution pushes hardest toward class 1, shown next to the feature's raw input value.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT s.id, s.feature, round(f.value, 4) AS feature_value, round(s.shap_value, 4) AS shap_value
FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS s
JOIN explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g ON g.feature = s.feature AND g.rank = 1
JOIN (UNPIVOT (SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')) ON signal, noise_a, noise_b INTO NAME feature VALUE value) AS f ON f.id = s.id AND f.feature = s.feature
WHERE s.class = '1'
ORDER BY s.shap_value DESC, s.id
LIMIT 5;
Link
shap_values (data TABLE, id := VARCHAR, model := BLOB) 2 examples

Per-row, per-feature SHAP contributions in long format (one row per row x feature [x class])

Positional arguments
data TABLE
Feature relation (must contain the model's feature columns).
Named arguments
id := VARCHAR
Optional id column to carry through (excluded from features). · default ""
model := BLOB
The fitted model to explain, as packed by the vgi-sklearn / vgi-xgboost workers; typically held in a session VARIABLE and passed as model := getvariable('m'). · default "b''"
Returns columns
id BIGINT
The value of the id column carried through (renamed to the `id` argument), or the 0-based input row index when no `id` is given. Mirrors the input id column's type (BIGINT for the documented examples).
feature VARCHAR
Feature (column) name the contribution is for.
class VARCHAR
Class label when the explainer reports one output per class (multi-class, or binary RandomForest / ExtraTrees / DecisionTree); NULL for single-output models (regression; binary XGBoost / GradientBoosting / logistic).
shap_value DOUBLE
Signed SHAP contribution of the feature to the model output.
Examples (2)

Explain each row's prediction feature by feature (model held in session VARIABLE m).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); SELECT id, feature, class, shap_value
FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id')
ORDER BY id, feature, class
LIMIT 6;

Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf'); WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba
FROM contrib AS c
JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class
WHERE c.class = '1'
ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id
LIMIT 6;
Link

Try these queries

Read the model's expected (base) output — one row per class ('0' and '1') for this binary RandomForest classifier.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT class, base_value FROM explain.main.shap_base_value(model := getvariable('m')) ORDER BY class;

Uses shap_base_value

Explain each row's prediction feature by feature (model held in session VARIABLE m).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT id, feature, class, shap_value FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') ORDER BY id, feature, class LIMIT 6;

Uses shap_values

Reconstruct each row's predicted class-1 probability as the SHAP base value plus the sum of the row's per-feature contributions (SHAP additivity), joining shap_values to shap_base_value on class, and list the rows the model is least sure about (probability closest to 0.5).

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
WITH contrib AS (SELECT id, class, sum(shap_value) AS total_shap FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') GROUP BY id, class) SELECT c.id, round(b.base_value, 4) AS base_value, round(c.total_shap, 4) AS total_shap, round(b.base_value + c.total_shap, 4) AS predicted_proba FROM contrib AS c JOIN explain.main.shap_base_value(model := getvariable('m')) AS b ON c.class IS NOT DISTINCT FROM b.class WHERE c.class = '1' ORDER BY round(abs(b.base_value + c.total_shap - 0.5), 4), c.id LIMIT 6;

Uses shap_valuesshap_base_value

Compare each feature's global importance with the number of rows in which it is the dominant (largest |SHAP|) driver of the class-1 prediction, joining feature_importance to per-row shap_values.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
WITH dominant AS (SELECT id, arg_max(feature, abs(shap_value)) AS feature FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') WHERE class = '1' GROUP BY id) SELECT g.feature, g.rank, round(g.importance, 4) AS importance, count(d.id) AS rows_dominated FROM explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g LEFT JOIN dominant AS d ON d.feature = g.feature GROUP BY g.feature, g.rank, g.importance ORDER BY g.rank;

Uses shap_valuesfeature_importance

Drill into the globally top-ranked feature: the rows where its SHAP contribution pushes hardest toward class 1, shown next to the feature's raw input value.

SET VARIABLE m = (SELECT model FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/models.parquet') WHERE name = 'rf_clf');
SELECT s.id, s.feature, round(f.value, 4) AS feature_value, round(s.shap_value, 4) AS shap_value FROM explain.main.shap_values((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS s JOIN explain.main.feature_importance((SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')), model := getvariable('m'), id := 'id') AS g ON g.feature = s.feature AND g.rank = 1 JOIN (UNPIVOT (SELECT id, signal, noise_a, noise_b FROM read_parquet('https://raw.githubusercontent.com/Query-farm/vgi-explain/v0.2.0/test/fixtures/features.parquet')) ON signal, noise_a, noise_b INTO NAME feature VALUE value) AS f ON f.id = s.id AND f.feature = s.feature WHERE s.class = '1' ORDER BY s.shap_value DESC, s.id LIMIT 5;

Uses shap_valuesfeature_importance

List every supported model family and the SHAP explainer chosen for it.

SELECT model_family, shap_explainer, exactness
FROM explain.main.supported_models
ORDER BY model_family

Uses supported_models

7 more examples in the Catalog, each listed under the table or function it uses.

Explain is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.