知识点回顾:
- 官方文档的检索方式:github和官网
- 官方文档的阅读和使用:要求安装的包和文档为同一个版本
- 类的关注点:
- 实例化所需要的参数
- 普通方法所需要的参数
- 普通方法的返回值
- 绘图的理解:对底层库的调用
作业:参考pdpbox官方文档中的其他类,绘制相应的图,任选即可
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# 加载鸢尾花数据集
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target # 添加目标列(0-2类:山鸢尾、杂色鸢尾、维吉尼亚鸢尾)
# 特征与目标变量
features = iris.feature_names # 4个特征:花萼长度、花萼宽度、花瓣长度、花瓣宽度
target = 'target' # 目标列名
X_train, X_test, y_train, y_test = train_test_split(
df[features], df[target], test_size=0.2, random_state=42
)
# 训练模型
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# 首先要确保库的版本是最新的,因为我们看的是最新的文档,库的版本可以在github上查看
import pdpbox
print(pdpbox.__version__) # pdpbox版本
from pdpbox.info_plots import PredictPlot
# 选择待分析的特征(如:petal length (cm))
feature = 'petal length (cm)'
feature_name = feature # 特征显示名称
# 初始化TargetPlot对象(移除plot_type参数)
predict_plot = PredictPlot(
df = df,
feature = feature,
feature_name = feature_name,
model=model,
model_features=features,
chunk_size=-1,
grid_type='percentile', # 分桶方式:百分位
num_grid_points=10 # 划分为10个桶
)
fig, axes, summary_df = predict_plot.plot(
which_classes=None, # 绘制所有类别(0,1,2)
show_percentile=True, # 显示百分位线
engine='plotly',
template='plotly_white'
)
# 手动设置图表尺寸(单位:像素)
fig.update_layout(
width=1000, # 宽度800像素
height=1000, # 高度500像素
title=dict(text=f'PredictPlot: {feature_name}', x=0.5) # 居中标题
)
fig.show()
1468

被折叠的 条评论
为什么被折叠?



