Skip to main content

Hyperparameter Optimizer

A lightweight and flexible Python library for hyperparameter tuning using metaheuristic techniques.
Currently, the library includes Particle Swarm Optimization (PSO) and Pattern Search (PS) only.

Designed for Scikit-learn-compatible models, this package offers an easy-to-use interface for optimizing model performance in just a few lines.

PyPI version License: MIT Downloads


📦 Installation

Install from PyPI:

pip install hyperparameter-optimizer

⚙️ Features

  • ✅ Metaheuristic optimization using Particle Swarm Optimization (PSO) and Pattern Search (PS)
  • ✅ Supports continuous, integer, and categorical hyperparameters
  • ✅ Compatible with any Scikit-learn estimator
  • ✅ Custom scoring metrics
  • ✅ Cross-validation built-in
  • ✅ Verbose logging and full traceability

📚 API Reference

HyperparameterOptimizer

HyperparameterOptimizer(
                            obj_func,                       # machine learning model or pipeline being created
                            params,                         # dictionary of parameters for which the model is to be optimized.
                            scoring,                        # scoring metric (e.g., 'accuracy')
                            opt_type="max",                 # type of optimization: "max" for maximization (default) or "min" for minimization.
                            cv=5,                           # number of cross-validation folds
                            verbose=1                       # binary with a value of 1 (default) to show iteration information.
)

optimizePSO

optimizePSO(
            features,                       # training features.
            target,                         # training target.
            nParticles,                     # number of particles in the swarm.
            bounds,                         # list of (min, max) tuples or a categorical list of choices. 
                                            # if either min/max is non-integer, the algorithm shall consider them as floats (i.e., continuous).
            w=0.5,                          # inertia weight.
            c1=1,                           # cognitive weight.
            c2=1,                           # social weight.
            maxIter=20,                     # maximum number of iterations.
            mutation_prob=0.1               # mtation probability -- for discrete hyperparameters.
)

optimizePS

optimizePS(
            features,                       # training features.
            target,                         # training target.
            bounds,                         # list of (min, max) tuples or a categorical list of choices. 
                                            # if either min/max is non-integer, the algorithm shall consider them as floats (i.e., continuous).
            mesh_size_coeff=0.6,            # mesh size coefficient.
            acc_coeff=1,                    # acceleration/mesh expansion coefficient.
            contr_coeff=0.5,                # mesh contraction coefficient.
            search_method="gps",            # new points exploration method; either 'gps' or 'mads'.
            min_mesh_ratio=0.001,           # minimum mesh size as a percentage of parameter range.
            maxIter=25                      # maximum number of iterations.
)

🚀 Quickstart

Here's a basic example of how to use Hyperparameter Optimizer with a Scikit-learn model (e.g., RandomForestClassifier):

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from hyperparameter_optimizer import HyperparameterOptimizer

# Load data
X, y = load_iris(return_X_y=True)

# Define parameter space
param_space = {
    'n_estimators': [],
    'max_depth': [],
    'criterion': []
}

# Initialize the optimizer
optimizer = HyperparameterOptimizer(
                                        obj_func=RandomForestClassifier(),
                                        params=param_space,
                                        scoring='accuracy',
                                        opt_type='max',
                                        cv=3,
                                        verbose=1
                                    )

# Run optimization
# [1] Particle Swarm Optimization
particles, Gbest_history, Gbest_pos, Gbest_score = optimizer.optimizePSO(
                                                                            features = X,
                                                                            target = y,
                                                                            nParticles = 10,
                                                                            bounds = [(10, 1000), (1, 10), ['gini', 'entropy']],
                                                                            maxIter = 10
                                                                    )
print("Best Params:", Gbest_pos)
print("Best Score:", Gbest_score)

# [2] Pattern Search Optimization
Gbest_history2, Gbest_pos2, Gbest_score2 = optimizer.optimizePS(
                                                                    features = X,
                                                                    target = y,
                                                                    bounds = [(10, 1000), (1, 10), ['gini', 'entropy']],
                                                                    mesh_size_coeff = 0.1,
                                                                    acc_coeff = 1,
                                                                    contr_coeff = 0.50,
                                                                    maxIter = 10
                                                            )
print("Best Params:", Gbest_pos2)
print("Best Score:", Gbest_score2)

🧠 How It Works

This library leverages two optimization techniques—Particle Swarm Optimization (PSO) and Pattern Search (PS)—to explore the hyperparameter space effectively.

Particle Swarm Optimization simulates a group of particles (candidate solutions) moving through the search space. Each particle adjusts its position based on its own best-known position and the best-known position among its peers, enabling a balance between exploration and exploitation as the swarm converges toward an optimal solution.

Pattern Search is a derivative-free method that explores the neighborhood of a current solution by systematically evaluating a set of search directions. If an improved solution is found, the algorithm moves in that direction and repeats the process, enabling efficient local refinement without relying on gradient information.

Together, these methods provide robust global and local search capabilities for hyperparameter tuning.


📜 License

This project is licensed under the MIT License.
© 2025 Dr. Ahmed Moussa


🤝 Contributing

Pull requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.


📫 Contact

For feedback, bugs, or collaboration ideas:


⭐️ Show Your Support

If you find this project useful, consider giving it a ⭐️ on GitHub!

Metadata

Release files for hyperparameter-optimizer 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hyperparameter-optimizer 0.1.2
File Size Uploaded Provenance Actions
hyperparameter_optimizer-0.1.2.tar.gz 11.4 kB Without provenance Details for hyperparameter_optimizer-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hyperparameter-optimizer 0.1.2
File Interpreter ABI Platform Actions
hyperparameter_optimizer-0.1.2-py3-none-any.whl Python 3 none any Details for hyperparameter_optimizer-0.1.2-py3-none-any.whl

Total release size: 20.9 kB

Release files / hyperparameter_optimizer-0.1.2.tar.gz

Download URL hyperparameter_optimizer-0.1.2.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e6013bb35d6baf78ab5ea32eb452702190888db3812b470ff94413e2852a0eba
BLAKE2b-256 checksum
How to use checksums
78115171bd4d86acdff85f3a6c853830e64f892227658bb84e27d289db1e4718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.8

Release files / hyperparameter_optimizer-0.1.2-py3-none-any.whl

Download URL hyperparameter_optimizer-0.1.2-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d808011ae7a11866ebde3b93c785aa3971d890d0a901b786e88d7ee730c2bbf
BLAKE2b-256 checksum
How to use checksums
dd98422ca923df50cdc45d93171a3b24a837faa4dc959491c395f081f1d8abd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.8

Security for hyperparameter-optimizer 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page