python機(jī)器學(xué)習(xí)Github已達(dá)8.9Kstars模型解釋器LIME
簡單的模型例如線性回歸,LR等模型非常易于解釋,但在實(shí)際應(yīng)用中的效果卻遠(yuǎn)遠(yuǎn)低于復(fù)雜的梯度提升樹模型以及神經(jīng)網(wǎng)絡(luò)等模型。
現(xiàn)在大部分互聯(lián)網(wǎng)公司的建模都是基于梯度提升樹或者神經(jīng)網(wǎng)絡(luò)模型等復(fù)雜模型,遺憾的是,這些模型雖然效果好,但是我們卻較難對其進(jìn)行很好地解釋,這也是目前一直困擾著大家的一個重要問題,現(xiàn)在大家也越來越加關(guān)注模型的解釋性。
本文介紹一種解釋機(jī)器學(xué)習(xí)模型輸出的方法LIME。它可以認(rèn)為是SHARP的升級版,Github鏈接:https://github.com/marcotcr/lime,有所收獲多多支持
LIME
LIME(Local Interpretable Model-agnostic Explanations)支持的模型包括:
- 結(jié)構(gòu)化模型的解釋;
- 文本分類器的解釋;
- 圖像分類器的解釋;
LIME被用作解釋機(jī)器學(xué)習(xí)模型的解釋,通過LIME我們可以知道為什么模型會這樣進(jìn)行預(yù)測。
本文我們就重點(diǎn)觀測一下LIME是如何對預(yù)測結(jié)果進(jìn)行解釋的。
代 碼
此處我們使用winequality-white數(shù)據(jù)集,并且將quality<=5設(shè)置為0,其它的值轉(zhuǎn)變?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ùn)練集測試集分割 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) # 模型訓(xùn)練 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
對單個樣本進(jìn)行預(yù)測解釋
下面的圖中表明了單個樣本的預(yù)測值中各個特征的貢獻(xiàn)。
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是最重要的預(yù)測參考因素。
exp = explainer.explain_instance(data_row=X_test.iloc[1], predict_fn=model.predict_proba) exp.show_in_notebook(show_table=True)
適用問題
LIME可以認(rèn)為是SHARP的升級版,它通過預(yù)測結(jié)果解釋機(jī)器學(xué)習(xí)模型很簡單。它為我們提供了一個很好的方式來向非技術(shù)人員解釋地下發(fā)生了什么。您不必?fù)?dān)心數(shù)據(jù)可視化,因?yàn)長IME庫會為您處理數(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已達(dá)8.9Kstars的最佳模型解釋器LIME的詳細(xì)內(nèi)容,更多關(guān)于模型解釋器LIME的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。