A package for calculating permutation importance and selecting features.
Project description
Permutation Feature Selector
Acknowledgements
This library is inspired by the functionality and design of Scikit-learn's permutation importance.
Links
PyPI:https://pypi.org/project/PermutationFeatureSelector/
Installation
To install the Library,
Standard Installation
You can install the package directly from PyPI using pip. This is the easiest and most common method:
$ pip install PermutationFeatureSelector
Installation from Source
If you prefer to install from the source or if you want the latest version that may not yet be released on PyPI, you can use the following commands:
$ git clone https://github.com/Itsuki-2822/PermutationFeatureSelector.git
$ cd PermutationFeatureSelector
$ python setup.py install
For developer
If you are a contributor or if you want to install the latest development version of the library, use the following command to install directly from the GitHub repository:
$ pip install --upgrade git+https://github.com/Itsuki-2822/PermutationFeatureSelector
What Permutation Importance
Basic Concept
The calculation of permutation importance proceeds through the following steps:
-
Evaluate Model Performance:
- Measure the performance metric (e.g., accuracy or error) of the model using the original dataset before any permutation.
-
Shuffle the Feature:
- Randomly shuffle the order of values in one feature of the dataset. This disrupts the relationship between that feature and the target variable.
-
Re-evaluate Performance:
- Assess the model's performance again, using the dataset with the shuffled feature.
-
Calculate Importance:
- Compute the difference in performance before and after the permutation. A larger difference indicates that the feature is more "important."
Model-Independent Advantage
Permutation importance is independent of the internal mechanisms of any specific model, which means it does not rely on the evaluation mechanisms specific to models like gradient boosting or decision trees. It can be applied across various predictive models (linear models, decision trees, neural networks, etc.)
Considerations for Use
-
Randomness:
- Since the feature shuffling is a random process, the results may vary slightly each time. To obtain stable evaluations, averaging several assessments is recommended.
-
Correlated Features:
- If multiple features are strongly correlated, their importance may be underestimated. Addressing this issue may require careful feature selection and engineering.
Examples
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_diabetes
import lightgbm as lgb
import matplotlib.pyplot as plt
from PermutationFeatureSelector import PermutationFeatureSelector
data = load_diabetes()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = lgb.LGBMRegressor()
model.fit(X_train, y_train)
selector = PermutationFeatureSelector(model, X_test, y_test, metric='rmse', n_repeats=30, random_state=42)
# Calculation of permutation importance
perm_importance = selector.calculate_permutation_importance()
print(perm_importance)
# Permutation Importance Plot
selector.plot_permutation_importance()
# Feature selection (e.g., select features with importance at least 1x the average)
chosen_features, chosen_features_df = selector.choose_feat(threshold_method='mean', threshold_value=1.0)
print(chosen_features)
print(chosen_features_df)
References
scikit-learn.org:
- https://scikit-learn.org/stable/modules/permutation_importance.html
- https://scikit-learn.org/stable/auto_examples/inspection/plot_permutation_importance.html#sphx-glr-auto-examples-inspection-plot-permutation-importance-py
- https://github.com/scikit-learn/scikit-learn/blob/2621573e6/sklearn/inspection/_permutation_importance.py#L111
medium.com:
hacarus.github.io:
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
Built Distribution
File details
Details for the file permutation_feature_selector-0.1.9.tar.gz
.
File metadata
- Download URL: permutation_feature_selector-0.1.9.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ba6742cd6f1efb022ece40e06de5a0f171e61970aff955da2014712e52a8ee0 |
|
MD5 | 51523ea1d40a9dca56ae18dc2198edfb |
|
BLAKE2b-256 | 138936830bec4fee3f3fc42825410897090bc76d154817efe96d874a96b51c4d |
File details
Details for the file permutation_feature_selector-0.1.9-py3-none-any.whl
.
File metadata
- Download URL: permutation_feature_selector-0.1.9-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccfe1b2be6369b83f5ff85ae68b7a7e55f7e440c2a258c90a4413427609b4b5a |
|
MD5 | 7804aaec6e1d1a1e4a20fad4b2e286d8 |
|
BLAKE2b-256 | 1e50019c14f56b29042a61f2790e627b104a8a90477c539f0d412c3b1c4e891b |