Random Forest Feature Importance Explanation using PermutationImportance

  • Share this:

Code introduction


This function uses the PermutationImportance from the ELI5 library to explain feature importance for a random forest model. It first creates a PermutationImportance object, then fits the model and calculates the feature importance scores. Finally, it generates an explanation using the explain_weights_df function from ELI5.


Technology Stack : eli5, sklearn, numpy, pandas

Code Type : The type of code

Code Difficulty : Intermediate


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

def random_forest_explanation(X, y, model):
    """
    Generate an explanation for a random forest model using PermutationImportance from the ELI5 library.
    """
    # Create a PermutationImportance object
    perm = PermutationImportance(model, random_state=42)
    
    # Fit the model and get the importance scores
    perm.fit(X, y)
    
    # Generate the top features and their importance
    top_features = perm.feature_importances_
    feature_names = X.columns.tolist()
    
    # Create a DataFrame to display the results
    df = pd.DataFrame({'Feature': feature_names, 'Importance': top_features})
    
    # Sort the DataFrame by importance in descending order
    df = df.sort_values(by='Importance', ascending=False)
    
    # Generate the explanation
    explanation = eli5.explain_weights_df(df, feature_names=feature_names)
    
    return explanation