Generating Permutation-Based Feature Importance

  • Share this:

Code introduction


This function uses the PermutationImportance from the Eli5 library to calculate the feature importance for a given dataset. It does so by shuffling the feature values and retraining the model, then comparing the change in model performance to evaluate the importance of each feature.


Technology Stack : Eli5, scikit-learn, NumPy, Pandas

Code Type : Machine learning

Code Difficulty : Intermediate


                
                    
import numpy as np
import pandas as pd
from eli5.sklearn import PermutationImportance

def random_feature_importance(X, y):
    """
    This function generates a permutation importance of features for a given dataset.
    """
    model = PermutationImportance().fit(X, y)
    return model.feature_importances_