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.
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/.
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)
# 4) Optimize the classifier, the optimization returns the best estimator found in the optimization process
# - 10 generations starting with a population of 10 individuals, other parameters are set to default
opt.fit(X, y, population_size=10, generations=10)
print(opt.best_estimator_)
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 can be found in examples on readthedocs.io.
The following dependencies are used in mloptimizer
:
Optional:
The documentation for mloptimizer
can be found in the project's wiki
with examples, classes and methods reference.
This project is licensed under the MIT License.