# boruta_py #
[![License](https://img.shields.io/github/license/scikit-learn-contrib/boruta_py)](https://github.com/scikit-learn-contrib/boruta_py/blob/master/LICENSE)
[![PyPI version](https://badge.fury.io/py/Boruta.svg)](https://badge.fury.io/py/Boruta)
[![Anaconda-Server Badge](https://anaconda.org/conda-forge/boruta_py/badges/version.svg)](https://anaconda.org/conda-forge/boruta_py)
This project hosts Python implementations of the [Boruta all-relevant feature selection method](https://www.jstatsoft.org/article/view/v036i11).
[Related blog post](https://danielhomola.com/feature%20selection/phd/borutapy-an-all-relevant-feature-selection-method/)
## How to install ##
Install with `pip`:
```shell
pip install Boruta
```
or with `conda`:
```shell
conda install -c conda-forge boruta_py
```
## Dependencies ##
* numpy
* scipy
* scikit-learn
## How to use ##
Download, import and do as you would with any other scikit-learn method:
* fit(X, y)
* transform(X)
* fit_transform(X, y)
## Description ##
Python implementations of the Boruta R package.
This implementation tries to mimic the scikit-learn interface, so use fit,
transform or fit_transform, to run the feature selection.
For more, see the docs of these functions, and the examples below.
[Original code](https://gitlab.com/mbq/Boruta) and method was authored by Miron B. Kursa.
Boruta is an all relevant feature selection method, while most other are
minimal optimal; this means it tries to find all features carrying
information usable for prediction, rather than finding a possibly compact
subset of features on which some classifier has a minimal error.
Why bother with all relevant feature selection?
When you try to understand the phenomenon that made your data, you should
care about all factors that contribute to it, not just the bluntest signs
of it in context of your methodology (yes, minimal optimal set of features
by definition depends on your classifier choice).
## What's different in BorutaPy? ##
It is the original R package recoded in Python with a few added extra features.
Some improvements include:
* Faster run times, thanks to scikit-learn
* Scikit-learn like interface
* Compatible with any ensemble method from scikit-learn
* Automatic n_estimator selection
* Ranking of features
* Feature importances are derived from Gini impurity instead of RandomForest R package's MDA.
For more details, please check the top of the docstring.
We highly recommend using pruned trees with a depth between 3-7.
Also, after playing around a lot with the original code I identified a few areas
where the core algorithm could be improved/altered to make it less strict and
more applicable to biological data, where the Bonferroni correction might be
overly harsh.
__Percentile as threshold__
The original method uses the maximum of the shadow features as a threshold in
deciding which real feature is doing better than the shadow ones. This could be
overly harsh.
To control this, I added the perc parameter, which sets the
percentile of the shadow features' importances, the algorithm uses as the
threshold. The default of 100 which is equivalent to taking the maximum as the
R version of Boruta does, but it could be relaxed. Note, since this is the
percentile, it changes with the size of the dataset. With several thousands of
features it isn't as stringent as with a few dozens at the end of a Boruta run.
__Two step correction for multiple testing__
The correction for multiple testing was relaxed by making it a two step
process, rather than a harsh one step Bonferroni correction.
We need to correct firstly because in each iteration we test a number of
features against the null hypothesis (does a feature perform better than
expected by random). For this the Bonferroni correction is used in the original
code which is known to be too stringent in such scenarios (at least for
biological data), and also the original code corrects for n features, even if
we are in the 50th iteration where we only have k<<n features left. For this
reason the first step of correction is the widely used Benjamini Hochberg FDR.
Following that however we also need to account for the fact that we have been
testing the same features over and over again in each iteration with the
same test. For this scenario the Bonferroni is perfect, so it is applied by
dividing the p-value threshold with the current iteration index.
If this two step correction is not required, the two_step parameter has to be
set to False, then (with perc=100) BorutaPy behaves exactly as the R version.
## Parameters ##
__estimator__ : object
> A supervised learning estimator, with a 'fit' method that returns the
> feature_importances_ attribute. Important features must correspond to
> high absolute values in the feature_importances_.
__n_estimators__ : int or string, default = 1000
> If int sets the number of estimators in the chosen ensemble method.
> If 'auto' this is determined automatically based on the size of the
> dataset. The other parameters of the used estimators need to be set
> with initialisation.
__perc__ : int, default = 100
> Instead of the max we use the percentile defined by the user, to pick
> our threshold for comparison between shadow and real features. The max
> tends to be too stringent. This provides a finer control over this. The
> lower perc is the more false positives will be picked as relevant but
> also the less relevant features will be left out. The usual trade-off.
> The default is essentially the vanilla Boruta corresponding to the max.
__alpha__ : float, default = 0.05
> Level at which the corrected p-values will get rejected in both correction
steps.
__two_step__ : Boolean, default = True
> If you want to use the original implementation of Boruta with Bonferroni
> correction only set this to False.
__max_iter__ : int, default = 100
> The number of maximum iterations to perform.
__verbose__ : int, default=0
> Controls verbosity of output.
## Attributes ##
**n_features_** : int
> The number of selected features.
**support_** : array of shape [n_features]
> The mask of selected features - only confirmed ones are True.
**support_weak_** : array of shape [n_features]
> The mask of selected tentative features, which haven't gained enough
> support during the max_iter number of iterations..
**ranking_** : array of shape [n_features]
> The feature ranking, such that ``ranking_[i]`` corresponds to the
> ranking position of the i-th feature. Selected (i.e., estimated
> best) features are assigned rank 1 and tentative features are assigned
> rank 2.
## Examples ##
[![Open example notebook](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/https://raw.githubusercontent.com/scikit-learn-contrib/boruta_py/master/boruta/examples/Madalon_Data_Set.ipynb)
[![Open example notebook](https://b.mineo.app/static/img/open_in_mineo.svg)](https://b.mineo.app/import/https://raw.githubusercontent.com/scikit-learn-contrib/boruta_py/master/boruta/examples/Madalon_Data_Set.ipynb)
```python
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy
# load X and y
# NOTE BorutaPy accepts numpy arrays only, hence the .values attribute
X = pd.read_csv('examples/test_X.csv', index_col=0).values
y = pd.read_csv('examples/test_y.csv', header=None, index_col=0).values
y = y.ravel()
# define random forest classifier, with utilising all cores and
# sampling in proportion to y labels
rf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5)
# define Boruta feature selection method
feat_selector = BorutaPy(rf, n_estimators='auto', verbose=2, random_state=1)
# find all relevant features - 5 features should be selected
feat_selector.fit(X, y)
# check selected features - first 5 features are selected
feat_selector.support_
# check ranking of features
feat_selector.ranking_
# call transform() on X to filter it down to selected features
X_filtered = feat_selector.transform(X)
```
## References ##
1. Kursa M., Rudnicki W., "Feature Selection with the Boruta Package" Journal of Statistical Software, Vol. 36, Issue 11, Sep 2010
Raw data
{
"_id": null,
"home_page": "https://github.com/danielhomola/boruta_py",
"name": "Boruta",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "feature selection, machine learning, random forest",
"author": "Daniel Homola",
"author_email": "dani.homola@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/83/ab/103f819f83e9e2833f95f3dfb5e6d162ad1616b136b96c683ad4d7de224c/Boruta-0.4.3.tar.gz",
"platform": null,
"description": "# boruta_py #\n\n[![License](https://img.shields.io/github/license/scikit-learn-contrib/boruta_py)](https://github.com/scikit-learn-contrib/boruta_py/blob/master/LICENSE)\n[![PyPI version](https://badge.fury.io/py/Boruta.svg)](https://badge.fury.io/py/Boruta)\n[![Anaconda-Server Badge](https://anaconda.org/conda-forge/boruta_py/badges/version.svg)](https://anaconda.org/conda-forge/boruta_py)\n\nThis project hosts Python implementations of the [Boruta all-relevant feature selection method](https://www.jstatsoft.org/article/view/v036i11).\n\n[Related blog post](https://danielhomola.com/feature%20selection/phd/borutapy-an-all-relevant-feature-selection-method/)\n\n\n## How to install ##\n\nInstall with `pip`:\n\n```shell\npip install Boruta\n```\n\nor with `conda`:\n\n```shell\nconda install -c conda-forge boruta_py\n```\n\n## Dependencies ##\n\n* numpy\n* scipy\n* scikit-learn\n\n## How to use ##\n\nDownload, import and do as you would with any other scikit-learn method:\n* fit(X, y)\n* transform(X)\n* fit_transform(X, y)\n\n## Description ##\n\nPython implementations of the Boruta R package.\n\nThis implementation tries to mimic the scikit-learn interface, so use fit,\ntransform or fit_transform, to run the feature selection.\n\nFor more, see the docs of these functions, and the examples below.\n\n[Original code](https://gitlab.com/mbq/Boruta) and method was authored by Miron B. Kursa.\n\nBoruta is an all relevant feature selection method, while most other are\nminimal optimal; this means it tries to find all features carrying\ninformation usable for prediction, rather than finding a possibly compact\nsubset of features on which some classifier has a minimal error.\n\nWhy bother with all relevant feature selection?\nWhen you try to understand the phenomenon that made your data, you should\ncare about all factors that contribute to it, not just the bluntest signs\nof it in context of your methodology (yes, minimal optimal set of features\nby definition depends on your classifier choice).\n\n\n## What's different in BorutaPy? ##\n\nIt is the original R package recoded in Python with a few added extra features.\nSome improvements include: \n\n* Faster run times, thanks to scikit-learn\n\n* Scikit-learn like interface\n\n* Compatible with any ensemble method from scikit-learn\n\n* Automatic n_estimator selection\n\n* Ranking of features\n\n* Feature importances are derived from Gini impurity instead of RandomForest R package's MDA. \n\nFor more details, please check the top of the docstring.\n\nWe highly recommend using pruned trees with a depth between 3-7.\n\nAlso, after playing around a lot with the original code I identified a few areas\nwhere the core algorithm could be improved/altered to make it less strict and\nmore applicable to biological data, where the Bonferroni correction might be\noverly harsh.\n\n__Percentile as threshold__ \nThe original method uses the maximum of the shadow features as a threshold in\ndeciding which real feature is doing better than the shadow ones. This could be\noverly harsh.\n\nTo control this, I added the perc parameter, which sets the\npercentile of the shadow features' importances, the algorithm uses as the\nthreshold. The default of 100 which is equivalent to taking the maximum as the\nR version of Boruta does, but it could be relaxed. Note, since this is the\npercentile, it changes with the size of the dataset. With several thousands of\nfeatures it isn't as stringent as with a few dozens at the end of a Boruta run.\n\n\n__Two step correction for multiple testing__ \nThe correction for multiple testing was relaxed by making it a two step\nprocess, rather than a harsh one step Bonferroni correction.\n\nWe need to correct firstly because in each iteration we test a number of\nfeatures against the null hypothesis (does a feature perform better than\nexpected by random). For this the Bonferroni correction is used in the original\ncode which is known to be too stringent in such scenarios (at least for\nbiological data), and also the original code corrects for n features, even if\nwe are in the 50th iteration where we only have k<<n features left. For this\nreason the first step of correction is the widely used Benjamini Hochberg FDR.\n\nFollowing that however we also need to account for the fact that we have been\ntesting the same features over and over again in each iteration with the\nsame test. For this scenario the Bonferroni is perfect, so it is applied by\ndividing the p-value threshold with the current iteration index.\n\nIf this two step correction is not required, the two_step parameter has to be\nset to False, then (with perc=100) BorutaPy behaves exactly as the R version.\n\n## Parameters ##\n\n__estimator__ : object\n > A supervised learning estimator, with a 'fit' method that returns the\n > feature_importances_ attribute. Important features must correspond to\n > high absolute values in the feature_importances_.\n\n__n_estimators__ : int or string, default = 1000\n > If int sets the number of estimators in the chosen ensemble method.\n > If 'auto' this is determined automatically based on the size of the\n > dataset. The other parameters of the used estimators need to be set\n > with initialisation.\n\n__perc__ : int, default = 100\n > Instead of the max we use the percentile defined by the user, to pick\n > our threshold for comparison between shadow and real features. The max\n > tends to be too stringent. This provides a finer control over this. The\n > lower perc is the more false positives will be picked as relevant but\n > also the less relevant features will be left out. The usual trade-off.\n > The default is essentially the vanilla Boruta corresponding to the max.\n\n__alpha__ : float, default = 0.05\n > Level at which the corrected p-values will get rejected in both correction\n steps.\n\n__two_step__ : Boolean, default = True\n > If you want to use the original implementation of Boruta with Bonferroni\n > correction only set this to False.\n\n__max_iter__ : int, default = 100\n > The number of maximum iterations to perform.\n\n__verbose__ : int, default=0\n > Controls verbosity of output.\n\n\n## Attributes ##\n\n**n_features_** : int\n > The number of selected features.\n\n**support_** : array of shape [n_features]\n > The mask of selected features - only confirmed ones are True.\n\n**support_weak_** : array of shape [n_features]\n > The mask of selected tentative features, which haven't gained enough\n > support during the max_iter number of iterations..\n\n**ranking_** : array of shape [n_features]\n > The feature ranking, such that ``ranking_[i]`` corresponds to the\n > ranking position of the i-th feature. Selected (i.e., estimated\n > best) features are assigned rank 1 and tentative features are assigned\n > rank 2.\n\n\n## Examples ##\n\n[![Open example notebook](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/https://raw.githubusercontent.com/scikit-learn-contrib/boruta_py/master/boruta/examples/Madalon_Data_Set.ipynb)\n[![Open example notebook](https://b.mineo.app/static/img/open_in_mineo.svg)](https://b.mineo.app/import/https://raw.githubusercontent.com/scikit-learn-contrib/boruta_py/master/boruta/examples/Madalon_Data_Set.ipynb)\n\n```python\nimport pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nfrom boruta import BorutaPy\n\n# load X and y\n# NOTE BorutaPy accepts numpy arrays only, hence the .values attribute\nX = pd.read_csv('examples/test_X.csv', index_col=0).values\ny = pd.read_csv('examples/test_y.csv', header=None, index_col=0).values\ny = y.ravel()\n\n# define random forest classifier, with utilising all cores and\n# sampling in proportion to y labels\nrf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5)\n\n# define Boruta feature selection method\nfeat_selector = BorutaPy(rf, n_estimators='auto', verbose=2, random_state=1)\n\n# find all relevant features - 5 features should be selected\nfeat_selector.fit(X, y)\n\n# check selected features - first 5 features are selected\nfeat_selector.support_\n\n# check ranking of features\nfeat_selector.ranking_\n\n# call transform() on X to filter it down to selected features\nX_filtered = feat_selector.transform(X)\n```\n\n## References ##\n\n1. Kursa M., Rudnicki W., \"Feature Selection with the Boruta Package\" Journal of Statistical Software, Vol. 36, Issue 11, Sep 2010\n",
"bugtrack_url": null,
"license": "BSD 3 clause",
"summary": "Python Implementation of Boruta Feature Selection",
"version": "0.4.3",
"project_urls": {
"Download": "https://github.com/danielhomola/boruta_py/tarball/0.1.5",
"Homepage": "https://github.com/danielhomola/boruta_py"
},
"split_keywords": [
"feature selection",
" machine learning",
" random forest"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "30de37bb80bba7fb7baa703b78fab37487b21b43fe4bb5d5a1ab09ecab9b76c6",
"md5": "ccc0f032468c81bd5dc221c9482d54ed",
"sha256": "58cc1d7f180d0ee3aace5b171a25f00b7fd8e323a749954d899cc983b2924085"
},
"downloads": -1,
"filename": "Boruta-0.4.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ccc0f032468c81bd5dc221c9482d54ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 57909,
"upload_time": "2024-08-13T19:54:37",
"upload_time_iso_8601": "2024-08-13T19:54:37.359404Z",
"url": "https://files.pythonhosted.org/packages/30/de/37bb80bba7fb7baa703b78fab37487b21b43fe4bb5d5a1ab09ecab9b76c6/Boruta-0.4.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "83ab103f819f83e9e2833f95f3dfb5e6d162ad1616b136b96c683ad4d7de224c",
"md5": "7ea22aae340ea778eb2871a266e2ca06",
"sha256": "abd67119bb88c7d1675b0dfc78072e50b5ba1d32b7cc7664258a640ad3a495a4"
},
"downloads": -1,
"filename": "Boruta-0.4.3.tar.gz",
"has_sig": false,
"md5_digest": "7ea22aae340ea778eb2871a266e2ca06",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 57656,
"upload_time": "2024-08-13T19:54:38",
"upload_time_iso_8601": "2024-08-13T19:54:38.755188Z",
"url": "https://files.pythonhosted.org/packages/83/ab/103f819f83e9e2833f95f3dfb5e6d162ad1616b136b96c683ad4d7de224c/Boruta-0.4.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-13 19:54:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "danielhomola",
"github_project": "boruta_py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "boruta"
}