Simple genetic programming library using tree representation for individuals
Project description
# Evolutionary Algorithm \- Genetic Programming
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/amstuta/genetic.py/blob/master/LICENSE.md)
This package is a high-level library implementing a
genetic programming algorithm using a tree representation for individuals. It
is compatible with Python 2.7 and all the following versions and uses only the
standard library (no dependencies).
It can be used to solve linear and nonlinear regression problems and is
usable through a simple interface:
- A fit function that takes as inputs the training examples, target values and the number of iterations
- A predict function that takes as parameter an input for prediction
An example of usage can be found in the example.py script. This example requires to
have the libraries numpy and matplotlib installed.
### Installation
The package can be installed simply using Pypi:
```sh
pip install genetic
```
### Usage
To use it, you need to give the algorithm:
- A fitness function that takes as parameters:
- An instance of the class Tree
- The training examples: array-like object of shape [n_samples, n_features]
- The target values: array-like object of shape [n_samples]
- The function set that can be used in the tree
Basic example:
```python
from operator import add, sub, mul
from genetic.core import EvolutionaryAlgorithm
# Load your dataset
train_features, train_targets = ...
test_features, test_targets = ...
# Define a fitness function
def compute_fitness(tree, features, targets):
...
return fitness
# Define the parameters of the algorithm
parameters = {
'min_depth': 2,
'max_depth': 5,
'nb_trees': 50,
'max_const': 100,
'func_ratio': 0.5,
'var_ratio': 0.5,
'crossover_prob': 0.8,
'mutation_prob': 0.2,
'iterations': 80,
'functions': [add,sub,mul],
'fitness_function': compute_fitness
}
# Create object and train algorithm
ea = EvolutionaryAlgorithm(**parameters)
ea.fit(train_features, train_targets)
# Make a prediction
predicted = ea.predict(test_features[0])
```
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/amstuta/genetic.py/blob/master/LICENSE.md)
This package is a high-level library implementing a
genetic programming algorithm using a tree representation for individuals. It
is compatible with Python 2.7 and all the following versions and uses only the
standard library (no dependencies).
It can be used to solve linear and nonlinear regression problems and is
usable through a simple interface:
- A fit function that takes as inputs the training examples, target values and the number of iterations
- A predict function that takes as parameter an input for prediction
An example of usage can be found in the example.py script. This example requires to
have the libraries numpy and matplotlib installed.
### Installation
The package can be installed simply using Pypi:
```sh
pip install genetic
```
### Usage
To use it, you need to give the algorithm:
- A fitness function that takes as parameters:
- An instance of the class Tree
- The training examples: array-like object of shape [n_samples, n_features]
- The target values: array-like object of shape [n_samples]
- The function set that can be used in the tree
Basic example:
```python
from operator import add, sub, mul
from genetic.core import EvolutionaryAlgorithm
# Load your dataset
train_features, train_targets = ...
test_features, test_targets = ...
# Define a fitness function
def compute_fitness(tree, features, targets):
...
return fitness
# Define the parameters of the algorithm
parameters = {
'min_depth': 2,
'max_depth': 5,
'nb_trees': 50,
'max_const': 100,
'func_ratio': 0.5,
'var_ratio': 0.5,
'crossover_prob': 0.8,
'mutation_prob': 0.2,
'iterations': 80,
'functions': [add,sub,mul],
'fitness_function': compute_fitness
}
# Create object and train algorithm
ea = EvolutionaryAlgorithm(**parameters)
ea.fit(train_features, train_targets)
# Make a prediction
predicted = ea.predict(test_features[0])
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
genepy-1.0.0b1.tar.gz
(6.0 kB
view details)
Built Distribution
File details
Details for the file genepy-1.0.0b1.tar.gz
.
File metadata
- Download URL: genepy-1.0.0b1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 151011d9c63f75dfd9bbf058998486c72538637d7227c775352ef080671c27b2 |
|
MD5 | 5d979eb0fecab0aff1c090b9d86ab36e |
|
BLAKE2b-256 | 2dade26cb513ec9b8eb3614b252d84f2afab21be7911ae7bfe1c61452b5ff2c9 |
File details
Details for the file genepy-1.0.0b1-py2.py3-none-any.whl
.
File metadata
- Download URL: genepy-1.0.0b1-py2.py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f09bb546a02a28cad187101fa9c1d7901e0834de0cf0a976937cca106d8ac07 |
|
MD5 | 657ca41c7ddcbafe2a0bf25e4e67ac87 |
|
BLAKE2b-256 | e318e4f954b24f277ba6c437db2acd1cc6a279daf86b42052a2ba4228dd906e3 |