Developing Trust in Machine Learning Models Predictions | by Renu Khandelwal | Towards Data Science

What if I tell you to invest $100,000 in a particular stock today as my machine learning model is predicting a high return. The question you will ask me will be to explain the basis of my prediction, as this is a big decision for you. You do not want to gamble but would like to make informed decisions based on data.
To trust the machine learning model’s prediction, you would ask the following questions.
How did the model make predictions?
Why did the model make the specific prediction for a particular instance?
Which features impact the predictions the most and the least with an easy to understand explanation.
The explanation of the model should present a textual or visual qualitative relationship between the features and the model’s prediction.
In this post, you will learn to interpret the machine learning model’s prediction using LIME and explain the features that contribute the most towards making the prediction.
There are several techniques to interpret Machine Learning models, but here we will discuss LIME(Local Interpretable Model-agnostics Explanation).
What is LIME?
LIME is Local Interpretable Model-agnostic Explanation of a machine learning model that gives you an understanding of individual prediction,
LIME can explain individual predictions of a machine learning model rather than explaining the entire dataset.
It is locally faithful and has local fidelity, implying globally essential features may not be as crucial in the local context and vice versa. It does this by approximating it locally with an interpretable model.
It is model agnostic and can be applied to a classifier as well as a regression-based Machine Learning algorithm.
How does LIME work?
LIME perturbs the input dataset to see the change in the predictions. These perturbations are based on the variance of the data.
It creates an interpretable representation that is trained on small perturbations of the input data, which provides a good local approximation used for explaining individual prediction.
It creates a similarity score between the interpretable representation data and the original data to understand how similar they are
Makes the prediction using the model on the interpretable representation dataset.
It tries different combinations on the interpretable representation dataset to find the minimum set of features that gives the maximum likelihood to match the prediction made by the model on the original dataset.
The minimum set of features identified, which gives the most information of the original data along with similarity score, is used to explain the individual prediction.
What are the different Machine Learning models where LIME can be used?
LIME is model-agnostic and can be used for
Classification as well as Regression problems.
It can be used on text data using NLP.
Can be applied to Image data
LIME can also be applied to Tabular data where columns represent the features and rows are the individual instances.
How to apply LIME for classification based Machine Learning models?
The dataset used is the Default Credit Card Client Dataset.
For the classification Problem, a value of 1 implies the client will default payment, and a value of 0 is when the client will not default the credit card payment.
Loading the data and displaying the column names
import pandas as pdimport numpy as npnp.random.seed(0)df = pd.read_csv(r'c:\data\default of credit card clients.csv')df.columns
Create the Features and the Target variable
Y = df[['default payment next month']]X =  df[['LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_0', 'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2','BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1', 'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6']]
Splitting the dataset into train and test
from sklearn.model_selection import train_test_splitfrom sklearn import preprocessingX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.4)
Create the RandomForest Classifier and fit the train data
from sklearn.ensemble import RandomForestClassifiermodel = RandomForestClassifier(max_depth=6, random_state=0, n_estimators=10)model.fit(X_train, Y_train)
Display Feature Importance
importances = model.feature_importances_indices = np.argsort(importances)features = X_train.columnsplt.title('Feature Importances')plt.barh(range(len(indices)), importances[indices], color='b', align='center')plt.yticks(range(len(indices)), [features[i] for i in indices])plt.xlabel('Relative Importance')plt.show()
Create the LIME Tabular instance
As our dataset is tabular, we create an instance of LimeTabularExplainer(). training_data: Pass the training dataset,
mode: can be regression or classification. We are working on a classification problem, so we set the mode parameter to “classification.”
feature_names: All the input features in the dataset, used for computing statistics on each feature. Mean, and the standard deviation is computed for numerical feature whereas categorical features are discretized it into quartiles.
training_labels: pass the training target variable
import limefrom lime.lime_tabular import LimeTabularExplainerexplainer= LimeTabularExplainer(training_data=np.array(X_train),                                mode='classification',                                 feature_names=list(X_train.columns),                                training_labels=np.array(Y_train),                                random_state=12
Computed statistics generated from the training data is used
To create the perturbed sample instances, which is an interpretable representation dataset.To scale the data for computing distances for similarity scores when the attributes are not on the same scale.
To create the perturbed sample instances, which is an interpretable representation dataset.
To scale the data for computing distances for similarity scores when the attributes are not on the same scale.
Generate explanations for a prediction
You can now generate an explanation for a particular observation by passing the observation to the explain_instance()
data_row: specify the observation for which you need explanation
classifier_fn: need to specify the model prediction probability for classification models, which takes a numpy array and outputs prediction probabilities. For Regression models, we can specify the model prediction.
num_features: maximum number of features for which we need for the explanation
exp = explainer.explain_instance(X_test.iloc[0], model.predict_proba, num_features=8)exp.as_pyplot_figure()from matplotlib import pyplot as pltplt.tight_layout()
Display HTML explanation in a python notebook
exp.show_in_notebook(show_table=True, show_all=True)
How to interpret the results?
For the observation, the probability prediction is 87%, that the client will not default the credit card payment.
The reasons for the client not to default the credit card payment
The client has not defaulted on any payments for the current month, 2 ,3,4,5, and 6 month back (PAY_0, PAY_2, PAY_3, PAY_4, PAY_5 and PAY_6 is 0). The payment in the last three months is less than $1800
Let’s look at the explanation for another observation.
exp = explainer.explain_instance(X_test.iloc[731], model.predict_proba)exp.show_in_notebook(show_table=True, show_all=False)
For the observation, the probability prediction is 75%, that the client will default the credit card payment.
The reasons for the client to default the credit card payment
The client delayed the payments by two months for the current month, 2 ,3,4,5, and 6 month back (PAY_0, PAY_2, PAY_3, PAY_4, PAY_5 and PAY_6 is 2). The consumer credit is less than or equal to $500.
How to apply LIME for Regression-based Machine Learning models?
Dataset used is Real Estate Price Prediction.
Here we will use RandomForestRegressor.
#Importing required librariesimport pandas as pdimport numpy as npimport plotlynp.random.seed(0)import matplotlib.pyplot as pltimport limefrom lime.lime_tabular import LimeTabularExplainerfrom sklearn.model_selection import train_test_splitfrom sklearn import preprocessingfrom sklearn.ensemble import RandomForestRegressor#Reading the data filedf = pd.read_csv(r'c:\data\Real estate.csv')# Creating the input features and target variableY = df[['Y house price of unit area']]X =  df[[ 'X2 house age',       'X3 distance to the nearest MRT station',       'X4 number of convenience stores', 'X5 latitude', 'X6 longitude']]# check for missing dataX.isnull().sum()#Creating train and test dataX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2)#Random Forest Regressormodel = RandomForestRegressor(max_depth=50, random_state=0, n_estimators=1000, bootstrap=True)model.fit(X_train, Y_train)#printing feature importancesimportances = model.feature_importances_indices = np.argsort(importances)features = X_train.columnsplt.title('Feature Importances')plt.barh(range(len(indices)), importances[indices], color='b', align='center')plt.yticks(range(len(indices)), [features[i] for i in indices])plt.xlabel('Relative Importance')plt.show()
Create a LIME Tabular instance
Specify the mode as regression.
explainer= LimeTabularExplainer(training_data=np.array(X_train),                                mode='regression',                                 feature_names=list(X_train.columns),                                class_names=['Y house price of unit area'],                                random_state=12                                         )
Generate explanations for a prediction
exp = explainer.explain_instance(X_test.iloc[0], model.predict, num_features=4)exp.as_pyplot_figure()from matplotlib import pyplot as pltplt.tight_layout()
exp.show_in_notebook(show_table=True, show_all=False)
LIME is a model agnostic machine learning technique for explaining the prediction of the classification or regression-based model in a human-understandable format. LIME provides local interpretation for an observation. It works best with tabular data, images, and text.
Why Should I Trust You? Explaining the Predictions of Any Classifier