局部依赖与个体条件期望

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

部分依赖图(PDP)和个体条件期望(ICE)图是用于可视化和分析目标响应与一组输入特征之间相互作用的有用工具。PDP 显示目标响应与输入特征之间的依赖关系,而 ICE 图则可视化每个单独样本的预测对某个特征的依赖关系。这些图有助于我们理解目标响应与输入特征之间的关系。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们会立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/ensemble("Ensemble Methods") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/inspection("Inspection") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/ensemble -.-> lab-71126{{"局部依赖与个体条件期望"}} sklearn/inspection -.-> lab-71126{{"局部依赖与个体条件期望"}} sklearn/datasets -.-> lab-71126{{"局部依赖与个体条件期望"}} ml/sklearn -.-> lab-71126{{"局部依赖与个体条件期望"}} end

导入必要的库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.inspection import plot_partial_dependence, partial_dependence

加载并准备数据

data = load_boston()
X = data.data
y = data.target
feature_names = data.feature_names

## 创建一个 DataFrame 以便于数据操作
df = pd.DataFrame(X, columns=feature_names)

训练一个随机森林模型

model = RandomForestRegressor()
model.fit(X, y)

创建并可视化部分依赖图

fig, ax = plot_partial_dependence(model, X, features=[(0, 1), (2, 3)], feature_names=feature_names, grid_resolution=20)

## 设置图形大小和标题
fig.set_size_inches(10, 8)
fig.suptitle('部分依赖图')

plt.show()

创建并可视化个体条件期望图

fig, ax = plot_partial_dependence(model, X, features=[(0, 1), (2, 3)], feature_names=feature_names, kind='individual')

## 设置图形大小和标题
fig.set_size_inches(10, 8)
fig.suptitle('个体条件期望图')

plt.show()

计算特定特征的部分依赖值

x_index = 0
pdp, axes = partial_dependence(model, X, features=[x_index], grid_resolution=20)

## 绘制部分依赖值
plt.plot(axes[x_index], pdp[0])
plt.xlabel(feature_names[x_index])
plt.ylabel("部分依赖")
plt.title("部分依赖图")

plt.show()

计算特定特征的个体条件期望值

x_index = 0
ice, axes = partial_dependence(model, X, features=[x_index], kind='individual')

## 绘制个体条件期望值
for i in range(len(ice)):
    plt.plot(axes[x_index], ice[i], color='lightgray')
plt.plot(axes[x_index], np.mean(ice, axis=0), color='blue')
plt.xlabel(feature_names[x_index])
plt.ylabel("个体条件期望")
plt.title("个体条件期望图")

plt.show()

总结

部分依赖图(Partial dependence plots)和个体条件期望图(individual conditional expectation plots)是用于可视化和理解目标响应与输入特征之间关系的强大工具。部分依赖图提供了依赖关系的整体视图,而个体条件期望图则展示了个体差异。通过使用这些图,我们可以深入了解目标响应如何随输入特征的不同值而变化。