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 single-sample inference.

Overview

Sklearn-Freezer compiles scikit-learn classifiers' predict or predict_proba methods into optimized implementations, dramatically improving single-sample prediction performance for real-time applications.

Disclaimer: This is a simple optimization created to address performance bottlenecks from iterative scikit-learn model calls. The implementation is naive with limited model support. Currently only predict_proba for binary classification of RandomForestClassifier and DecisionTreeClassifier are supported.

Compilation Backends

  • Python: Pure Python implementation (baseline)
  • Cython: Cython-compiled implementation (significant speedup)
  • C: Native C extension (maximum speed)

Installation

# Basic installation
pip install sklearn-freezer

# With optional backends
pip install sklearn-freezer[cython]  # Cython backend
pip install sklearn-freezer[c]       # C backend
pip install sklearn-freezer[all]     # All backends

Quick Start

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn_freezer import compile_predict_proba

# Train and compile model
X, y = make_classification(n_samples=1000, n_features=4, random_state=42)
clf = RandomForestClassifier(random_state=42).fit(X, y)
compiled_func = compile_predict_proba(clf, backend="c")

# Fast single-sample prediction
probability = compiled_func(*X[0])

Performance

The benchmark compares five prediction approaches:

  1. Iterative clf.predict_proba: Individual clf.predict_proba([sample]) calls
  2. Compiled Python/Cython/C: Using compiled backends
  3. Batch clf.predict_proba: Single clf.predict_proba(all_samples) call

Performance ordering: iterative single-sample calls < compiled functions < batch processing.

Run benchmark: python example/benchmark.py

Requirements

  • Python 3.10+
  • scikit-learn

Optional: cython, setuptools

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.1.0.tar.gz (38.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.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sklearn_freezer-0.1.0.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for sklearn_freezer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9f516a4f4dbcd789d46adaa73ae5249995e6ff727ecb546f2bb692677a2aea94
MD5 b0f321460227943c11013cc7b2f3eb46
BLAKE2b-256 26f47143d41b31c603e0962f6963ddf4527f78067553751bdadbfa8b2655c0b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sklearn_freezer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8fdcd7372ab59c8920231b31e1b57ffa16ff13ed885ef953f4486a125f58e6
MD5 c8cdd96d7352c062829691c986b99689
BLAKE2b-256 033ba066bc6f11d4f5ea57bc3dc783584d553799220021d96433999d9e162d5d

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