python機器學習Github已達8.9Kstars模型解釋器LIME
簡單的模型例如線性回歸,LR等模型非常易于解釋,但在實際應用中的效果卻遠遠低于復雜的梯度提升樹模型以及神經(jīng)網(wǎng)絡等模型。
現(xiàn)在大部分互聯(lián)網(wǎng)公司的建模都是基于梯度提升樹或者神經(jīng)網(wǎng)絡模型等復雜模型,遺憾的是,這些模型雖然效果好,但是我們卻較難對其進行很好地解釋,這也是目前一直困擾著大家的一個重要問題,現(xiàn)在大家也越來越加關注模型的解釋性。
本文介紹一種解釋機器學習模型輸出的方法LIME。它可以認為是SHARP的升級版,Github鏈接:https://github.com/marcotcr/lime,有所收獲多多支持
LIME
LIME(Local Interpretable Model-agnostic Explanations)支持的模型包括:
- 結構化模型的解釋;
- 文本分類器的解釋;
- 圖像分類器的解釋;
LIME被用作解釋機器學習模型的解釋,通過LIME我們可以知道為什么模型會這樣進行預測。
本文我們就重點觀測一下LIME是如何對預測結果進行解釋的。
代 碼
此處我們使用winequality-white數(shù)據(jù)集,并且將quality<=5設置為0,其它的值轉變?yōu)?.
# !pip install lime import pandas as pd from xgboost import XGBClassifier import shap import numpy as np from sklearn.model_selection import train_test_split
df = pd.read_csv('./data/winequality-white.csv',sep = ';') df['quality'] = df['quality'].apply(lambda x: 0 if x <= 5 else 1) df.head()
# 訓練集測試集分割 X = df.drop('quality', axis=1) y = df['quality'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1) # 模型訓練 model = XGBClassifier(n_estimators = 100, random_state=42) model.fit(X_train, y_train) score = model.score(X_test, y_test) score
The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. 0.832653061224489
對單個樣本進行預測解釋
下面的圖中表明了單個樣本的預測值中各個特征的貢獻。
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' )
模型有84%的置信度是壞的wine,而其中alcohol
,totals ulfur dioxide是最重要的。
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' )
模型有59%的置信度是壞的wine,而其中alcohol,chlorides, density, citric acid是最重要的預測參考因素。
exp = explainer.explain_instance(data_row=X_test.iloc[1], predict_fn=model.predict_proba) exp.show_in_notebook(show_table=True)
適用問題
LIME可以認為是SHARP的升級版,它通過預測結果解釋機器學習模型很簡單。它為我們提供了一個很好的方式來向非技術人員解釋地下發(fā)生了什么。您不必擔心數(shù)據(jù)可視化,因為LIME庫會為您處理數(shù)據(jù)可視化。
參考鏈接
https://www.kaggle.com/piyushagni5/white-wine-quality
LIME: How to Interpret Machine Learning Models With Python
https://github.com/marcotcr/lime
https://mp.weixin.qq.com/s/47omhEeHqJdQTtciLIN2Hw
以上就是Github已達8.9Kstars的最佳模型解釋器LIME的詳細內容,更多關于模型解釋器LIME的資料請關注腳本之家其它相關文章!
相關文章
淺談Python中的zip()與*zip()函數(shù)詳解
這篇文章主要介紹了淺談Python中的zip()與*zip()函數(shù)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02python利用有道翻譯實現(xiàn)"語言翻譯器"的功能實例
小編就為大家分享一篇python利用有道翻譯實現(xiàn)"語言翻譯器"的功能實例。具有比較好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11matlab灰度圖像調整及imadjust函數(shù)的用法詳解
這篇文章主要介紹了matlab圖像灰度調整及imadjust函數(shù)的用法詳解,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02