Skip to main content

Add your description here

Project description

sk-stepwise

Overview

StepwiseHyperoptOptimizer is a custom Python class that combines the power of the Hyperopt optimization library with a stepwise optimization strategy for hyperparameter tuning of machine learning models. It extends the capabilities of scikit-learn's BaseEstimator and MetaEstimatorMixin, making it easy to integrate into existing machine learning workflows.

This class enables you to optimize a model's hyperparameters in a sequential manner, following a predefined series of hyperparameter spaces. Each step in the sequence focuses on refining a specific set of parameters, allowing for a more targeted and efficient optimization process. The hyperparameter optimization uses Tree of Parzen Estimators (TPE) through the Hyperopt library.

Features

  • Stepwise Hyperparameter Tuning: Break down the optimization process into multiple steps, each refining a specific set of hyperparameters.
  • Hyperopt Integration: Utilize Hyperopt's TPE algorithm to find the optimal parameters efficiently.
  • Scikit-learn Compatibility: StepwiseHyperoptOptimizer is compatible with the scikit-learn ecosystem, making it easy to use in scikit-learn pipelines and workflows.
  • Flexible Scoring: Supports both default scikit-learn scoring metrics and custom scoring functions.

Installation

pip install sk-stepwise

If you are planning on developing this package, you should install the precommit hooks and other development dependencies.

pip install -r requirements-dev.txt

You should also install the package in editable mode:

uv pip install -e .

Run the tests with:

uv run pytest

Usage

Here's an example of how to use StepwiseHyperoptOptimizer to optimize a scikit-learn model:

>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.ensemble import RandomForestRegressor
>>> from sk_stepwise import StepwiseHyperoptOptimizer
>>> import hyperopt

>>> # Sample data
>>> X = pd.DataFrame(np.random.rand(100, 5), columns=[f"feature_{i}" for i in range(5)])
>>> y = pd.Series(np.random.rand(100))

>>> # Define the model
>>> model = RandomForestRegressor()

>>> # Define the parameter space sequence for stepwise optimization
>>> param_space_sequence = [
...     {"n_estimators": hyperopt.hp.choice("n_estimators", [50, 100, 150])},
...     {"max_depth": hyperopt.hp.quniform("max_depth", 3, 10, 1)},
...     {"min_samples_split": hyperopt.hp.uniform("min_samples_split", 0.1, 1.0)},
... ]

>>> # Create the optimizer
>>> optimizer = StepwiseHyperoptOptimizer(model=model, param_space_sequence=param_space_sequence, max_evals_per_step=50)

>>> # Fit the optimizer
>>> optimizer.fit(X, y)

>>> # Make predictions
>>> predictions = optimizer.predict(X)

Key Methods

  • fit(X, y): Fits the optimizer to the data, performing stepwise hyperparameter optimization.
  • predict(X): Uses the optimized model to make predictions.
  • score(X, y): Evaluates the optimized model on a test set.

Parameters

  • model (_Fitable): A scikit-learn compatible model that implements fit, predict, and set_params methods.
  • param_space_sequence (list[dict]): A list of dictionaries representing the hyperparameter spaces for each optimization step.
  • max_evals_per_step (int): The maximum number of evaluations to perform for each step of the optimization.
  • cv (int): Number of cross-validation folds.
  • scoring (str or Callable): The scoring metric to use for evaluation. Default is "neg_mean_squared_error".
  • random_state (int): Random seed for reproducibility.

Contributing

Contributions are welcome! Feel free to open issues or pull requests for new features, bug fixes, or documentation improvements.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

  • Hyperopt for hyperparameter optimization.
  • scikit-learn for model implementation and evaluation utilities.

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

sk_stepwise-0.1.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

sk_stepwise-0.1.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file sk_stepwise-0.1.2.tar.gz.

File metadata

  • Download URL: sk_stepwise-0.1.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.5

File hashes

Hashes for sk_stepwise-0.1.2.tar.gz
Algorithm Hash digest
SHA256 06ae88249e106b1effeaa9243c3da7db4b1ab6fea753d5dcc2ee6b313445ac26
MD5 124e29dd59a0d4a80a0de6eeb0191fc6
BLAKE2b-256 2cb864cd9c3fc29049410770da2beee4892a1c59b17dfbeda9cbe941ccbd1612

See more details on using hashes here.

File details

Details for the file sk_stepwise-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sk_stepwise-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b93b37dc71212644cc9bec57f441e1e54e0d126472d999bfe9661d0e9ffcf45
MD5 07f4dc5b6b7252afb8676ae767d6748f
BLAKE2b-256 40c8b110371695948f55c54f209d2a92c74171013c7e8a879bb668cc50281367

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