Skip to main content

Utility package for compiling scikit-learn classifiers for faster single-sample inference

Project description

Sklearn-Freezer

High-performance scikit-learn classifier compilation for ultra-fast inference.

Overview

Compiles scikit-learn predict_proba methods into optimized implementations for real-time applications.

Support: RandomForestClassifier and DecisionTreeClassifier binary classification only.

Installation

pip install sklearn-freezer[all]  # Includes all backends

Quick Start

import sklearn_freezer as skf
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import numpy as np

# Train model
X, y = make_classification(n_samples=1000, n_features=4, random_state=42, n_classes=2)
clf = RandomForestClassifier(random_state=42).fit(X, y)

# Compile for single fast inference
fast_predict = skf.compile(clf.predict_proba, backend="c")

# Fast prediction
probability = fast_predict(*X[0])

Backends

  • python: Pure Python (baseline)
  • cython: Cython-compiled (faster)
  • c: Native C extension (fastest)

Batch Processing

# Compile for fast batch inference (cython/c only)
batch_predict = skf.compile(clf.predict_proba, backend="c", batch_mode="numpy")

# Example batch input
X_batch = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=np.float64)

# Get probabilities using the compiled implementation
probabilities = batch_predict(X_batch)

# Get probabilities using the original scikit-learn method
# Both results should be identical, but the compiled implementation is faster
probabilities = clf.predict_proba(X_batch)[:, 1]

Module Caching

# Save compiled module for reuse
fast_predict = skf.compile(clf.predict_proba, backend="c", module_name="my_model")
# Subsequent calls with same module_name will reuse if source unchanged

Examples

python example/basic_compiler_test.py      # Basic compilation test
python example/sklearn_model_benchmark.py  # Model performance benchmark

Requirements

  • Python 3.10+
  • scikit-learn
  • cython (for Cython backend)
  • setuptools (for C backend)
  • a C compiler (for C backends)

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

sklearn_freezer-0.2.0.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

sklearn_freezer-0.2.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sklearn_freezer-0.2.0.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for sklearn_freezer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 63b4342711e85c4b738d5100e57ac1b301aee5bbd60604303c5c105a1887b89a
MD5 396624fdeae82f159cb8c6bfdd7e6e5f
BLAKE2b-256 774eb497368e84b2e523c2e419d1711c4fe9ff090397487ff58d521eacf2de35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sklearn_freezer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd22b76305838b2d7eb42f22a621cba2a2bf78bbaf93526b2e7f93f42557835e
MD5 4d85cfe37bc9a566851d957e8f6753d1
BLAKE2b-256 3dd943ff137a5adfdcb1703fb53adc39d9e994d89ff0c2704d578bcf23043f78

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