---
title: 'Interpretable ML - Học máy khả diễn giải'
tags: ML, Machine Learning
---
<!-- \begin{eqnarray}
\text{Precision} &=& \frac{\text{Dương tính đúng}}{\text{Dương tính đúng} +\text{Dương tính giả}} \
\text{Recall} &=& \frac{\text{Dương tính đúng}}{\text{Dương tính đúng} + \text{Âm tính giả}}
\end{eqnarray} -->
Interpretable ML - Học máy khả diễn giải
===
Reference links :
[Google Cloud XRAI](https://storage.googleapis.com/cloud-ai-whitepapers/AI%20Explainability%20Whitepaper.pdf)
[XAI from EthicalML](https://github.com/EthicalML/xai/blob/master/examples/XAI%20Tabular%20Data%20Example%20Usage.ipynb)
[Paper - Washington University](https://arxiv.org/pdf/1602.04938v3.pdf)
[Interpretable Machine Learning - Christophm Molnar](htps://christophm.github.io/interpretable-ml-book/lime.html)
[VNM version](https://github.com/giangnguyen2412/InterpretableMLBook-Vietnamese/blob/master/IML_bookv1.0.pdf)
[Colab notebook - Victor Dibia](https://colab.research.google.com/drive/1pjPzsw_uZew-Zcz646JTkRDhF2GkPk0N#scrollTo=I1SRBtGjI5mZ)
[Decrypting your Machine Learning model using LIME](https://towardsdatascience.com/decrypting-your-machine-learning-model-using-lime-5adc035109b5)
[Understanding Lime](https://towardsdatascience.com/understanding-how-lime-explains-predictions-d404e5d1829c)
[Idea behind Lime](https://towardsdatascience.com/idea-behind-lime-and-shap-b603d35d34eb)
[Lime confess](https://santiagof.medium.com/model-interpretability-making-your-model-confess-lime-89db7f70a72b)
## Table of Contents
[TOC]
What is interpretable ML?
---
> interpretability: “khả diễn giải là khi con người có thể hiểu được nguyên nhân của một kết quả”
Khi một mô hình học máy có thể giải thích quyết định của nó, ta có thể
kiểm tra những tiêu chí sau một cách dễ dàng:
1. Tính công bằng (Fairness): Đảm bảo rằng dự đoán không bị ảnh hưởng bởi sai lệch (bias) và có tình trạng “phân biệt đối xử” với các nhóm dữ liệu đặc biệt.
3. Tính riêng tư (Privacy): Đảm bảo rằng những thông tin nhạy cảm được bảo vệ.
4. Tính đáp ứng nhanh (Robustness): Đảm bảo rằng những thay đổi nhỏ ở đầu vào không ảnh hưởng lớn tới kết quả đầu ra.
5. Tính nhân quả (Causality): Đảm bảo rằng chỉ những mối quan hệ có tính nhân quả được sử dụng.
6. Tính tin cậy (Trust): Đảm bảo mô hình đáng tin cậy hơn mô hình hộp đen.
Methods
---
Thống kê tổng quan đặc trưng (Feature summary statistic) & Điểm dữ liệu (Data points)
Mô hình khả diễn giải nội tại (Intrinsically interpretable model)
Phạm vi
---
Phạm vi của interpretable
- Toàn cục hay cục bộ?
- Toàn cục tại cấp bộ module
Các mô hình khả diễn giải
---
BlackBox model: Neural Network, Deep learning...
WhiteBox model: Linear model, Tree-based model
*mô tả giải thích cục bộ mô hình non-linear*
<!--  -->

LIME
---
Ý tưởng dựa trên theo việc tập trung giải thích tập (các) điểm cục bộ thay vì global
Trên tập dữ liệu được xáo trộn, LIME sau đó huấn luyện một mô hình khả diễn giải bằng cách minimize loss các điểm dữ liệu được lấy mẫu với điểm dữ liệu lân cận
**Formula**: $\text{explanation}(x)=\arg\min_{g\in{}G}L(f,g,\pi_x)+\Omega(g)$
> $x$: điểm dữ liệu được xét
> $f$: original model
> $g$: explanation model (linear, tree,...)
> $\pi$: proximity measure từ x (có thể là $L2$ norm, $cosine$ ...)
> $\pi_x$: điểm dữ liệu được biến đổi
> $\Omega(g)$: độ phức tạp thuật toán. Eg: Độ sâu của tree với tree-based model, số lượng các feature trong linear model. Package lime mặc định trên python $\sqrt(len(features)) * 0.75$
**Các bước thực hiện**:
*Lấy mẫu cho điểm cục bộ*

> $x'$: Phiên bản binary vector của x
> $z'$(perturbed sample): một phần $x'$, noise added
> $f(z)$: label set
> $g(z')$: explanation model
- Lấy ngẫu nhiên nhiều $z'$ từ $x'$ hay nói cách khách lấy nhiều $z'$ từ 1 sample $x$ duy nhất
- Weighted $z'$ bởi $\pi(x)$ (focus vào những $z'$ gần $x$)
*Linear Explanation model loss function*

*LIME Algorithm*

- Lựa chọn mẫu mà ta quan tâm.
- Biến đổi tập dữ liệu và lưu lại dự đoán của mô hình hộp đen trên tập dữ liệu đã biến đổi này.
- Đánh trọng số các mẫu dữ liệu mới tương ứng với khoảng cách tới điểm dữ liệu ta đang quan tâm ở bước đầu.
- Huấn luyện một mô hình mang trọng số và khả diễn giải trên tập dữ
liệu đã được biến đổi.
- Giải thích dự đoán bằng cách diễn giải mô hình cục bộ.
```python
predict_fn_rf = lambda x: model2.predict_proba(x).astype(float)
X = X_train.values
explainer = lime.lime_tabular.LimeTabularExplainer(X,feature_names = X_train.columns,class_names=['Not Churn','Churn'])
choosen_instance = X_test.loc[[264886]].values[0]
exp = explainer.explain_instance(choosen_instance, predict_fn_rf,num_features=10)
exp.show_in_notebook(show_all=False)
```
*Ví dụ:*

> Câu hỏi: Chỉ huấn luyện trên 1 điểm bẳng cách nào?
> kernel_width: Chọn ntn, có công bằng nếu mọi điểm đều xáo trộn 1 đại lượng giống nhau? (defaults to sqrt (number of columns) * 0.75)
> Việc chọn các feature ảnh hưởng đến LIME như thế nào, giải pháp là chọn top feature importance?
**Ưu và nhược điểm của LIME**
- Giải thích dễ hiểu
- Làm việc dở với categorical feature
- Giải thích không ổn định. Ví dụ:
<!--  -->

lime-fail-1 https://christophm.github.io/interpretable-ml-book/images/lime-fail-1.png
*keywords: LimeTabularExplainer,perturb, sampling, Discretizer*
:bookmark: [Từ $x$, Lime biến đổi sang $x'$ ntn?](https://marcotcr.github.io/lime/tutorials/Tutorial%20-%20continuous%20and%20categorical%20features.html) , [github](https://github.com/marcotcr/lime/blob/a2c7a6fb70bce2e089cb146a31f483bf218875eb/lime/discretize.py#L175)
> "If the feature is numerical, we compute the mean and std, and discretize it into quartiles. If the feature is categorical, we compute the frequency of each value".
[addtional link](https://ema.drwhy.ai/LIME.html)
SHAP
---
*:link: Reference*
[Paper](https://proceedings.neurips.cc/paper/2017/file/8a20a8621978632d76c43dfd28b67767-Paper.pdf)
[Interpreting complex models with SHAP values - Gabriel Tseng](https://medium.com/@gabrieltseng/interpreting-complex-models-with-shap-values-1c187db6ec83)
[A Unified Approach to Interpreting Model Predictions](https://proceedings.neurips.cc/paper/2017/hash/8a20a8621978632d76c43dfd28b67767-Abstract.html)
[Wiki Shapley value](https://en.wikipedia.org/wiki/Shapley_value)
[Sample notebooks](https://slundberg.github.io/shap/notebooks/NHANES%20I%20Survival%20Model.html)
*Ví dụ*

| name | age | gender | occupation |
|-------|-----|--------|-------------|
| Frank | 56 | Male | |
| Bobby | 14 | Male | Game Tester |
| May | 52 | Female | Cooking |
| July | 13 | Female | |
Quay lại việc muốn giải thích một sample, ta phải tìm ϕ sao cho
*g_Frank* = *ϕFrankAge + ϕFrankGender + ϕFrankJob*
> p: prediction function
> g: interpretable function
> ϕ: coefficient của feature
Trong ví dụ trên, ta thấy ở mức độ global thì tuổi tác đóng vai trò là feature quan trọng (*có thể sử dụng hàm .getscore() trong RandomForrest*). Tuy nhiên sẽ thế nào nếu Frank - người làm nghề test game đã 50 tuổi vẫn yêu thích game. Khi đó xét thuộc tính age của Frank không còn quan trọng như Bob(14 tuổi) nữa, thay vào đó là occupation.
:key: Shapley: Idea từ game theory, xét đóng góp của feature trong payout của 1 player (hay 1 sample $x$).
**1. Tính chất của fair-game trong game theory**
- The sum of what everyone receives should equal the total reward
- If two people contributed the same value, then they should receive the same amount from the reward
- Someone who contributed no value should receive nothing
- If the group plays two games, then an individual’s reward from both games should equal their reward from their first game plus their reward from the second game
Trở lại ví dụ trên về Dự đoán yêu thích game (trong đó gọi *g_Frank* là... *p_Frank* là...)
- g_Frank = p_Frank.
- Nếu 2 features $i,j$ có đóng góp giống nhau trong kết quả dự đoán, thì ϕ phải bằng nhau
- Nếu feature không đóng góp gì(hoặc null-missing) trong dự đoán p , thì đóng góp của nó trong g cũng là 0
- g_(Frank+Bobby) = g_Frank + g_Bobby
**Từ tính chất game theory, ý tưởng tính importance của $xi$: Importance($xi$) = $p$(with $xi$) - $p$(without $xi$)**
:warning: Tuy nhiên, ***Do sự ảnh hưởng bởi thứ tự lấy feature mà giải thích local tại sample $x$ có thể khác nhau***

Quay lại ví dụ ban đầu với mô hình Tree. Giải thích với Bobby, ta xét age trước, gender sau. Khi đó
- gender là null, khi đó ϕ_age ($p(S)$) là (2+0.1)/2 = 1.05 => ϕ_gender = 2-1.05 = 0.95
Xét gender trước, age sau. Khi đó
- age là null, không có thuộc tính để chia root, mô hình lấy trung bình tất cả các leaf. Trung bình leaf tại depth_2 = (2+0.1)/2 = 1.05 và trung bình leaf tại depth_1 = (-1). vì vậy trung bình tất cả các leaf = (1.05 + (-1))/2 = 0.025 => ϕ_gender = 0.025 & ϕ_age = 2-0.025 = 1.975
Để giải quyết vấn đề này cần 1 phương pháp tính có weighted như Shapley
**Shapley Value**
$\phi_i(p)=\sum_{S\subseteq\{N/i\}}\frac{|S|!\left(n-|S|-1\right)!}{n!}\left(p\left(S \cup i\right)-p(S)\right)$
*Trong đó*
> $p$: giá trị prediction với sample $x$ (của mô hình ban đầu)
$\phi_i(p)$ : Value ảnh hưởng của feature $i$ lên dự đoán $p$
$S$: subset của các feature không có $i$ theo thứ tự (eg: N={gender, age,occu} => $S$_gender =[{age,occu},{occu,age},{age},{occu},{}])
$n$: số lượng feature
$p(S)$: giá trị prediction với tập subset $S$
Về cơ bản, Shapley xem xét có bao nhiêu hoán vị khác nhau của các tập hợp tồn tại. Trong đó bao gồm feature trong subset $S$ ( $|S|!$ ) và feature không trong $S$ ( $(n−∣S∣−1)!$ )
**Thực hành: Tính Shapley value**
Quay lại ví dụ tính Shapley value của age cho Bobby
> $N$: {age, gender}
> $S$: [ {} , {gender} ]
> $n$ = 2
1. Xét $S$ = {}
- $|S|$=0 , $|S|!=1$
- $p\left(S \cup i\right)-p(S)$ = $1.05-0.025 = 1.025$
- $\frac{|S|!\left(n-|S|-1\right)!}{n!}\left(p\left(S \cup i\right)-p(S)\right)$ = $\frac{1(2-1)!}{2!}(1.025)$ = $0.5125$
2. Xét $S$ = {gender}
- $|S|$=1 , $|S|!=1$
- $p\left(S \cup i\right)-p(S)$ = $2-0.025 = 1.975$
- $\frac{|S|!\left(n-|S|-1\right)!}{n!}\left(p\left(S \cup i\right)-p(S)\right)$ = $\frac{1(2-1)!}{2!}(1.975)$ = $0.9875$
At last, $ϕAgeBobby$ = $0.5125 + 0.9875 = 1.5$
SHAP (Shapley Additive exPlanations)
---
:link:
[SHAP python](https://shap.readthedocs.io/en/latest/index.html)
[SHAP Github](https://github.com/slundberg/shap#citations)
[Paper](https://www.nature.com/articles/s42256-019-0138-9.epdf?shared_access_token=RCYPTVkiECUmc0CccSMgXtRgN0jAjWel9jnR3ZoTv0O81kV8DqPb2VXSseRmof0Pl8YSOZy4FHz5vMc3xsxcX6uT10EzEoWo7B-nZQAHJJvBYhQJTT1LnJmpsa48nlgUWrMkThFrEIvZstjQ7Xdc5g%3D%3D)
:warning: Phạm vi chỉ nói về TreeExplainer, sử dụng giải thích các model tree-based (RandomForrest, XGBoost, LGBM... - các model có thể handle giá trị null value [link](https://stats.stackexchange.com/questions/98953/why-doesnt-random-forest-handle-missing-values-in-predictors)), tìm hiểu thêm tại [link](https://towardsdatascience.com/interpretable-neural-networks-45ac8aa91411)
*Triển khai trong Python*
```python
import shap
explain_data = X_test.sample(5000)
explainer = shap.TreeExplainer(model2)
shap_values = explainer.shap_values(explain_data)
shap.force_plot(explainer.expected_value[1], shap_values[1][0], subsampled_test_data[0], feature_names=X_test.columns)
```
| Variable | Description |
|-------------------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| model | the model to be explained |
| background_data | This is a required argument to KernelExplainer. Since most models aren’t designed to handle arbitrary missing data at test time, SHAP simulates a “missing” feature by replacing it with the values it takes in the background dataset. For small problems, this background dataset can be the whole training set, but for larger problems, it is suggested that a subsample of the training set (or the kmeans function to summarize the dataset) is used. Background data is optional for tree-based models. |

:information_source::closed_book: Mở rộng,
- Tìm hiểu về cách SHAP làm việc với mô hình k thể handle null value. Keywords: Integrated Gradients, DeepLift. [Link](https://towardsdatascience.com/interpretable-neural-networks-45ac8aa91411)
- Tìm hiểu cách SHAP giải thích global interpretable [Link](https://github.com/slundberg/shap)
