This article was published as a part of the Data Science Blogathon.

In this article, I will walk you through one technique that makes any machine learning model interpretable. Generally, there is a misconception that only linear machine learning models are more interpretable than others. The Model explainability helps in decision making and gives clients reliability.

** **

Explainability in AI is how much your features contribute or how important is your feature for the given output.

For Example,

If we have a linear model, feature importance is calculated by the magnitude of weights. If we have tree-based models, feature importance is calculated by Information gain or entropy. For deep learning models, we calculate using Integrated gradients.

Many of these AI systems are black-box, meaning we don’t understand how they work and why they make such decisions.

A Research Paper named **“Why Should I Trust You?”** published by Marco Tulio Ribeiro, Sameer Singh, and Carlos Guestrin from the University of Washington in 2016 Link explains two techniques.

The two techniques are:

- LIME (Local Interpretable Model-agnostic Explanations)
- SHAP (SHapley Additive exPlanations)

In this blog, we will cover the in-depth intuition of the LIME technique.

LIME aims to identify an interpretable model over the interpretable representation that is locally faithful to the classifier.

Source: https://arxiv.org/pdf/1602.04938.pdf

Local Interpretability answers the question, “Why is the model behaving in a specific way in the locality of a data point x ?”. LIME only looks at the local structure and not the global structure of the data.

Source: https://arxiv.org/pdf/1602.04938.pdf

The high-level intuition is to consider the dark red + named X from the above diagram and take the data points neighboring X. From the above image, we can see that points closer to X have higher weightage and the points which are farther have lower weightage. Sample a bunch of points near X and fit a surrogate model g() (consider g () as Linear Model). The surrogate model approximates the f() model in the neighborhood of X. f() is the final black-box model, were given an input x, we’ll get the output y.

Let’s get into the mathematical intuition; for every X, there is another X’, where X’ is d’ dimension interpretable binary vector. For text data, the interpretable vector X’ is the presence or absence of a word (i.e.) Bag of Words. For Image data, the interpretable vector X’ is the presence or absence of an image patch or superpixel. Superpixel is nothing but a contiguous patch of similar pixels. For tabular data, if the feature is categorical, then X’ is the one-hot encoding of that feature, or if the feature is real-valued, then we do feature binning. The surrogate model g ∈ G is any interpretable model like linear models or Decision trees. The Ω(g) measures the complexity of the surrogate model. For example, the complexity increases as the number of non-zero weights increases in the linear model. Similarly, if the depth of the decision tree increases, the model complexity increases. The g is the function of X’.

**Proximity Function**

Πx(Z) = proximity measure between X and Z

where Πx is the locality of X

**Local Fidelity and Interpretability**

Local Fidelity means in the neighborhood of X, the surrogate function g() should closely resemble f().

SOURCE: https://arxiv.org/pdf/1602.04938.pdf

The Loss function L(f, g, πx) shows how closely the function g() approximates f().

SOURCE: https://arxiv.org/pdf/1602.04938.pdf

Let Πx(z) = exp(−D(x, z)^{2} /σ^{2} ) be an exponential kernel.

g(z’) = w_{g} ·z’

z’ is an interpretable feature corresponding to z.

Ω(g) represents K-Lasso, which means selecting the first K-features using Lasso Regularization.

SOURCE: https://arxiv.org/pdf/1602.04938.pdf

- Works well on Images, Text, and Tabular data.
- Model Agnostic
- K Lasso gives highly interpretable K features

- Determining the right neighborhood Πx(z).
- Determining the correct kernel Width σ.
- The distance metric D(x, z) does not work well for high-dimensional data.

Install LIME using

pip install lime

The data is split into train and test

from sklearn.model_selection import train_test_split X = wine.drop('quality', axis=1) y = wine['quality'] X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42)

For this example, we are using Random Forest Classifier.

from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier(random_state=42) model.fit(X_train, y_train) score = model.score(X_test, y_test)

The Lime Library has a module named **lime_tabular,** which creates a tabular Explainer object. The parameters are:

- training_data – Training data must be in a Numpy array format.
- feature_names – Column names from the training set
- class_names – Distinct classes from the target variable
- mode – Classification in our Case

import lime from lime import lime_tabular explainer = lime_tabular.LimeTabularExplainer( training_data=np.array(X_train), feature_names=X_train.columns, class_names=['bad', 'good'], mode='classification')

The LIME library has an **explain_instance** function of the **explainer **object. The parameters required are:

- data_row – a single data point from the dataset
- predict_fn – a function used to make predictions

exp = explainer.explain_instance( data_row=X_test.iloc[1], predict_fn=model.predict_proba) exp.show_in_notebook(show_table=True)

The **show_in_notebook** is used for data visualization

The above diagrams show that the random forest classifier is 81% confident that the data point belongs to the “bad” class. The values of alcohol, sulfates, and total sulfur dioxide elevate the chances of the data point being “bad.” There are different visualization techniques available in the LIME library.

- Congratulations! You’ve learned one of the techniques of the XAI Framework. The applications of Explainable AI(XAI) frameworks will continue to grow, enabling us to develop more robust and complex solutions in a ‘White-Box’ approach.
- XAI will enable both data scientists and stakeholders to understand complex data predictions. The LIME library also provides great visualization. By building such an interpretable model,s you gain acceptance and trust from the stakeholders.
- Besides LIME, examples of other explainable AI tools like IBM AIX 360, What-if Tool, and Shap can help increase the interpretability of the data and machine learning models.

**The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.**

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Become a full stack data scientist
##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

##

Understanding Cost Function
Understanding Gradient Descent
Math Behind Gradient Descent
Assumptions of Linear Regression
Implement Linear Regression from Scratch
Train Linear Regression in Python
Implementing Linear Regression in R
Diagnosing Residual Plots in Linear Regression Models
Generalized Linear Models
Introduction to Logistic Regression
Odds Ratio
Implementing Logistic Regression from Scratch
Introduction to Scikit-learn in Python
Train Logistic Regression in python
Multiclass using Logistic Regression
How to use Multinomial and Ordinal Logistic Regression in R ?
Challenges with Linear Regression
Introduction to Regularisation
Implementing Regularisation
Ridge Regression
Lasso Regression

Introduction to Stacking
Implementing Stacking
Variants of Stacking
Implementing Variants of Stacking
Introduction to Blending
Bootstrap Sampling
Introduction to Random Sampling
Hyper-parameters of Random Forest
Implementing Random Forest
Out-of-Bag (OOB) Score in the Random Forest
IPL Team Win Prediction Project Using Machine Learning
Introduction to Boosting
Gradient Boosting Algorithm
Math behind GBM
Implementing GBM in python
Regularized Greedy Forests
Extreme Gradient Boosting
Implementing XGBM in python
Tuning Hyperparameters of XGBoost in Python
Implement XGBM in R/H2O
Adaptive Boosting
Implementing Adaptive Boosing
LightGBM
Implementing LightGBM in Python
Catboost
Implementing Catboost in Python

Introduction to Clustering
Applications of Clustering
Evaluation Metrics for Clustering
Understanding K-Means
Implementation of K-Means in Python
Implementation of K-Means in R
Choosing Right Value for K
Profiling Market Segments using K-Means Clustering
Hierarchical Clustering
Implementation of Hierarchial Clustering
DBSCAN
Defining Similarity between clusters
Build Better and Accurate Clusters with Gaussian Mixture Models

Introduction to Machine Learning Interpretability
Framework and Interpretable Models
model Agnostic Methods for Interpretability
Implementing Interpretable Model
Understanding SHAP
Out-of-Core ML
Introduction to Interpretable Machine Learning Models
Model Agnostic Methods for Interpretability
Game Theory & Shapley Values

Deploying Machine Learning Model using Streamlit
Deploying ML Models in Docker
Deploy Using Streamlit
Deploy on Heroku
Deploy Using Netlify
Introduction to Amazon Sagemaker
Setting up Amazon SageMaker
Using SageMaker Endpoint to Generate Inference
Deploy on Microsoft Azure Cloud
Introduction to Flask for Model
Deploying ML model using Flask