An Optuna integration for hyperparameter tuning in Scikit-Learn
Project description
What is Sklearn-Optuna?
OptunaSearchCV is 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 sklearn search attributes work exactly as expected.
Optuna's tree-structured Parzen estimator (TPE), CMA-ES, and other samplers explore the search space more efficiently than grid or random search, while Optuna distributions (FloatDistribution, IntDistribution, CategoricalDistribution) let you express log-scaled, conditional, and mixed-type parameter spaces. Sklearn-Optuna requires Python 3.11+, scikit-learn ≥ 1.6, and Optuna ≥ 3.5.
Note: This project is inspired by optuna-integration's OptunaSearchCV.
What are the features of Sklearn-Optuna?
- Drop-in sklearn search: Extends
BaseSearchCV— works withPipeline,clone(),get_params()/set_params(), and any estimator that follows the sklearn interface. - Optuna samplers: Choose TPE, CMA-ES, Random, or any Optuna sampler through the
Samplerwrapper, with full sklearn parameter introspection. - Flexible distributions: Use Optuna's
FloatDistribution,IntDistribution, andCategoricalDistributionfor log-scaled, bounded, and categorical search spaces. - Study persistence and reuse: Pass a
Storagebackend to persist trials to a database, or resume optimization by passing an existing study tofit(study=...). - Callbacks: Attach Optuna callbacks (e.g.,
MaxTrialsCallback) via theCallbackwrapper to control optimization flow. - Nested pipeline search: Use
OptunaSearchCVas a step inside a pipeline tuned by anotherOptunaSearchCVand tune itsSamplerduring 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:
- Online: https://sklearn-optuna.readthedocs.io/en/latest/pages/examples/
- Locally: Run
just example quickstartto open an interactive notebook
Can I contribute?
We welcome contributions, feedback, and questions:
- Report issues or request features: GitHub Issues
- Join the discussion: GitHub Discussions
- Contributing Guide: CONTRIBUTING.md
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?
- Documentation: https://sklearn-optuna.readthedocs.io/
- Interactive examples: https://sklearn-optuna.readthedocs.io/en/latest/pages/examples/
- GitHub Discussions: https://github.com/stateful-y/sklearn-optuna/discussions
License
This project is licensed under the terms of the Apache-2.0 License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sklearn_optuna-0.1.0a1.tar.gz.
File metadata
- Download URL: sklearn_optuna-0.1.0a1.tar.gz
- Upload date:
- Size: 106.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c59521f4d409e2656e2c542aec7fad767868c9752c8017bf4724cbb9acb0fbb
|
|
| MD5 |
bb693bf44bb70ff0b6f1bd8e62fa027d
|
|
| BLAKE2b-256 |
6f18d42a831804872db46f3f43313bce8cfe9be673884aa89ee2b906df93bea2
|
Provenance
The following attestation bundles were made for sklearn_optuna-0.1.0a1.tar.gz:
Publisher:
publish-release.yml on stateful-y/sklearn-optuna
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sklearn_optuna-0.1.0a1.tar.gz -
Subject digest:
5c59521f4d409e2656e2c542aec7fad767868c9752c8017bf4724cbb9acb0fbb - Sigstore transparency entry: 937953970
- Sigstore integration time:
-
Permalink:
stateful-y/sklearn-optuna@f889e2ff220875fd99c5ba4283788049dc250a74 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stateful-y
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@f889e2ff220875fd99c5ba4283788049dc250a74 -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file sklearn_optuna-0.1.0a1-py3-none-any.whl.
File metadata
- Download URL: sklearn_optuna-0.1.0a1-py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dc8a3eee46b82d6c5f5ddc1770d61d4be7f5bdd7f0cec59a4c3fa49974e924d
|
|
| MD5 |
b0642e9e05d189a76c061e5aa44a1fd4
|
|
| BLAKE2b-256 |
021f2165b9adf3316307421c7ca969771fe7c428fb33d84731052c0e747a79b0
|
Provenance
The following attestation bundles were made for sklearn_optuna-0.1.0a1-py3-none-any.whl:
Publisher:
publish-release.yml on stateful-y/sklearn-optuna
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sklearn_optuna-0.1.0a1-py3-none-any.whl -
Subject digest:
6dc8a3eee46b82d6c5f5ddc1770d61d4be7f5bdd7f0cec59a4c3fa49974e924d - Sigstore transparency entry: 937954003
- Sigstore integration time:
-
Permalink:
stateful-y/sklearn-optuna@f889e2ff220875fd99c5ba4283788049dc250a74 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stateful-y
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@f889e2ff220875fd99c5ba4283788049dc250a74 -
Trigger Event:
pull_request
-
Statement type: