Implementing explainability

Local Interpretable Model-Agnostic Explanations (LIME) is a model-agnostic approach that can explain individual predictions made by a trained model. Being model-agnostic, it can explain the predictions of most types of trained machine learning models.

LIME explains decisions by inducing small changes to the input for each instance. It can gather the effects on the local decision boundary for that instance. It iterates over the loop to provide details for each variable. Looking at the output, we can see which variable has the most influence on that instance. 

Let's see how we can use LIME to make the individual predictions of our house price model explainable:

  1. If you have never used LIME before, you need to install the package using pip:
!pip install lime
  1. Then, let's import the Python packages that we need:
import sklearn as sk
import numpy as np
from lime.lime_tabular import LimeTabularExplainer as ex
  1. We will train a model that can predict housing prices in a particular city. For that we will first import the dataset that is stored in the housing.pkl file. Then, we will explore the features it has:

Based on these features we need to predict the price of a home.

  1. Now, let's train the model. We will be using a random forest regressor to train the model. First we divide the data into testing and training partitions and then we using it to train the model:
from sklearn.ensemble import RandomForestRegressor
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
housing.data, housing.target)

regressor = RandomForestRegressor()
regressor.fit(X_train, y_train)
  1. Next, let us identify the category columns:
cat_col = [i for i, col in enumerate(housing.data.T)
if np.unique(col).size < 10]
  1. Now, let's instantiate the LIME explainer with the required configuration parameters. Note that we are specifying that our label is 'price', representing the prices of houses in Boston:
myexplainer = ex(X_train,
feature_names=housing.feature_names,
class_names=['price'],
categorical_features=cat_col,
mode='regression')
  1. Let us try to look into the details of predictions. For that first let us import the pyplot as the plotter from matplotlib
exp = myexplainer.explain_instance(X_test[25], regressor.predict,
num_features=10)

exp.as_pyplot_figure()
from matplotlib import pyplot as plt
plt.tight_layout()
  1. As the LIME explainer works on individual predictions, we need to choose the predictions we want to analyze. We have asked the explainer for its justification of the predictions indexed as 1 and 35:

Let's try to analyze the preceding explanation by LIME, which tells us the following:

  • The list of features used in the individual predictions: They are indicated on the y-axis in the preceding screenshot.
  • The relative importance of the features in determining the decision: The larger the bar line, the greater the importance is. The value of the number is on the x-axis.
  • The positive or negative influence of each of the input features on the label: Red bars show a negative influence and green bars show the positive influence of a particular feature.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset