https://github.com/rsteca/sklearn-deap
Use evolutionary algorithms instead of gridsearch in scikit-learn
https://github.com/rsteca/sklearn-deap
Last synced: 8 months ago
JSON representation
Use evolutionary algorithms instead of gridsearch in scikit-learn
- Host: GitHub
- URL: https://github.com/rsteca/sklearn-deap
- Owner: rsteca
- License: mit
- Created: 2015-10-28T22:52:34.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-10T07:16:54.000Z (almost 2 years ago)
- Last Synced: 2025-03-02T21:37:53.871Z (9 months ago)
- Language: Jupyter Notebook
- Size: 108 KB
- Stars: 772
- Watchers: 29
- Forks: 129
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-production-machine-learning - sklearn-deap - deap.svg?style=social) Use evolutionary algorithms instead of gridsearch in scikit-learn. (AutoML)
- awesome-datascience - sklearn-deap
- awesome-python-machine-learning-resources - GitHub - 32% open · ⏱️ 30.07.2021): (超参数优化和AutoML)
- awesome-python-data-science - sklearn-deap - Use evolutionary algorithms instead of gridsearch in scikit-learn. <img height="20" src="img/sklearn_big.png" alt="sklearn"> (Optimization / NLP)
- fucking-awesome-datascience - sklearn-deap
README
# sklearn-deap
Use evolutionary algorithms instead of gridsearch in scikit-learn. This allows you to reduce the time required to find the best parameters for your estimator. Instead of trying out every possible combination of parameters, evolve only the combinations that give the best results.
[Here](https://github.com/rsteca/sklearn-deap/blob/master/test.ipynb) is an ipython notebook comparing EvolutionaryAlgorithmSearchCV against GridSearchCV and RandomizedSearchCV.
It's implemented using deap library: https://github.com/deap/deap
Install
-------
To install the library use pip:
pip install sklearn-deap
or clone the repo and just type the following on your shell:
python setup.py install
Usage examples
--------------
Example of usage:
```python
import sklearn.datasets
import numpy as np
import random
data = sklearn.datasets.load_digits()
X = data["data"]
y = data["target"]
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
paramgrid = {"kernel": ["rbf"],
"C" : np.logspace(-9, 9, num=25, base=10),
"gamma" : np.logspace(-9, 9, num=25, base=10)}
random.seed(1)
from evolutionary_search import EvolutionaryAlgorithmSearchCV
cv = EvolutionaryAlgorithmSearchCV(estimator=SVC(),
params=paramgrid,
scoring="accuracy",
cv=StratifiedKFold(n_splits=4),
verbose=1,
population_size=50,
gene_mutation_prob=0.10,
gene_crossover_prob=0.5,
tournament_size=3,
generations_number=5,
n_jobs=4)
cv.fit(X, y)
```
Output:
Types [1, 2, 2] and maxint [0, 24, 24] detected
--- Evolve in 625 possible combinations ---
gen nevals avg min max
0 50 0.202404 0.10128 0.962716
1 26 0.383083 0.10128 0.962716
2 31 0.575214 0.155259 0.962716
3 29 0.758308 0.105732 0.976071
4 22 0.938086 0.158041 0.976071
5 26 0.934201 0.155259 0.976071
Best individual is: {'kernel': 'rbf', 'C': 31622.776601683792, 'gamma': 0.001}
with fitness: 0.976071229827
Example for maximizing just some function:
```python
from evolutionary_search import maximize
def func(x, y, m=1., z=False):
return m * (np.exp(-(x**2 + y**2)) + float(z))
param_grid = {'x': [-1., 0., 1.], 'y': [-1., 0., 1.], 'z': [True, False]}
args = {'m': 1.}
best_params, best_score, score_results, _, _ = maximize(func, param_grid, args, verbose=False)
```
Output:
```python
best_params = {'x': 0.0, 'y': 0.0, 'z': True}
best_score = 2.0
score_results = (({'x': 1.0, 'y': -1.0, 'z': True}, 1.1353352832366128),
({'x': -1.0, 'y': 1.0, 'z': True}, 1.3678794411714423),
({'x': 0.0, 'y': 1.0, 'z': True}, 1.3678794411714423),
({'x': -1.0, 'y': 0.0, 'z': True}, 1.3678794411714423),
({'x': 1.0, 'y': 1.0, 'z': True}, 1.1353352832366128),
({'x': 0.0, 'y': 0.0, 'z': False}, 2.0),
({'x': -1.0, 'y': -1.0, 'z': False}, 0.36787944117144233),
({'x': 1.0, 'y': 0.0, 'z': True}, 1.3678794411714423),
({'x': -1.0, 'y': -1.0, 'z': True}, 1.3678794411714423),
({'x': 0.0, 'y': -1.0, 'z': False}, 1.3678794411714423),
({'x': 1.0, 'y': -1.0, 'z': False}, 1.1353352832366128),
({'x': 0.0, 'y': 0.0, 'z': True}, 2.0),
({'x': 0.0, 'y': -1.0, 'z': True}, 2.0))
```