Py之eli5:eli5库的简介、安装、使用方法之详细攻略
目录
eli5库的简介
1、eli5库有助于机器学习的可解释性
2、eli5库实现了几种用于检查黑盒模型的算法(参见检查黑盒估计器)
3、eli5库支持的机器学习框架和包—可以和大多数通用的python机器学习工具包一起使用
eli5库的安装
T1、pip安装eli5过程
T2、conda安装eli5过程
eli5库的使用方法
1、基础用法
1.1、eli5.show_weights()用法
2、进阶用法
eli5库的简介
eli5是一个Python包,它可以帮助调试机器学习分类器并解释它们的预测。ELI5是一个Python库,它允许使用统一的API可视化和调试各种机器学习模型。它内置了对几种ML框架的支持,并提供了一种解释黑盒模型的方法。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 eli5pip 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 tohttps://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.0latest version: 4.13.0
Please update conda by running$ conda update -n base -c defaults conda
## Package Plan ##environment location: D:\ProgramData\Anaconda3added / 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/maincertifi-2021.10.8 | py39haa95532_2 152 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainconda-4.12.0 | py39hcbf5309_0 1.0 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgeeli5-0.11.0 | pyhd8ed1ab_0 76 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgegraphviz-2.38.0 | h6538335_1011 41.0 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgeopenssl-1.1.1n | h2bbff1b_0 4.8 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainpython-graphviz-0.16 | pyhd3deb0d_1 20 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgepython_abi-3.9 | 2_cp39 4 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forgesingledispatch-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_0graphviz anaconda/cloud/conda-forge/win-64::graphviz-2.38.0-h6538335_1011python-graphviz anaconda/cloud/conda-forge/noarch::python-graphviz-0.16-pyhd3deb0d_1python_abi anaconda/cloud/conda-forge/win-64::python_abi-3.9-2_cp39singledispatch anaconda/cloud/conda-forge/noarch::singledispatch-3.6.1-pyh44b312d_0The following packages will be SUPERSEDED by a higher-priority channel:ca-certificates pkgs/main --> anaconda/pkgs/maincertifi pkgs/main --> anaconda/pkgs/mainconda pkgs/main::conda-4.12.0-py39haa95532_0 --> anaconda/cloud/conda-forge::conda-4.12.0-py39hcbf5309_0openssl 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):基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用RF随机森林算法和PFI置换特征重要性算法实现模型特征可解释性排序实