Skip to main content

Convert tree-based ML models to SQL queries

Project description

sqlgbm

⚠️ Warning: This library is in a very early development stage. The API and functionality will improve significantly over time. Not ready for production use yet.

sqlgbm is a Python library that converts tree-based machine learning models into SQL queries. This allows you to deploy your ML models directly in your database without any additional infrastructure.

sqlgbm in action

installation

pip install sqlgbm

overview

sqlgbm takes your trained tree-based models and generates SQL code that reproduces the model's predictions. This enables you to:

  • Run predictions directly in your database
  • Eliminate latency from API calls between your database and ML serving infrastructure
  • Simplify your production architecture by removing additional serving components

supported models

Currently supported models:

  • LightGBM
  • XGBoost

usage

basic example

from sqlgbm import SQLGBM
import lightgbm as lgb
import pandas as pd

# Load titanic dataset
titanic = pd.read_csv('titanic.csv')
features = ['pclass', 'sex', 'age', 'fare']
X = titanic[features]
X['sex'] = X['sex'].astype('category')
y = titanic['survived']

# Train model
clf = lgb.LGBMClassifier(n_estimators=3, max_depth=3)
clf.fit(X, y, categorical_feature=['sex'])

# Convert to SQL
sqlgbm = SQLGBM(clf, cat_features=['sex'])
sql = sqlgbm.generate_query('titanic', 'probability')

print(sql)

xgboost example

import xgboost as xgb
from sqlgbm import SQLGBM

# Prepare data and train model
# ...

# Convert XGBoost model to SQL
model = xgb.XGBClassifier(n_estimators=3, max_depth=3, base_score=0.5)
model.fit(X, y)

sqlgbm = SQLGBM(model, X=X)  # X used to infer categorical features
sql = sqlgbm.generate_query('my_table', 'all')

output types

sqlgbm supports different output formats through the output_type parameter:

  • raw: Returns the raw model output
  • probability: Returns the probability (after sigmoid transformation)
  • prediction: Returns the binary prediction (0 or 1) based on a 0.5 threshold
  • all: Returns all three outputs

Additional options:

  • fast_sigmoid: Use a faster approximation of the sigmoid function

roadmap

  • Add support for CatBoost
  • Optimize SQL generation for large models
  • Add support for multiclass classification
  • Provide specialized optimizations for different database engines

license

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlgbm-0.2.0.tar.gz (621.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlgbm-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlgbm-0.2.0.tar.gz.

File metadata

  • Download URL: sqlgbm-0.2.0.tar.gz
  • Upload date:
  • Size: 621.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sqlgbm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4658dc44a8b38d5611539ecebda80876f69791ad9ba512112297256a92a2e468
MD5 f4e3cab2fa87e6eecd9d0f370ae76a32
BLAKE2b-256 308c69eecf2283a678cbb0810da753cc9b20c49c0b0e5b5b28fc50b7c0823675

See more details on using hashes here.

File details

Details for the file sqlgbm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sqlgbm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sqlgbm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8aafb3b74e7becf09fae7a841db3a1db43c29ebdf86d7b6a0cf50322c8f4dbb
MD5 d470536a80c45bb7170c499bb9ddfbdc
BLAKE2b-256 3ebd6cba4909657b5018fddf7bfa076803851d72a27b4fb787c6186bfd00defe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page