pdp部分依赖图

目录

一、作用

二、存在的问题

三、代码

3.1 单特征对预测结果的边际效应

3.2 两个特征对预测结果的边际效应


一、作用

【定义一】:部分依赖图(Partial Dependence Plot)显示了一个或两个特征对机器学习模型的预测结果的边际效应

【定义二】通过改变感兴趣特征的值同时保持补充特征的值不变,分析模型输出,从而计算特征变量对模型预测结果影响的函数关系:例如近似线性关系、单调关系或者更复杂的关系。

二、存在的问题

  • 自变量间可能存在复杂的交互关系,所以单看一个变量的pdp图是不全面的
  • pdp图不显示特征分布,这可能会产生误导,因为可能会过度解释几乎没有数据的区域
  • pdp假设计算部分依赖的特征与其他特征不相关,如果相关,pdp图会包含不可能的点
  • pdp绘制的是平均响应,个体间的异质性可能无法被观察到;即有可能个体间的变化差异明显。(个体间的变化差异通过个体条件期望(Individual Conditional Expectation,ICE)曲线观察
  • 由于算法的精度和难以避免的过拟合,pdp图可能存在较小的波动,而解释性研究更加关注整体的变化趋势。

三、代码

def partial_dependence(
        ind,  //目标特征
        model,  //预测函数model.predict
        data,  //数据集
        xmin="percentile(0)",
        xmax="percentile(100)",
        npoints=None, 
        feature_names=None, 
        hist=True, 
        model_expected_value=False, //平均模型预测
        feature_expected_value=False, //平均特征值
        shap_values=None,
        ylabel=None, 
        ice=True,  //是否制作部份依赖图或个体条件期望图
        ace_opacity=1, 
        pd_opacity=1, 
        pd_linewidth=2,
        ace_linewidth='auto', 
        ax=None, 
        show=True)

补充说明:model_expected_value,feature_expected_value

3.1 单特征对预测结果的边际效应

from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
model = RandomForestRegressor(random_state=42).fit(X, y)
X100 = X.sample(n=100)

from shap.plots._partial_dependence import partial_dependence
partial_dependence(ind="AveOccup", model=model.predict, data=X100, ice=False, model_expected_value=True, feature_expected_value=True)
partial_dependence(ind="MedInc", model=model.predict, data=X100, ice=False, model_expected_value=True, feature_expected_value=True)

 

partial_dependence(ind="AveOccup", model=model.predict, data=X100, ice=True, model_expected_value=True, feature_expected_value=True)
partial_dependence(ind="MedInc", model=model.predict, data=X100, ice=True, model_expected_value=True, feature_expected_value=True)

3.2 两个特征对预测结果的边际效应

 pip install plotly_express

import plotly_express as px
fig = px.scatter(x=X["AveOccup"], y=X["MedInc"], color=y,labels={"x": "AveOccup", "y": "MedInc", "color": "price"},range_x=(0, 10))
fig.write_image('./1.png')
fig.show()

【参考资料】

  1. 8.1 Partial Dependence Plot (PDP) | Interpretable Machine Learning
  2. 科研中的部分依赖图(pdp)绘制 - 知乎
  3. 4.1. Partial Dependence and Individual Conditional Expectation plots — scikit-learn 1.1.3 documentation
  4. https://www.toutiao.com/article/7076250560642515467/?app=news_article&timestamp=1667365264&use_new_style=1&req_id=202211021301040101511830281138B758&group_id=7076250560642515467&wxshare_count=1&tt_from=weixin&utm_source=weixin&utm_medium=toutiao_android&utm_campaign=client_share&share_token=52f690e2-4a1d-4ecb-a4c9-66974484cac5&source=m_redirect

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值