Skip to main content

Logo

Evolutionary Algorithm For Machine Learning

Installation

To use evolearn, first install it using pip:

pip install evolearn

Genetic Optimization CV

To perform hyperparameter tuning using genetic algoritm, you need to first import other modules from

  1. evolearn.hyperparameter_tuning.initialization
  2. evolearn.hyperparameter_tuning.evaluation
  3. evolearn.hyperparameter_tuning.selection
  4. evolearn.hyperparameter_tuning.mating
  5. evolearn.hyperparameter_tuning.reproduction
  6. evolearn.hyperparameter_tuning.mutation
  7. evolearn.hyperparameter_tuning.environment (optional)
  8. evolearn.hyperparameter_tuning.genetic_hyperparameter_tuning

Although the modules from environment are optional for you to determine to use them in your search or not, the searching might end up stopping early or not finding the ideal results. These modules can help to prevent pre-mature convergence and also control other hyperparameters for GA.

For example:

from evolearn.hyperparameter_tuning.initialization import Genes
from evolearn.hyperparameter_tuning.evaluation import FitnessFunction
from evolearn.hyperparameter_tuning.selection import (RankSelection,
                                           				RouletteWheelSelection,
                                            				SteadyStateSelection,
                                            				TournamentSelection,
                                            				StochasticUniversalSampling,
                                            				BoltzmannSelection
                                            				)
from evolearn.hyperparameter_tuning.mating import MatingFunction
from evolearn.hyperparameter_tuning.reproduction import (KPointCrossover,
                                               			      LinearCombinationCrossover,
                                               			      FitnessProportionateAverage
                                             				      )
from evolearn.hyperparameter_tuning.mutation import (Boundary,
                                           				Shrink
                                          				 )
from evolearn.hyperparameter_tuning.environment import (AdaptiveReproduction,
                                              			     AdaptiveMutation,
                                              			     Elitism
                                              			     )
from evolearn.hyperparameter_tuning.genetic_hyperparameter_tuning import GenesSearchCV
from sklearn.ensemble import RandomForestRegressor
search_space_rf = {
          'max_depth':(1, 16, 'uniform'),
          'n_estimators':(100, 1000, 'uniform'),
          'criterion':('squared_error', 'absolute_error', 'poisson')
      }  
opt = GenesSearchCV(
      n_gen=10,
      initialization_fn=Genes(search_space=search_space_rf, pop_size=30),
      fitness_fn=FitnessFunction(
          estimator=RandomForestRegressor(n_jobs=-1),
          cv=3,
          scoring='neg_mean_absolute_error',
      ),
      selection_fn=StochasticUniversalSampling(.7),
      mating_fn=MatingFunction(increst_prevention=False),
      reproduction_fn=KPointCrossover(1),
      mutation_fn=Shrink(),
      adaptive_population=AdaptiveReproduction(10),
      elitism=Elitism(),
      adaptive_mutation=AdaptiveMutation()
  )   
opt.fit(X_train, y_train)

Max Fitness: -2023.200579609583
{'max_depth': 5, 'n_estimators': 561, 'criterion': 'absolute_error'}

The choices of selection_fn, reproduction_fn, mutation_fn are actually up to your personal preference. One can pick what they believe are most benefit to their searching preocess.

Genetic Feature Selection

To perform feature selection using genetic algoritm, you need to first import other modules from

  1. evolearn.feature_selection.initialization
  2. evolearn.feature_selection.evaluation
  3. evolearn.feature_selection.selection
  4. evolearn.feature_selection.mating
  5. evolearn.feature_selection.reproduction
  6. evolearn.feature_selection.mutation
  7. evolearn.feature_selection.environment (optional)
  8. evolearn.feature_selection.genetic_feature_selection

The modules looks similar to those modules from the GenesSearchCV section, but in fact their internal mechanisim work slightly differently. You need to be ware of importing the wrong modules when using genetic feature selection.

For example:

from evolearn.feature_selection.initialization import Genes
from evolearn.feature_selection.evaluation import FitnessFunction
from evolearn.feature_selection.selection import (RankSelection,
                                                   RouletteWheelSelection,
                                                   SteadyStateSelection,
                                                   TournamentSelection,
                                                   StochasticUniversalSampling,
                                                   BoltzmannSelection
                                                   )
from evolearn.feature_selection.mating import MatingFunction
from evolearn.feature_selection.reproduction import KPointCrossover
from evolearn.feature_selection.mutation import (BitStringMutation,
                                                ExchangeMutation,
                                                ShiftMutation
                                                )

from evolearn.feature_selection.environment import (AdaptiveReproduction,
                                                AdaptiveMutation,
                                                Elitism
                                                )

from evolearn.feature_selection.genetic_feature_selection import GeneticFeatureSelectionCV
from sklearn.ensemble import RandomForestRegressor
opt = GeneticFeatureSelectionCV(
   n_gen=10,
   initialization_fn=Genes(pop_size=50),
   fitness_fn=FitnessFunction(
       estimator=RandomForestRegressor(n_jobs=-1),
       cv=3,
       scoring='neg_mean_absolute_error'
   ),
   selection_fn=RouletteWheelSelection(.7),
   mating_fn=MatingFunction(),
   reproduction_fn=KPointCrossover(k=4),
   mutation_fn=BitStringMutation(),
   adaptive_population=None,
   elitism=None,
   adaptive_mutation=None
   )

opt.fit(X_train, y_train)
print(opt.best_fitness_)
print(opt.best_params_)

-2797.7245589631652
{'age': True, 'sex': False, 'bmi': True, 'children': True, 'smoker': True, 'region': False}

Metadata

Release files for evolearn 1.0.8

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

Source distribution (sdist)

Source distribution for evolearn 1.0.8
File Size Uploaded Provenance Actions
evolearn-1.0.8.tar.gz 31.8 kB Without provenance Details for evolearn-1.0.8.tar.gz

Built distribution (wheel)

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

Total release size: 94.8 kB

Release files / evolearn-1.0.8.tar.gz

Download URL evolearn-1.0.8.tar.gz
Size 31.8 kB
Tags Source
SHA-256 checksum
How to use checksums
37f8ed33ecfe7de85cd1ef63cfc927e0edad775b5ccf603fa2204255c5202444
BLAKE2b-256 checksum
How to use checksums
a2a2daa51f0539e55162256b7321f249ccfc93c9f387fc204326fd06c71962ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / evolearn-1.0.8-py3-none-any.whl

Download URL evolearn-1.0.8-py3-none-any.whl
Size 63.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e813c3422d45b42cca6775c6701e278aecd7f110f8dafc0051b08e3d3f0024f9
BLAKE2b-256 checksum
How to use checksums
c92298d9bab26909d007ebceafe863bc34f8042b67cb22e29f9a82382b24eda5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Security for evolearn 1.0.8

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