# Nature-Inspired Algorithms for scikit-learn
[![CI](https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms/workflows/CI/badge.svg?branch=master)](https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms/actions?query=workflow:CI+branch:master)
[![Maintainability](https://api.codeclimate.com/v1/badges/ed99e5c765bf5c95d716/maintainability)](https://codeclimate.com/github/timzatko/Sklearn-Nature-Inspired-Algorithms/maintainability)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sklearn-nature-inspired-algorithms)
[![PyPI version](https://badge.fury.io/py/sklearn-nature-inspired-algorithms.svg)](https://pypi.org/project/sklearn-nature-inspired-algorithms/)
[![PyPI downloads](https://img.shields.io/pypi/dm/sklearn-nature-inspired-algorithms)](https://pypi.org/project/sklearn-nature-inspired-algorithms/)
[![Fedora package](https://img.shields.io/fedora/v/python3-sklearn-nature-inspired-algorithms?color=blue&label=Fedora%20Linux&logo=fedora)](https://src.fedoraproject.org/rpms/python-sklearn-nature-inspired-algorithms)
Nature-inspired algorithms for hyper-parameter tuning of [scikit-learn](https://github.com/scikit-learn/scikit-learn) models. This package uses algorithms implementation from [NiaPy](https://github.com/NiaOrg/NiaPy).
## Installation
```shell script
$ pip install sklearn-nature-inspired-algorithms
```
To install this package on Fedora, run:
```sh
$ dnf install python3-sklearn-nature-inspired-algorithms
```
## Usage
The usage is similar to using sklearn's `GridSearchCV`. Refer to the [documentation](https://sklearn-nature-inspired-algorithms.readthedocs.io/en/stable/) for more detailed guides and more examples.
```python
from sklearn_nature_inspired_algorithms.model_selection import NatureInspiredSearchCV
from sklearn.ensemble import RandomForestClassifier
param_grid = {
'n_estimators': range(20, 100, 20),
'max_depth': range(2, 40, 2),
'min_samples_split': range(2, 20, 2),
'max_features': ["auto", "sqrt", "log2"],
}
clf = RandomForestClassifier(random_state=42)
nia_search = NatureInspiredSearchCV(
clf,
param_grid,
algorithm='hba', # hybrid bat algorithm
population_size=50,
max_n_gen=100,
max_stagnating_gen=10,
runs=3,
random_state=None, # or any number if you want same results on each run
)
nia_search.fit(X_train, y_train)
# the best params are stored in nia_search.best_params_
# finally you can train your model with best params from nia search
new_clf = RandomForestClassifier(**nia_search.best_params_, random_state=42)
```
Also you plot the search process with _line plot_ or _violin plot_.
```python
from sklearn_nature_inspired_algorithms.helpers import score_by_generation_lineplot, score_by_generation_violinplot
# line plot will plot all of the runs, you can specify the metric to be plotted ('min', 'max', 'median', 'mean')
score_by_generation_lineplot(nia_search, metric='max')
# in violin plot you need to specify the run to be plotted
score_by_generation_violinplot(nia_search, run=0)
```
Jupyter notebooks with full examples are available in [here](examples/notebooks).
### Using a Custom Nature-Inspired Algorithm
If you do not want to use any of the pre-defined algorithm configurations, you can use any algorithm from the [NiaPy](https://github.com/NiaOrg/NiaPy) collection.
This will allow you to have more control of the algorithm behavior.
Refer to their [documentation](https://niapy.readthedocs.io/en/latest/) and [examples](https://github.com/NiaOrg/NiaPy/tree/master/examples) for the usage.
__Note:__ Use version >2.x.x of NiaPy package
```python
from niapy.algorithms.basic import GeneticAlgorithm
algorithm = GeneticAlgorithm() # when custom algorithm is provided random_state is ignored
algorithm.set_parameters(NP=50, Ts=5, Mr=0.25)
nia_search = NatureInspiredSearchCV(
clf,
param_grid,
algorithm=algorithm,
population_size=50,
max_n_gen=100,
max_stagnating_gen=20,
runs=3,
)
nia_search.fit(X_train, y_train)
```
## Contributing
Detailed information on the contribution guidelines are in the [CONTRIBUTING.md](./CONTRIBUTING.md).
Raw data
{
"_id": null,
"home_page": "https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms",
"name": "sklearn-nature-inspired-algorithms",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8.1,<=3.12",
"maintainer_email": "",
"keywords": "sklearn,scikit-learn,nature-inspired-algorithms,hyper-parameter-tuning",
"author": "Timotej Zatko",
"author_email": "timi.zatko@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/71/1a/34331491748a8a19e11edb4e5de9e70027ad5a3f649e56cf6009cd7380af/sklearn_nature_inspired_algorithms-0.12.0.tar.gz",
"platform": null,
"description": "# Nature-Inspired Algorithms for scikit-learn\n\n[![CI](https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms/workflows/CI/badge.svg?branch=master)](https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms/actions?query=workflow:CI+branch:master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/ed99e5c765bf5c95d716/maintainability)](https://codeclimate.com/github/timzatko/Sklearn-Nature-Inspired-Algorithms/maintainability)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sklearn-nature-inspired-algorithms)\n[![PyPI version](https://badge.fury.io/py/sklearn-nature-inspired-algorithms.svg)](https://pypi.org/project/sklearn-nature-inspired-algorithms/)\n[![PyPI downloads](https://img.shields.io/pypi/dm/sklearn-nature-inspired-algorithms)](https://pypi.org/project/sklearn-nature-inspired-algorithms/)\n[![Fedora package](https://img.shields.io/fedora/v/python3-sklearn-nature-inspired-algorithms?color=blue&label=Fedora%20Linux&logo=fedora)](https://src.fedoraproject.org/rpms/python-sklearn-nature-inspired-algorithms)\n\nNature-inspired algorithms for hyper-parameter tuning of [scikit-learn](https://github.com/scikit-learn/scikit-learn) models. This package uses algorithms implementation from [NiaPy](https://github.com/NiaOrg/NiaPy). \n\n## Installation\n\n```shell script\n$ pip install sklearn-nature-inspired-algorithms\n```\n\nTo install this package on Fedora, run:\n\n```sh\n$ dnf install python3-sklearn-nature-inspired-algorithms\n```\n\n## Usage\n\nThe usage is similar to using sklearn's `GridSearchCV`. Refer to the [documentation](https://sklearn-nature-inspired-algorithms.readthedocs.io/en/stable/) for more detailed guides and more examples.\n\n```python\nfrom sklearn_nature_inspired_algorithms.model_selection import NatureInspiredSearchCV\nfrom sklearn.ensemble import RandomForestClassifier\n\nparam_grid = { \n 'n_estimators': range(20, 100, 20), \n 'max_depth': range(2, 40, 2),\n 'min_samples_split': range(2, 20, 2), \n 'max_features': [\"auto\", \"sqrt\", \"log2\"],\n}\n\nclf = RandomForestClassifier(random_state=42)\n\nnia_search = NatureInspiredSearchCV(\n clf,\n param_grid,\n algorithm='hba', # hybrid bat algorithm\n population_size=50,\n max_n_gen=100,\n max_stagnating_gen=10,\n runs=3,\n random_state=None, # or any number if you want same results on each run\n)\n\nnia_search.fit(X_train, y_train)\n\n# the best params are stored in nia_search.best_params_\n# finally you can train your model with best params from nia search\nnew_clf = RandomForestClassifier(**nia_search.best_params_, random_state=42)\n```\n\nAlso you plot the search process with _line plot_ or _violin plot_.\n\n```python\nfrom sklearn_nature_inspired_algorithms.helpers import score_by_generation_lineplot, score_by_generation_violinplot\n\n# line plot will plot all of the runs, you can specify the metric to be plotted ('min', 'max', 'median', 'mean')\nscore_by_generation_lineplot(nia_search, metric='max')\n\n# in violin plot you need to specify the run to be plotted\nscore_by_generation_violinplot(nia_search, run=0)\n```\n\nJupyter notebooks with full examples are available in [here](examples/notebooks).\n\n### Using a Custom Nature-Inspired Algorithm\n\nIf you do not want to use any of the pre-defined algorithm configurations, you can use any algorithm from the [NiaPy](https://github.com/NiaOrg/NiaPy) collection.\nThis will allow you to have more control of the algorithm behavior. \nRefer to their [documentation](https://niapy.readthedocs.io/en/latest/) and [examples](https://github.com/NiaOrg/NiaPy/tree/master/examples) for the usage. \n\n__Note:__ Use version >2.x.x of NiaPy package\n\n```python\nfrom niapy.algorithms.basic import GeneticAlgorithm\n\nalgorithm = GeneticAlgorithm() # when custom algorithm is provided random_state is ignored\nalgorithm.set_parameters(NP=50, Ts=5, Mr=0.25)\n\nnia_search = NatureInspiredSearchCV(\n clf,\n param_grid,\n algorithm=algorithm,\n population_size=50,\n max_n_gen=100,\n max_stagnating_gen=20,\n runs=3,\n)\n\nnia_search.fit(X_train, y_train)\n```\n\n## Contributing \n\nDetailed information on the contribution guidelines are in the [CONTRIBUTING.md](./CONTRIBUTING.md).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Search using nature inspired algorithms over specified parameter values for an sklearn estimator.",
"version": "0.12.0",
"project_urls": {
"Homepage": "https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms",
"Repository": "https://github.com/timzatko/Sklearn-Nature-Inspired-Algorithms"
},
"split_keywords": [
"sklearn",
"scikit-learn",
"nature-inspired-algorithms",
"hyper-parameter-tuning"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8eb1e6798b51f01d8663d9b34ef0f12d995fe168ecd30eb7d65926c5cc46033f",
"md5": "59e223abaa12fd82a0fae10c85c5d6a7",
"sha256": "fb7d1a1e19b2d23d60d5f028d54fa7150737e887422829db9e2bcaefc909cefc"
},
"downloads": -1,
"filename": "sklearn_nature_inspired_algorithms-0.12.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "59e223abaa12fd82a0fae10c85c5d6a7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.1,<=3.12",
"size": 10081,
"upload_time": "2023-08-05T18:50:41",
"upload_time_iso_8601": "2023-08-05T18:50:41.011085Z",
"url": "https://files.pythonhosted.org/packages/8e/b1/e6798b51f01d8663d9b34ef0f12d995fe168ecd30eb7d65926c5cc46033f/sklearn_nature_inspired_algorithms-0.12.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "711a34331491748a8a19e11edb4e5de9e70027ad5a3f649e56cf6009cd7380af",
"md5": "bb3f2ef7edb6dfab1c40a546a065dd29",
"sha256": "60dbf6092133231948fc9d1cb6e707c23739a3294261535c7e3e9caf9990c700"
},
"downloads": -1,
"filename": "sklearn_nature_inspired_algorithms-0.12.0.tar.gz",
"has_sig": false,
"md5_digest": "bb3f2ef7edb6dfab1c40a546a065dd29",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8.1,<=3.12",
"size": 8056,
"upload_time": "2023-08-05T18:50:42",
"upload_time_iso_8601": "2023-08-05T18:50:42.920191Z",
"url": "https://files.pythonhosted.org/packages/71/1a/34331491748a8a19e11edb4e5de9e70027ad5a3f649e56cf6009cd7380af/sklearn_nature_inspired_algorithms-0.12.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-05 18:50:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "timzatko",
"github_project": "Sklearn-Nature-Inspired-Algorithms",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "sklearn-nature-inspired-algorithms"
}