模型可解释性:特征重要性/SHAP/LIME

模型可解释性:特征重要性/SHAP/LIME

1. 特征重要性

python 复制代码
from sklearn.ensemble import RandomForestClassifier
import pandas as pd

# 树模型内置特征重要性
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

importance = pd.Series(rf.feature_importances_, index=feature_names)
print(importance.nlargest(10))

# XGBoost 特征重要性
import xgboost as xgb
xgb_clf = xgb.XGBClassifier().fit(X_train, y_train)
xgb.plot_importance(xgb_clf, max_num_features=10)

2. SHAP 值

python 复制代码
import shap

# 计算 SHAP 值
explainer = shap.TreeExplainer(rf)
shap_values = explainer.shap_values(X_test)

# 摘要图
shap.summary_plot(shap_values[1], X_test, feature_names=feature_names)

# 单样本解释
shap.force_plot(explainer.expected_value[1], shap_values[1][0], X_test.iloc[0])

# 依赖图
shap.dependence_plot('feature_name', shap_values[1], X_test)

3. LIME

python 复制代码
from lime.lime_tabular import LimeTabularExplainer

explainer = LimeTabularExplainer(
    X_train.values,
    feature_names=feature_names,
    class_names=['class_0', 'class_1'],
    mode='classification'
)

# 解释单个样本
exp = explainer.explain_instance(
    X_test.iloc[0].values,
    rf.predict_proba,
    num_features=10
)

exp.show_in_notebook()

总结

方法 适用模型 粒度 计算速度
特征重要性 树模型 全局 快
SHAP 任意模型 全局+局部 中
LIME 任意模型 局部 快
相关推荐
Mr Robot44 分钟前
AI 赋能光伏发电量预测:从特征工程到 LSTM/XGboost 双基线实战
人工智能·能源
迪飞特科技1 小时前
大模型接入业务系统:API、本地部署、RAG、智能体四种路线怎么选
java·数据库·人工智能·知识图谱·deepseek·本地化大模型
狂奔solar1 小时前
Harness²:Google 如何让 AI 自己“调教“AI——递归代理优化框架深度拆解
人工智能
viperrrrrrrrrr71 小时前
SFT 微调指南
人工智能
海姐软件测试1 小时前
Python 自动化与 AI 结合:基于 Jenkins 的回归验证实践
人工智能·python·自动化
回眸&啤酒鸭1 小时前
【回眸】纳米 AI 搜索效果实测与能力展示
人工智能
Haoyunyi1 小时前
2026年做期货量化,和三年前已经不是一回事了
大数据·人工智能·python·金融·区块链
通信瓦工1 小时前
Meta ORv3 HPR Next 机架与供电方案map
人工智能
shaibdoio1 小时前
两项计算机软件著作权背后:AI获客系统的技术自研路径
人工智能·长沙瞬维ai
紧跟先前的步伐1 小时前
Go AI Agent 实战 04|Prompt 与消息角色:System、User、Assistant
人工智能·golang·prompt