You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用PartialDependenceDisplay.from_estimator触发AttributeError如何解决

问题原因

AttributeError的触发原因是当前环境安装的scikit-learn版本低于0.24,PartialDependenceDisplay.from_estimator 是scikit-learn 0.24版本才正式加入的API,低版本中不存在该方法。

可行解决方案

方案1:升级scikit-learn版本(推荐)

直接升级依赖到支持该API的版本即可兼容原有代码,执行以下安装命令:

  • pip用户执行:pip install --upgrade scikit-learn
  • conda用户执行:conda install scikit-learn>=0.24

升级完成后重新运行原有代码即可正常输出部分依赖图。

方案2:适配低版本API(无法升级环境时使用)

如果当前环境不允许升级依赖,可以替换为低版本兼容的plot_partial_dependence接口实现相同效果,示例代码如下:

from sklearn.inspection import plot_partial_dependence
from sklearn.datasets import make_friedman1
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt

X, y = make_friedman1()
est1 = LinearRegression().fit(X, y)
est2 = RandomForestRegressor().fit(X, y)
# 先绘制第一个模型的PDP
disp1 = plot_partial_dependence(est1, X, [1, 2])
# 复用第一个的坐标轴绘制第二个模型的PDP
disp2 = plot_partial_dependence(est2, X, [1, 2], ax=disp1.axes_)
plt.show()

这个接口在scikit-learn 0.21~0.23版本中均可正常使用,最终效果和原有代码一致。

内容的提问来源于stack exchange,提问作者user17065776

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 00:24:03