Skip to main content

A Package for wrapping Python classes into Scikit-Learn estimators

Project description

Sklearn-Wrap

Python Version License PyPI Version Conda Version codecov

What is Sklearn-Wrap?

Sklearn-wrap enables you to wrap any Python class into a scikit-learn compatible estimator without rewriting your code. Whether you're integrating XGBoost's Booster API, custom gradient descent algorithms, or third-party machine learning libraries, sklearn-wrap provides the glue layer that makes them work seamlessly with sklearn's ecosystem.

With sklearn-wrap, you gain immediate access to GridSearchCV for hyperparameter tuning, Pipeline for composable workflows, and joblib for serialization, all while maintaining your original implementation. Perfect for data scientists who want sklearn compatibility without sacrificing custom logic or performance.

What are the features of Sklearn-Wrap?

  • Minimal boilerplate: 10-15 lines to wrap any Python class into a full sklearn estimator
  • Auto-parameter discovery: Constructor parameters automatically exposed for GridSearchCV/RandomizedSearchCV
  • Nested parameters: Support sklearn's double-underscore syntax for nested estimator hierarchies
  • Full ecosystem compatibility: Works seamlessly with Pipeline, cross-validation, and joblib serialization
  • Built-in validation: Optional parameter constraints with automatic type/value checking before fit
  • YAML configuration: Declarative estimator definitions with EstimatorConfig - build, save, and load estimators from YAML files (pip install sklearn-wrap[config])

How to install Sklearn-Wrap?

Install the Sklearn-Wrap package using pip:

pip install sklearn_wrap

or using uv:

uv pip install sklearn_wrap

or using conda:

conda install -c conda-forge sklearn_wrap

or using mamba:

mamba install -c conda-forge sklearn_wrap

or alternatively, add sklearn_wrap to your requirements.txt or pyproject.toml file.

How to get started with Sklearn-Wrap?

Here's a minimal example wrapping a custom polynomial regression class:

import numpy as np
from sklearn_wrap.base import BaseClassWrapper, _fit_context
from sklearn.base import RegressorMixin
from sklearn.model_selection import GridSearchCV

# Your custom class (unchanged)
class PolynomialRegressor:
    def __init__(self, degree=2, learning_rate=0.01, n_iterations=1000):
        self._degree = degree
        self._learning_rate = learning_rate
        self._n_iterations = n_iterations

    def fit_model(self, X, y):
        # ... your implementation ...
        return self

    def predict_output(self, X):
        # ... your implementation ...
        pass

# Wrapper (just 10 lines!)
class PolynomialWrapper(BaseClassWrapper, RegressorMixin):
    _estimator_name = "regressor"
    _estimator_base_class = object

    @_fit_context(prefer_skip_nested_validation=True)
    def fit(self, X, y):
        self.instance_.fit_model(X, y)  # Delegate to wrapped instance
        return self

    def predict(self, X):
        return self.instance_.predict_output(X)

# Use with sklearn tools
wrapper = PolynomialWrapper(
    regressor=PolynomialRegressor,
    degree=2,
    learning_rate=0.01
)

# Immediate GridSearchCV compatibility
param_grid = {'degree': [1, 2, 3], 'learning_rate': [0.001, 0.01, 0.1]}
grid_search = GridSearchCV(wrapper, param_grid, cv=5)
grid_search.fit(X, y)

How do I use Sklearn-Wrap?

Full documentation is available at https://sklearn-wrap.readthedocs.io/.

Interactive examples are available in the examples/ directory:

Can I contribute?

We welcome contributions, feedback, and questions:

If you are interested in becoming a maintainer or taking a more active role, please reach out to Guillaume Tauzin on stateful-y.io contact page.

Where can I learn more?

For questions and discussions, you can also open a discussion.

License

This project is licensed under the terms of the Apache-2.0 License.

Acknowledgements

This project is maintained by stateful-y, an ML consultancy specializing in data science & engineering. If you're interested in collaborating or learning more about our services, please visit our website.

Made by stateful-y

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_wrap-0.1.0a6.tar.gz (174.4 kB view details)

Uploaded Source

Built Distribution

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

sklearn_wrap-0.1.0a6-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file sklearn_wrap-0.1.0a6.tar.gz.

File metadata

  • Download URL: sklearn_wrap-0.1.0a6.tar.gz
  • Upload date:
  • Size: 174.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sklearn_wrap-0.1.0a6.tar.gz
Algorithm Hash digest
SHA256 cc7f1ad172e130d299ddeaf28f9562dbcd0b24918cd93ccb12dbb94cea664b7b
MD5 420ee33b8fdaeb72a2cbe1b8846df47b
BLAKE2b-256 b5c13d173328789b5e801ef321e617cc5f6e1000950760784c14fc2e2b227814

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklearn_wrap-0.1.0a6.tar.gz:

Publisher: publish-release.yml on stateful-y/sklearn-wrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sklearn_wrap-0.1.0a6-py3-none-any.whl.

File metadata

  • Download URL: sklearn_wrap-0.1.0a6-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sklearn_wrap-0.1.0a6-py3-none-any.whl
Algorithm Hash digest
SHA256 4a41bd942c2b88798507d9092f11389b424de2a8620860e9fbf03e73b6ea67cd
MD5 bb61a169c223b75e0a817de0c0cf764b
BLAKE2b-256 94ba88749d2d58db0dba37efeb40c9655492f1cdd7f0fc8f6a738b949feda2ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklearn_wrap-0.1.0a6-py3-none-any.whl:

Publisher: publish-release.yml on stateful-y/sklearn-wrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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