簡單的模型例如線性回歸,LR等模型非常易于解釋,但在實際應用中的效果卻遠遠低于復雜的梯度提升樹模型以及神經網絡等模型。
現在大部分互聯網公司的建模都是基于梯度提升樹或者神經網絡模型等復雜模型,遺憾的是,這些模型雖然效果好,但是我們卻較難對其進行很好地解釋,這也是目前一直困擾著大家的一個重要問題,現在大家也越來越加關注模型的解釋性。
本文介紹一種解釋機器學習模型輸出的方法LIME。它可以認為是SHARP的升級版,Github鏈接:https://github.com/marcotcr/lime,有所收獲多多支持
LIME
LIME(Local Interpretable Model-agnostic Explanations)支持的模型包括:
- 結構化模型的解釋;
- 文本分類器的解釋;
- 圖像分類器的解釋;
LIME被用作解釋機器學習模型的解釋,通過LIME我們可以知道為什么模型會這樣進行預測。
本文我們就重點觀測一下LIME是如何對預測結果進行解釋的。
代 碼
此處我們使用winequality-white數據集,并且將quality<=5設置為0,其它的值轉變為1.
# !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的升級版,它通過預測結果解釋機器學習模型很簡單。它為我們提供了一個很好的方式來向非技術人員解釋地下發生了什么。您不必擔心數據可視化,因為LIME庫會為您處理數據可視化。
參考鏈接
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的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/118313872