Calculating Feature Importance with PermutationImportance in eli5

  • Share this:

Code introduction


This function calculates the feature importance using PermutationImportance from the eli5 library. First, it creates a PermutationImportance object with the given model, then fits the object to the data, and finally retrieves the importance scores for each feature. The feature names and importance scores are stored in a DataFrame and returned.


Technology Stack : eli5, PermutationImportance, sklearn

Code Type : Data science

Code Difficulty : Intermediate


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

def feature_importance(model, X, y):
    """
    This function calculates the feature importance using PermutationImportance from eli5 library.
    """
    # Create a PermutationImportance object with the given model
    perm = PermutationImportance(model, random_state=42)
    
    # Fit the PermutationImportance object to the data
    perm.fit(X, y)
    
    # Get the importance scores for each feature
    importance_scores = perm.feature_importances_
    
    # Get the feature names
    feature_names = X.columns
    
    # Create a DataFrame to store the importance scores and feature names
    importance_df = pd.DataFrame({'Feature': feature_names, 'Importance': importance_scores})
    
    return importance_df