Skip to main content

An evolutionary (genetic) algorithm designed specifically for optimizing predictive models with integer, real, boolean, and categorical inputs.

Project description

evolutionary_algorithm

evolutionary-algorithm is a Python library adapted from https://pypi.org/project/geneticalgorithm/ with modifications for streamlining the fine-tuning of predictive models.

Random-search parameter optimization tends to be extremely sample-inefficient. This library attempts to address this sample efficiency issue by intelligently moving through parameter spaces to discover (locally or globally) optimimal solutions. Noteworthy improvements over existing Python implementations are the ability to pass a dictionary as input to the objective function, as well as a structured dictionary as the final output. The algorithm also handles categorical (Boolean and multilabel) data in a manner similar to Hyperopt (https://github.com/hyperopt/hyperopt).

Installation

The recommended installation process makes use of pip (or pip3):

pip install evolutionary-algorithm

A minimal example

An ideal use case is passing a list of parameters and parameter bounds directly to a scikit-learn model, unpacking the paramters as model arguments within the objective function, and receiving a set of fine-tuned parameters as a result, which can be unpacked directly for downstream usage.

# Dependencies
from evolutionary_algorithm import EvolutionaryAlgorithm as ea
from sklearn.linear_model import SGDClassifier
from sklearn import datasets
from sklearn.metrics import accuracy_score

# Sample dataset
X, y = datasets.load_iris(return_X_y=True, as_frame=False)

# Declare objective function parameters and bounds
objective_parameters = [
    {'name' : 'average',
     'bounds' : [0, 1],
     'type' : 'bool'},
    {'name' : 'alpha',
     'bounds' : [1e-5, 1e-1],
     'type' : 'float'},
    {'name' : 'loss',   
     'bounds' : ['hinge', 'log', 'squared_hinge', 'modified_huber', 'perceptron'],
     'type' : 'cat'},
    {'name' : 'penalty',
     'bounds' : ['l1', 'l2', 'elasticnet'], 
     'type' : 'cat'},
    {'name' : 'l1_ratio',
     'bounds' : [0, 1],
     'type' : 'float'}
]

# Define the objective function (with embedded predictive model)
def objective_function(args):

    clf = SGDClassifier(**args)
    clf.fit(X, y)
    preds = clf.predict(X)

    return accuracy_score(y, preds) * -1 # Expects a value to be minimized

# Create instance of EA object
evo_algo = ea(function=objective_function, 
           parameters=objective_parameters)

# Run EA
evo_algo.run()

# Access best model parameters
evo_algo.best_parameters

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

evolutionary_algorithm-0.0.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

evolutionary_algorithm-0.0.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file evolutionary_algorithm-0.0.2.tar.gz.

File metadata

  • Download URL: evolutionary_algorithm-0.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for evolutionary_algorithm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b17fb7ced14e147eb48f56f611233312d043c210b70faa21bd482fa2931943fd
MD5 793d5554ebf158b11cc3f2427a36cb56
BLAKE2b-256 93d85d661d9a176234a9aa179f17c2f588e5e8d0bd3b28daf7b0a04eafb8c2c3

See more details on using hashes here.

File details

Details for the file evolutionary_algorithm-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: evolutionary_algorithm-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for evolutionary_algorithm-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 43c00a27ab280417f296c2a202b8bd88f18ebe2f5e2f09b76e34f8da829d729c
MD5 c44b9dfca8eba36ab659eb39530b5e85
BLAKE2b-256 181fa99c29e49b95787795d045879c108835e79700acddea06cd15a540dd7046

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page