Calculating Feature Importance with PermutationImportance

  • Share this:

Code introduction


This function uses the PermutationImportance class from the Eli5 library to calculate the feature importance of a given model on the data. It takes data, target, and model as inputs and returns the feature importance scores.


Technology Stack : Eli5, sklearn

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def random_feature_importance(data, target, model):
    from eli5.sklearn import PermutationImportance

    # Create a permutation importance model
    perm = PermutationImportance(model, random_state=1)
    
    # Fit the permutation importance model on the data
    perm.fit(data, target)
    
    # Get the feature importances
    importances = perm.feature_importances_
    
    return importances                
              
Tags: