Evaluating Model Feature Importance with PermutationImportance

  • Share this:

Code introduction


This function uses eli5's PermutationImportance to calculate the feature importance of a given model. It first creates a PermutationImportance object, then fits the training data, and finally returns the feature importance scores.


Technology Stack : eli5, sklearn

Code Type : Machine Learning Model Evaluation

Code Difficulty : Intermediate


                
                    
import eli5
from eli5.sklearn import PermutationImportance

def evaluate_model(model, X_train, y_train):
    """
    使用eli5库中的PermutationImportance来评估模型的重要性。
    """
    # 创建PermutationImportance对象
    perm = PermutationImportance(model, random_state=42)
    
    # 计算重要性得分
    perm.fit(X_train, y_train)
    
    # 获取重要性得分
    importances = perm.feature_importances_
    
    return importances                
              
Tags: