LightGBM
Train, tune, evaluate, and explain gradient-boosted classifiers and regressors, with a saved-model registry
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
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/lightgbm/index.md to help me query LightGBM 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 where a classifier gets its predictions right
Example output · captured
- Connector release
- v0.2.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
| actual | predicted | n |
|---|---|---|
| setosa | setosa | 50 |
| versicolor | versicolor | 46 |
| versicolor | virginica | 4 |
| virginica | versicolor | 6 |
| virginica | virginica | 44 |
Quick start
Choose where to run LightGBM, 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.
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;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uv
Needs uv. uv fetches the worker script and its package at the release tag on first attach and reuses the cached environment after that.
ATTACH 'lightgbm' AS lightgbm (
TYPE vgi,
LOCATION 'uv run --with git+https://github.com/Query-farm/[email protected] https://raw.githubusercontent.com/Query-farm/vgi-lightgbm/v0.2.0/lightgbm_worker.py'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'lightgbm' AS lightgbm (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-lightgbm:0.2.0'
);
3. Run a query
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
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;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'lightgbm' AS lightgbm
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
Query Farm operates LightGBM for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
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;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use lightgbm as the catalog alias for the query below.
4. Run a query
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
Technical details
- Verified connector release
- v0.2.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
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 uv
Needs uv. uv fetches the worker script and its package at the release tag on first attach and reuses the cached environment after that.
ATTACH 'lightgbm' AS lightgbm (
TYPE vgi,
LOCATION 'uv run --with git+https://github.com/Query-farm/[email protected] https://raw.githubusercontent.com/Query-farm/vgi-lightgbm/v0.2.0/lightgbm_worker.py'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'lightgbm' AS lightgbm (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-lightgbm:0.2.0'
);
Overview
LightGBM train/predict model registry, datasets, and interpretation for DuckDB/SQL
Exposes LightGBM to DuckDB/SQL as VGI functions:
- Datasets — toy datasets and generators
- Models —
fit/predict, typedfit_<estimator>, cross-validation, grid/randomized search - Interpretation — feature importance, SHAP
explain, partial dependence, permutation importance
Models are stored as reusable BLOBs in a registry; native text serialization.
Catalog
Everything lightgbm exposes once attached — read straight from the worker's
released build.
main 6 tables · 16 functions
LightGBM train/predict model registry, datasets, and interpretation for SQL
Schema examples (4)
Fit a LightGBM classifier on iris and return the training summary
SELECT estimator, task, n_samples, n_features, train_score
FROM lightgbm.main.fit((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id');
Out-of-fold confusion matrix on iris with species names: 5-fold cross_val_predict joined back to iris for the true and the predicted species (setosa is never confused; versicolor and virginica are mixed up a handful of times)
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
| actual | predicted | n |
|---|---|---|
| setosa | setosa | 50 |
| versicolor | versicolor | 46 |
| versicolor | virginica | 4 |
| virginica | versicolor | 6 |
| virginica | virginica | 44 |
Do split-gain and permutation importance agree? Join the stored 'iris_clf' model's gain ranking to its permutation (accuracy-drop) ranking per feature; both put the two petal measurements first
SELECT f.feature, f.rank AS gain_rank, p.rank AS permutation_rank,
round(f.importance, 1) AS gain,
round(p.importance_mean, 4) AS accuracy_drop
FROM lightgbm.main.feature_importance('iris_clf', importance_type := 'gain') f
JOIN lightgbm.main.permutation_importance((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', target := 'target', random_state := 42) p USING (feature)
ORDER BY f.rank;
Which features push each flower toward its own species? Join SHAP contributions from the stored 'iris_clf' to iris on sample and true class, then average |SHAP| per species and feature (petal length leads for all three species)
SELECT i.target_name, e.feature,
round(avg(abs(e.shap_value)), 3) AS mean_abs_shap
FROM lightgbm.main.explain((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', id := 'sample_id') e
JOIN lightgbm.main.iris() i ON i.sample_id = e.sample_id AND i.target = e.class
GROUP BY i.target_name, e.feature
ORDER BY i.target_name, mean_abs_shap DESC;
breast_cancer
569 tumour samples with 30 cell-nucleus measurements and a benign/malignant label.
Class balance
SELECT target_name, count(*)
FROM lightgbm.main.breast_cancer
GROUP BY target_name;
SELECT target_name, count(*) AS n,
round(avg(mean_radius), 2) AS avg_mean_radius
FROM lightgbm.main.breast_cancer()
GROUP BY target_name
ORDER BY target_name;
california_housing
20640 census block groups with 8 features and the median house value (regression).
Average median house value by latitude band
SELECT round(latitude) AS lat,
round(avg(median_house_value), 2) AS avg_value
FROM lightgbm.main.california_housing
GROUP BY lat
ORDER BY lat;
SELECT round(latitude) AS lat, count(*) AS block_groups,
round(avg(median_house_value), 3) AS avg_value
FROM lightgbm.main.california_housing()
GROUP BY lat
ORDER BY lat;
diabetes
442 patients with 10 baseline measurements and a one-year disease-progression score.
Range and mean of the one-year progression score
SELECT min(progression) AS lo, max(progression) AS hi,
round(avg(progression), 1) AS mean
FROM lightgbm.main.diabetes;
SELECT round(min(progression), 1) AS lo, round(max(progression), 1) AS hi,
round(avg(progression), 1) AS mean
FROM lightgbm.main.diabetes();
iris
150 iris flowers with four sepal/petal measurements (cm) and their species.
Row count per species
SELECT target_name, count(*) FROM lightgbm.main.iris GROUP BY target_name;
SELECT target_name, count(*) AS n,
round(avg(petal_length_cm), 2) AS avg_petal_length_cm
FROM lightgbm.main.iris()
GROUP BY target_name
ORDER BY target_name;
SELECT target_name, avg(petal_length_cm)
FROM lightgbm.main.iris()
GROUP BY target_name;
models
One row per model persisted in the registry, with its estimator, task, shape, and score.
How many models are saved
SELECT count(*) AS model_count FROM lightgbm.main.models;
SELECT count(*) AS n_models, count(DISTINCT estimator) AS n_estimators
FROM lightgbm.main.models();
wine
178 wines with 13 chemical measurements and their cultivar of origin.
Average alcohol per cultivar
SELECT target_name, round(avg(alcohol), 2)
FROM lightgbm.main.wine
GROUP BY target_name;
SELECT sample_id, alcohol, flavanoids, target_name
FROM lightgbm.main.wine()
WHERE alcohol > 13.5
ORDER BY alcohol DESC
LIMIT 10;
drop_model (model_name VARCHAR)
Delete a model from the registry
Delete a model by name; `dropped` is false when the name did not exist
SELECT model_name, dropped FROM lightgbm.main.drop_model('no_such_model');
feature_importance (model_name VARCHAR, importance_type := VARCHAR, model := BLOB)
Per-feature importance (split or gain) for a model, ranked
Gain-based feature importance for a stored model
SELECT feature, round(importance, 3) AS importance
FROM lightgbm.main.feature_importance('iris_clf', importance_type := 'gain')
WHERE rank <= 5
ORDER BY rank;
Do split-gain and permutation importance agree? Join the stored 'iris_clf' model's gain ranking to its permutation (accuracy-drop) ranking per feature; both put the two petal measurements first
SELECT f.feature, f.rank AS gain_rank, p.rank AS permutation_rank,
round(f.importance, 1) AS gain,
round(p.importance_mean, 4) AS accuracy_drop
FROM lightgbm.main.feature_importance('iris_clf', importance_type := 'gain') f
JOIN lightgbm.main.permutation_importance((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', target := 'target', random_state := 42) p USING (feature)
ORDER BY f.rank;
make_classification (n_classes := BIGINT, n_features := BIGINT, n_informative := BIGINT, n_samples := BIGINT, random_state := BIGINT)
Generate a synthetic classification dataset
Class balance and mean first feature of a 500-row, 3-feature, 3-class sample
SELECT target, count(*) AS n, round(avg(feature_0), 3) AS avg_feature_0
FROM lightgbm.main.make_classification(n_samples := 500, n_features := 3, n_classes := 3)
GROUP BY target
ORDER BY target;
make_regression (n_features := BIGINT, n_informative := BIGINT, n_samples := BIGINT, noise := DOUBLE, random_state := BIGINT)
Generate a synthetic regression dataset
Mean and spread of a noisy 500-row, 3-feature regression target
SELECT round(avg(target), 3) AS mean_target,
round(stddev(target), 3) AS sd_target
FROM lightgbm.main.make_regression(n_samples := 500, n_features := 3, noise := 5.0);
model_info (model_name VARCHAR)
Describe a single stored model (one row, empty if absent)
Check whether a model named 'iris_clf' is in the registry
SELECT count(*) AS found FROM lightgbm.main.model_info('iris_clf');
cross_val_predict (data TABLE, cv := BIGINT, estimator := VARCHAR, id := VARCHAR, params := VARCHAR, target := VARCHAR)
Out-of-fold cross-validated predictions (no model is stored)
Out-of-fold confusion matrix on iris with species names: 5-fold cross_val_predict joined back to iris for the true and the predicted species (setosa is never confused; versicolor and virginica are mixed up a handful of times)
SELECT a.target_name AS actual, l.target_name AS predicted, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id', cv := 5) p
JOIN lightgbm.main.iris() a USING (sample_id)
JOIN (SELECT DISTINCT target, target_name FROM lightgbm.main.iris()) l ON l.target = p.prediction
GROUP BY actual, predicted
ORDER BY actual, predicted;
| actual | predicted | n |
|---|---|---|
| setosa | setosa | 50 |
| versicolor | versicolor | 46 |
| versicolor | virginica | 4 |
| virginica | versicolor | 6 |
| virginica | virginica | 44 |
Distribution of the 5-fold out-of-fold predictions on iris
SELECT prediction, count(*) AS n
FROM lightgbm.main.cross_val_predict((SELECT sample_id, sepal_length_cm, sepal_width_cm, petal_length_cm, petal_width_cm, target FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target')
GROUP BY prediction
ORDER BY prediction;
cross_val_score (data TABLE, cv := BIGINT, estimator := VARCHAR, id := VARCHAR, params := VARCHAR, target := VARCHAR)
Per-fold cross-validation scores (accuracy or R^2); no model is stored
5-fold accuracy scores on iris
SELECT fold, score
FROM lightgbm.main.cross_val_score((SELECT sample_id, sepal_length_cm, sepal_width_cm, petal_length_cm, petal_width_cm, target FROM lightgbm.main.iris()), estimator := 'lgbm_classifier', target := 'target', cv := 5);
explain (data TABLE, id := VARCHAR, model := BLOB, model_name := VARCHAR)
Per-row SHAP feature contributions, long format (row, [class], feature, shap_value, base_value)
Rank features by mean absolute SHAP contribution for a stored regressor 'diab_reg'
SELECT feature, round(avg(abs(shap_value)), 4) AS mean_abs_shap
FROM lightgbm.main.explain((SELECT * FROM lightgbm.main.diabetes()), model_name := 'diab_reg', id := 'sample_id')
GROUP BY feature
ORDER BY mean_abs_shap DESC;
Which features push each flower toward its own species? Join SHAP contributions from the stored 'iris_clf' to iris on sample and true class, then average |SHAP| per species and feature (petal length leads for all three species)
SELECT i.target_name, e.feature,
round(avg(abs(e.shap_value)), 3) AS mean_abs_shap
FROM lightgbm.main.explain((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', id := 'sample_id') e
JOIN lightgbm.main.iris() i ON i.sample_id = e.sample_id AND i.target = e.class
GROUP BY i.target_name, e.feature
ORDER BY i.target_name, mean_abs_shap DESC;
fit (data TABLE, estimator := VARCHAR, id := VARCHAR, model_name := VARCHAR, params := VARCHAR, target := VARCHAR)
Fit a LightGBM estimator; returns the model as a BLOB and stores it when model_name is given
Train a LightGBM classifier on iris and store it as 'iris_clf'
SELECT model_name, task, n_samples
FROM lightgbm.main.fit((SELECT sample_id, sepal_length_cm, sepal_width_cm, petal_length_cm, petal_width_cm, target FROM lightgbm.main.iris()), model_name := 'iris_clf', estimator := 'lgbm_classifier', target := 'target', id := 'sample_id');
fit_lgbm_classifier (data TABLE, boosting_type := VARCHAR, colsample_bytree := DOUBLE, id := VARCHAR, learning_rate := DOUBLE, max_depth := BIGINT, min_child_samples := BIGINT, model_name := VARCHAR, n_estimators := BIGINT, num_leaves := BIGINT, objective := VARCHAR, random_state := BIGINT, reg_alpha := DOUBLE, reg_lambda := DOUBLE, subsample := DOUBLE, target := VARCHAR)
Fit a lgbm_classifier with typed LightGBM hyperparameters; returns/stores the model
Train a lgbm_classifier with named hyperparameters
SELECT model_name, task
FROM lightgbm.main.fit_lgbm_classifier((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'm', target := 'target', id := 'sample_id', n_estimators := 100, num_leaves := 31);
fit_lgbm_regressor (data TABLE, boosting_type := VARCHAR, colsample_bytree := DOUBLE, id := VARCHAR, learning_rate := DOUBLE, max_depth := BIGINT, min_child_samples := BIGINT, model_name := VARCHAR, n_estimators := BIGINT, num_leaves := BIGINT, objective := VARCHAR, random_state := BIGINT, reg_alpha := DOUBLE, reg_lambda := DOUBLE, subsample := DOUBLE, target := VARCHAR)
Fit a lgbm_regressor with typed LightGBM hyperparameters; returns/stores the model
Train a lgbm_regressor with named hyperparameters
SELECT model_name, task
FROM lightgbm.main.fit_lgbm_regressor((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'm', target := 'target', id := 'sample_id', n_estimators := 100, num_leaves := 31);
grid_search (data TABLE, boosting_type := VARCHAR[], colsample_bytree := DOUBLE[], cv := BIGINT, estimator := VARCHAR, id := VARCHAR, learning_rate := DOUBLE[], max_depth := BIGINT[], min_child_samples := BIGINT[], n_estimators := BIGINT[], num_leaves := BIGINT[], objective := VARCHAR[], random_state := BIGINT[], reg_alpha := DOUBLE[], reg_lambda := DOUBLE[], subsample := DOUBLE[], target := VARCHAR)
Cross-validated grid search; returns the leaderboard + refit best model BLOB on the best row
Grid-search num_leaves x learning_rate on iris
SELECT params, mean_score, rank
FROM lightgbm.main.grid_search((SELECT sample_id, sepal_length_cm, sepal_width_cm, petal_length_cm, petal_width_cm, target FROM lightgbm.main.iris()), target := 'target', id := 'sample_id', cv := 2, estimator := 'lgbm_classifier', num_leaves := [15, 31], learning_rate := [0.1])
ORDER BY rank;
partial_dependence (data TABLE, feature := VARCHAR, grid_resolution := BIGINT, model := BLOB, model_name := VARCHAR)
How a stored model's average prediction changes as one feature varies over a grid
Partial dependence of 'iris_clf' on petal length
SELECT feature_value, round(partial_dependence, 4) AS pd
FROM lightgbm.main.partial_dependence((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', feature := 'petal_length_cm', grid_resolution := 5)
WHERE class = 0
ORDER BY feature_value;
permutation_importance (data TABLE, model := BLOB, model_name := VARCHAR, n_repeats := BIGINT, random_state := BIGINT, scoring := VARCHAR, target := VARCHAR)
Model-agnostic feature importance: the drop in score when each feature is shuffled, ranked
Rank iris features by permutation importance for a stored model
SELECT feature, round(importance_mean, 4) AS importance_mean
FROM lightgbm.main.permutation_importance((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', target := 'target', n_repeats := 1)
WHERE rank <= 3
ORDER BY rank;
predict (data TABLE, id := VARCHAR, model := BLOB, model_name := VARCHAR, output_margin := BOOLEAN, pred_leaf := BOOLEAN, with_proba := BOOLEAN)
Score a table through a stored model (model_name) or an inline model BLOB
Predict with the stored 'iris_clf' model
SELECT sample_id, prediction
FROM lightgbm.main.predict((SELECT * FROM lightgbm.main.iris()), model_name := 'iris_clf', id := 'sample_id')
ORDER BY sample_id
LIMIT 10;
randomized_search (data TABLE, boosting_type := VARCHAR[], colsample_bytree := DOUBLE[], cv := BIGINT, estimator := VARCHAR, id := VARCHAR, learning_rate := DOUBLE[], max_depth := BIGINT[], min_child_samples := BIGINT[], n_estimators := BIGINT[], n_iter := BIGINT, num_leaves := BIGINT[], objective := VARCHAR[], random_state := BIGINT[], reg_alpha := DOUBLE[], reg_lambda := DOUBLE[], search_seed := BIGINT, subsample := DOUBLE[], target := VARCHAR)
Cross-validated randomized search: sample n_iter hyperparameter combinations
Randomized-search num_leaves x learning_rate on iris
SELECT params, mean_score, rank
FROM lightgbm.main.randomized_search((SELECT sample_id, sepal_length_cm, sepal_width_cm, petal_length_cm, petal_width_cm, target FROM lightgbm.main.iris()), target := 'target', id := 'sample_id', n_iter := 2, cv := 2, estimator := 'lgbm_classifier', num_leaves := [15, 31, 63], learning_rate := [0.05, 0.1, 0.2])
ORDER BY rank;
Try these queries
Fit a LightGBM classifier on iris, then predict with the fitted model.
SET VARIABLE iris_model = (SELECT model FROM lightgbm.main.fit((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris), estimator := 'lgbm_classifier', target := 'target', id := 'sample_id'));
SELECT sample_id, prediction FROM lightgbm.main.predict((SELECT * FROM lightgbm.main.iris), model := getvariable('iris_model'), id := 'sample_id') LIMIT 5;
Three-fold cross-validated accuracy of a LightGBM classifier on iris.
SELECT fold, score
FROM lightgbm.main.cross_val_score((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris), estimator := 'lgbm_classifier', target := 'target', cv := 3);
Uses cross_val_scoreiris
Grid-search a LightGBM classifier on iris over a tiny grid (3-fold).
SELECT params, mean_score, rank
FROM lightgbm.main.grid_search((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris), target := 'target', cv := 3, estimator := 'lgbm_classifier', num_leaves := [15, 31], learning_rate := [0.1])
ORDER BY rank;
Uses grid_searchiris
Do split-gain and permutation importance agree? Join the stored 'iris_clf' model's gain ranking to its permutation (accuracy-drop) ranking per feature; both put the two petal measurements first
SELECT f.feature, f.rank AS gain_rank, p.rank AS permutation_rank,
round(f.importance, 1) AS gain,
round(p.importance_mean, 4) AS accuracy_drop
FROM lightgbm.main.feature_importance('iris_clf', importance_type := 'gain') f
JOIN lightgbm.main.permutation_importance((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', target := 'target', random_state := 42) p USING (feature)
ORDER BY f.rank
Which features push each flower toward its own species? Join SHAP contributions from the stored 'iris_clf' to iris on sample and true class, then average |SHAP| per species and feature (petal length leads for all three species)
SELECT i.target_name, e.feature,
round(avg(abs(e.shap_value)), 3) AS mean_abs_shap
FROM lightgbm.main.explain((SELECT * EXCLUDE (target_name) FROM lightgbm.main.iris()), model_name := 'iris_clf', id := 'sample_id') e
JOIN lightgbm.main.iris() i ON i.sample_id = e.sample_id AND i.target = e.class
GROUP BY i.target_name, e.feature
ORDER BY i.target_name, mean_abs_shap DESC
Uses explain
30 more examples in the Catalog, each listed under the table or function it uses.
LightGBM 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.
Related workers
XGBoost
Statistics & ML
Train, tune, score and explain gradient-boosted tree models on your tables with plain SQL
scikit-learn
Statistics & ML
scikit-learn — datasets, metrics, transforms and a train/predict model registry
Explain
Statistics & ML
Explain scikit-learn and XGBoost model predictions with per-row SHAP values and global feature importance