Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Sklearn-Optuna

Python Version License PyPI Version Conda Version codecov

What is Sklearn-Optuna?

Sklearn-Optuna provides OptunaSearchCV, a drop-in replacement for Scikit-Learn's GridSearchCV and RandomizedSearchCV that uses Optuna for hyperparameter optimization. It extends BaseSearchCV so that fit(), score(), best_params_, cv_results_, and all other Scikit-Learn search attributes work exactly as expected.

With Sklearn-Optuna, you gain immediate access to sophisticated Bayesian optimization while maintaining full compatibility with Scikit-Learn's Pipeline, cross-validation, and serialization. Perfect for data scientists who want smarter hyperparameter search without learning a new API.

Note: This project is inspired by Optuna-Integration's OptunaSearchCV.

What are the features of Sklearn-Optuna?

  • Drop-in Scikit-Learn search: Extends BaseSearchCV and works with Pipeline, clone(), get_params()/set_params(), and any estimator
  • Smart optimization: TPE, CMA-ES, and other samplers explore the search space more efficiently than grid or random search
  • Flexible distributions: Optuna distributions (FloatDistribution, IntDistribution, CategoricalDistribution) for log-scaled, bounded, and categorical spaces
  • Study persistence: Pass a Storage backend to persist trials to a database, or resume optimization by passing an existing study to fit()
  • Callbacks: Attach Optuna callbacks (e.g., MaxTrialsCallback) to control optimization flow
  • Nested pipeline search: Use OptunaSearchCV inside another OptunaSearchCV for hierarchical hyperparameter optimization

How to install Sklearn-Optuna?

Install the Sklearn-Optuna package using pip:

pip install sklearn_optuna

or using uv:

uv pip install sklearn_optuna

or using conda:

conda install -c conda-forge sklearn_optuna

or using mamba:

mamba install -c conda-forge sklearn_optuna

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

How to get started with Sklearn-Optuna?

1. Define a search space with Optuna distributions

from optuna.distributions import FloatDistribution
from sklearn.linear_model import LogisticRegression

param_distributions = {
    "C": FloatDistribution(1e-2, 10.0, log=True),
}

2. Create and fit an OptunaSearchCV

from sklearn_optuna import OptunaSearchCV, Sampler
import optuna

search = OptunaSearchCV(
    LogisticRegression(max_iter=200),
    param_distributions,
    n_trials=20,
    sampler=Sampler(sampler=optuna.samplers.TPESampler, seed=42),
    cv=5,
)
search.fit(X_train, y_train)

3. Inspect results

print(search.best_params_)   # {'C': 1.23}
print(search.best_score_)    # 0.95
search.best_estimator_.predict(X_test)

How do I use Sklearn-Optuna?

Full documentation is available at https://sklearn-optuna.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 GitHub Discussions.

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

Download files

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

Source Distribution

sklearn_optuna-0.1.0a5.tar.gz (352.8 kB view details)

Uploaded Source

Built Distribution

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

sklearn_optuna-0.1.0a5-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file sklearn_optuna-0.1.0a5.tar.gz.

File metadata

  • Download URL: sklearn_optuna-0.1.0a5.tar.gz
  • Upload date:
  • Size: 352.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sklearn_optuna-0.1.0a5.tar.gz
Algorithm Hash digest
SHA256 67cedb8ed1078d7bc2ed5eed204ddb5eac545703b529770df3810dc3bedf3d1e
MD5 2fa23afaa731fbc724eb4e0fabc291f4
BLAKE2b-256 eb858eb0ed4b174673032c3a2881ae6ef5a6774faf7371cccc881a025d439e22

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklearn_optuna-0.1.0a5.tar.gz:

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

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_optuna-0.1.0a5-py3-none-any.whl.

File metadata

File hashes

Hashes for sklearn_optuna-0.1.0a5-py3-none-any.whl
Algorithm Hash digest
SHA256 5a1966e9e2010d9f99e05fee05de372098ee4f3faa21245a30cc166a84b59d12
MD5 c6510b2a1e03b61336c66385b3f7ae68
BLAKE2b-256 1619112731163ed94f9475936fde34b4f64c9d27682fa6d4ca5ec2f76e36dbc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklearn_optuna-0.1.0a5-py3-none-any.whl:

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

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

Release history Release notifications | RSS feed

This release

0.1.0a5 This release

2 files

Supported by

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