Skip to main content

mloptimizer_banner

Documentation Status PyPI version PyPI pyversions Tests Coverage Status Shield: Buy me a coffee

mloptimizer is a Python library for optimizing hyperparameters of machine learning algorithms using genetic algorithms. With mloptimizer, you can find the optimal set of hyperparameters for a given machine learning model and dataset, which can significantly improve the performance of the model. The library supports several popular machine learning algorithms, including decision trees, random forests, and gradient boosting classifiers. The genetic algorithm used in mloptimizer provides an efficient and flexible approach to search for the optimal hyperparameters in a large search space.

Features

  • Easy to use
  • DEAP-based genetic algorithm ready to use with several machine learning algorithms
  • Adaptable to use with any machine learning algorithm that complies with the Scikit-Learn API
  • Default hyperparameter ranges
  • Default score functions for evaluating the performance of the model
  • Reproducibility of results
  • Early stopping to prevent overfitting
  • Population seeding with known good configurations
  • Performance tracking (trials count, optimization time)
  • Zero file output mode for cleaner workflows (enabled by default)

Advanced Features

  • Extensible with more machine learning algorithms that comply with the Scikit-Learn API
  • Customizable hyperparameter ranges
  • Customizable score functions
  • Optional mlflow compatibility for tracking the optimization process
  • Generation-level MLflow tracking with nested runs
  • Responsive Plotly visualizations with WebGL acceleration
  • Joblib-based parallelization for better compatibility

Installation

It is recommended to create a virtual environment using the venv package. To learn more about how to use venv, check out the official Python documentation at https://docs.python.org/3/library/venv.html.

# Create the virtual environment
python -m venv myenv
# Activate the virtual environment
source myenv/bin/activate

To install mloptimizer, run:

pip install mloptimizer

You can get more information about the package installation at https://pypi.org/project/mloptimizer/.

Quickstart

Here's a simple example of how to optimize hyperparameters in a decision tree classifier using the iris dataset:

from mloptimizer.interfaces import GeneticSearch, HyperparameterSpaceBuilder
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris

# 1) Load the dataset and get the features and target
X, y = load_iris(return_X_y=True)

# 2) Define the hyperparameter space (a default space is provided for some algorithms)
hyperparameter_space = HyperparameterSpaceBuilder.get_default_space(DecisionTreeClassifier)

# 3) Create the optimizer and optimize the classifier
opt = GeneticSearch(
    estimator_class=DecisionTreeClassifier,
    hyperparam_space=hyperparameter_space,
    generations=10,
    population_size=20,
    early_stopping=True,        # Stop early if no improvement
    patience=3,                  # Wait 3 generations
    cv=5,                        # 5-fold cross-validation
    seed=42                      # Reproducibility
)

# 4) Optimize (no files created by default)
opt.fit(X, y)

# Access results
print(f"Best score: {opt.best_estimator_.score(X, y)}")
print(f"Trials evaluated: {opt.n_trials_}")
print(f"Time taken: {opt.optimization_time_:.2f}s")

Other algorithms can be used, such as RandomForestClassifier or XGBClassifier which have a default hyperparameter space defined in the library. Even if the algorithm is not included in the default hyperparameter space, you can define your own hyperparameter space following the documentation.

More details in the documentation.

Examples

Examples can be found in examples on readthedocs.io.

Dependencies

The following dependencies are used in mloptimizer:

  • Deap - Genetic Algorithms
  • XGBoost - Gradient boosting framework
  • CatBoost - Gradient boosting framework
  • LightGBM - Gradient boosting framework
  • Scikit-Learn - Machine learning algorithms and utilities
  • Plotly - Interactive visualizations
  • Seaborn - Statistical visualizations
  • joblib - Parallel processing
  • tqdm - Progress bars

Optional:

  • Keras - Deep learning library
  • mlflow - Tracking the optimization process

Documentation

The documentation for mloptimizer can be found in the project's wiki with examples, classes and methods reference.

Authors

  • Antonio Caparrini - Author - caparrini
  • Javier Arroyo Gallardo - Author - javiag

Analytics

Alt

License

This project is licensed under the MIT License.

Metadata

Release files for mloptimizer 0.9.4

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

Source distribution (sdist)

Source distribution for mloptimizer 0.9.4
File Size Uploaded
mloptimizer-0.9.4.tar.gz 80.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mloptimizer 0.9.4
File Interpreter ABI Platform
mloptimizer-0.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 194.7 kB

Release files / mloptimizer-0.9.4.tar.gz

Download URL mloptimizer-0.9.4.tar.gz
Size 80.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9457bd884005756429e4b69d5ecb51ebe5735346eefa49177206e0e4a7e9deb1
BLAKE2b-256 checksum
How to use checksums
088f8baf7a59aa6d5ff067e9b5a6fab8ed8f0797d0bf1ac29d39bf658c79e607
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / mloptimizer-0.9.4-py3-none-any.whl

Download URL mloptimizer-0.9.4-py3-none-any.whl
Size 114.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d27a19bac3abfe0197667350d3b1c2aea5f54b22ab3c91bf5994ecd5a23011d0
BLAKE2b-256 checksum
How to use checksums
7835b9f4f60444fd99370b1c43f572b8c97aa98b3cc2a41e876d7345da3f2968
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.9.4 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.7

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

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