Explaining Random Forest Predictions with SHAP

  • Share this:

Code introduction


This function uses a random forest model to fit the given data and uses the SHAP library to explain the model's predictions. It first creates a random forest model, then uses SHAP's TreeExplainer to explain the model's predictions. Finally, it uses SHAP's summary_plot function to generate a SHAP value summary plot.


Technology Stack : Packages and technologies used in the code[English]: NumPy, scikit-learn, SHAP, random forest, tree explainer, SHAP value summary plot

Code Type : The type of code

Code Difficulty : Advanced


                
                    
import numpy as np
import shap
from sklearn.ensemble import RandomForestClassifier

def predict_and_explain(X):
    # Create a random forest classifier
    model = RandomForestClassifier(n_estimators=100)
    
    # Fit the model to the data
    model.fit(X[:, :-1], X[:, -1])
    
    # Create a SHAP explainer
    explainer = shap.TreeExplainer(model)
    
    # Explain the prediction of the model on the given data
    shap_values = explainer.shap_values(X)
    
    # Calculate the expected value for the model
    expected_value = explainer.expected_value[-1]
    
    # Create a SHAP summary plot
    shap.summary_plot(shap_values, X, feature_names=[f'Feature {i}' for i in range(X.shape[1]-1)], max_display=10)
    
    return shap_values, expected_value