Py之eli5:eli5库的简介、安装、使用方法之详细攻略
目录
2、eli5库实现了几种用于检查黑盒模型的算法(参见检查黑盒估计器)
3、eli5库支持的机器学习框架和包—可以和大多数通用的python机器学习工具包一起使用
ML之PFI(eli5):基于mpg汽车油耗数据集利用RF随机森林算法和PFI置换特征重要性算法实现模型特征可解释性排序
eli5库的简介
ELI5(Explain Like I'm Five,解释给五岁小孩听)是一个Python库,它可以帮助调试机器学习分类器并解释它们的预测。它使机器学习模型更具可解释性,允许使用统一的API可视化和调试各种机器学习模型。它内置了对几种ML框架的支持,并提供了一种解释黑盒模型的方法。
ELI5 (Explain Like I'm 5) 重点关注各种回归和分类模型的预测解释性,并支持scikit-learn、Keras、XGBoost、LightGBM等模型库,主要目标是通过权重和特征贡献等信息来解释模型预测。ELI5 支持多种解释性方法(如LIME、PI)、模型(线性模型和决策树),包括特征重要性、模型权重解释和局部解释,帮助用户理解模型的决策过程和特征的贡献度。
ELI5的名字来源于互联网上一个流行的语言"请对我五岁的孩子一样解释"。ELI5的优点是解释结果易于理解、与主流ML库集成好、配置简单易用;缺点是只提供LIME和SHAP这两种解释方法,可解释性有限。
ELI5的主要特点包括:
>> 提供LIME和PI(Permutation Importance)等模型解释方法;
>> 提供功能来可视化解释结果;
>> 提供的解释易于理解;
>> 可以很好地解释决策树和线性模型:ELI5支持scikit-learn、Keras、XGBoost、LightGBM等主要ML库中的模型;
>> ELI5可以解释线性分类器和回归器的权重,打印决策树作为文本或SVG,显示决策树和树式集成的功能重要性并解释预测。
eli5对比Yellowbrick,Yellowbrick 侧重于特征和模型性能解释,eli5侧重于模型参数和预测结果。
官方文档:Welcome to ELI5’s documentation! — ELI5 0.11.0 documentation
Github:GitHub - TeamHG-Memex/eli5: A library for debugging/inspecting machine learning classifiers and explaining their predictions,GitHub - eli5-org/eli5: A library for debugging/inspecting machine learning classifiers and explaining their predictions
1、eli5主要使用两种方式分析分类器或回归器
(1)、检查模型参数,试图弄清楚模型是如何全局工作的;
(2)、检查模型的单个预测,并找出模型做出决策的原因;
2、eli5库实现了几种用于检查黑盒模型的算法(参见检查黑盒估计器)
(1), TextExplainer允许使用LIME算法解释任何文本分类器的预测(Ribeiro et al., 2016)。有一些实用程序可以将LIME与非文本数据和任意黑盒分类器一起使用,但是这个特性目前还处于试验阶段。
(2)、置换重要性方法可用于计算黑盒估计器的特征重要性。
3、eli5库支持的机器学习框架和包—可以和大多数通用的python机器学习工具包一起使用
eli5能够同大多数通用的python机器学习工具包一起使用,这其中包括scikit-learn、Keras、LightGBM、XGBoost、CatBoost 等等。
(1) 、scikit-learn-目前,ELI5允许解释scikit-learn线性分类器和回归器的权重和预测,将决策树打印为文本或SVG,显示特征的重要性,并解释决策树和基于树的集合的预测。ELI5理解来自scikit-learn的文本处理实用程序,并能相应地高亮显示文本数据。支持Pipeline和FeatureUnion。它还允许通过撤销哈希来调试包含HashingVectorizer的scikit-learn管道。
(2)、Keras -通过 Grad-CAM 可视化解释图像分类器的预测。
(3)、xgboost -显示特征重要性并解释XGBClassifier, XGBRegressor和xgboost. booster的预测。
(4)、LightGBM -显示特征重要性,解释LGBMClassifier, LGBMRegressor和LightGBM . booster的预测。
(5)、CatBoost -显示CatBoostClassifier、CatBoostRegressor和CatBoost. CatBoost的特征重要性。
(6)、lightning -解释lightning 分类器和回归器的权重和预测。
(7) 、sklearn-crfsuite-ELI5允许检查sklearn_crfsuite.CRF模型的权重。
eli5库的安装
T1、pip安装eli5过程
pip install eli5
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple eli5
T2、conda安装eli5过程
(base) PS C:\Users\99386> conda install eli5
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- eli5
Current channels:
- https://repo.anaconda.com/pkgs/main/win-64
- https://repo.anaconda.com/pkgs/main/noarch
- https://repo.anaconda.com/pkgs/r/win-64
- https://repo.anaconda.com/pkgs/r/noarch
- https://repo.anaconda.com/pkgs/msys2/win-64
- https://repo.anaconda.com/pkgs/msys2/noarch
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
(base) PS C:\Users\99386> conda config --show channels
channels:
- defaults
(base) PS C:\Users\99386> conda config --show channels
channels:
- defaults
(base) PS C:\Users\99386> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
(base) PS C:\Users\99386> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
(base) PS C:\Users\99386> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
(base) PS C:\Users\99386>
(base) PS C:\Users\99386> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
(base) PS C:\Users\99386> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
(base) PS C:\Users\99386> conda config --set show_channel_urls yes
(base) PS C:\Users\99386> conda config --show channels
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
(base) PS C:\Users\99386> conda install eli5
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.12.0
latest version: 4.13.0
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: D:\ProgramData\Anaconda3
added / updated specs:
- eli5
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2022.3.29 | haa95532_1 122 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
certifi-2021.10.8 | py39haa95532_2 152 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda-4.12.0 | py39hcbf5309_0 1.0 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
eli5-0.11.0 | pyhd8ed1ab_0 76 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
graphviz-2.38.0 | h6538335_1011 41.0 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
openssl-1.1.1n | h2bbff1b_0 4.8 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
python-graphviz-0.16 | pyhd3deb0d_1 20 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python_abi-3.9 | 2_cp39 4 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
singledispatch-3.6.1 | pyh44b312d_0 12 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
------------------------------------------------------------
Total: 47.2 MB
The following NEW packages will be INSTALLED:
eli5 anaconda/cloud/conda-forge/noarch::eli5-0.11.0-pyhd8ed1ab_0
graphviz anaconda/cloud/conda-forge/win-64::graphviz-2.38.0-h6538335_1011
python-graphviz anaconda/cloud/conda-forge/noarch::python-graphviz-0.16-pyhd3deb0d_1
python_abi anaconda/cloud/conda-forge/win-64::python_abi-3.9-2_cp39
singledispatch anaconda/cloud/conda-forge/noarch::singledispatch-3.6.1-pyh44b312d_0
The following packages will be SUPERSEDED by a higher-priority channel:
ca-certificates pkgs/main --> anaconda/pkgs/main
certifi pkgs/main --> anaconda/pkgs/main
conda pkgs/main::conda-4.12.0-py39haa95532_0 --> anaconda/cloud/conda-forge::conda-4.12.0-py39hcbf5309_0
openssl pkgs/main --> anaconda/pkgs/main
Proceed ([y]/n)? y
Downloading and Extracting Packages
python-graphviz-0.16 | 20 KB | ################################################################################################################################################# | 100%
ca-certificates-2022 | 122 KB | ################################################################################################################################################# | 100%
singledispatch-3.6.1 | 12 KB | ################################################################################################################################################# | 100%
python_abi-3.9 | 4 KB | ################################################################################################################################################# | 100%
openssl-1.1.1n | 4.8 MB | ################################################################################################################################################# | 100%
eli5-0.11.0 | 76 KB | ################################################################################################################################################# | 100%
conda-4.12.0 | 1.0 MB | ################################################################################################################################################# | 100%
graphviz-2.38.0 | 41.0 MB | ################################################################################################################################################# | 100%
certifi-2021.10.8 | 152 KB | ################################################################################################################################################# | 100%
Preparing transaction: done
Verifying transaction: failed
EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
environment location: D:\ProgramData\Anaconda3
eli5库的使用方法
1、基础用法
1.1、eli5.show_weights()用法
eli5.show_weights(model, feature_names = X.columns.tolist())
from NDataScience.NDS_temp import read_html_open,PFI_plot
read_html_open(display_HTML_content)
features = val_X.columns.tolist()
PFI_plot(PI_RF,features)
`show_weights`方法默认采用gain来计算权重,当然也可以通过设置`importance_type`参数,来传入其他控制特征重要性的计算。
2、进阶用法
ML之PFI(eli5):基于mpg汽车油耗数据集利用RF随机森林算法和PFI置换特征重要性算法实现模型特征可解释性排序
ML之PFI(eli5):基于mpg汽车油耗数据集利用RF随机森林算法和PFI置换特征重要性算法实现模型特征可解释性排序_一个处女座的程序猿的博客-CSDN博客