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

Azure Databricks中PyCaret的plot_model、evaluate_model无正常可视化输出

在Azure Databricks中修复PyCaret可视化失效问题

问题描述

在Azure Databricks环境中使用PyCaret进行模型评估时,遇到以下问题:

  • 调用evaluate_model()后,控制台仅输出交互式组件的文本表示,无法渲染可视化小部件及图表
  • plot_model()的参数无法正常工作,无法生成预期的可视化结果

示例代码

from pycaret.datasets import get_data
juice = get_data('juice')
from pycaret.classification import *
exp_name = setup(data = juice,  target = 'Purchase',silent=True,html=False)
lr = create_model('lr')
evaluate_model(lr)

执行后的异常输出

interactive(children=(ToggleButtons(description='Plot Type:', icons=('',), options=(('Hyperparameters', 'parameter'), ('AUC', 'auc'), ('Confusion Matrix', 'confusion_matrix'), ('Threshold', 'threshold'), ('Precision Recall', 'pr'), ('Prediction Error', 'error'), ('Class Report', 'class_report'), ('Feature Selection', 'rfe'), ('Learning Curve', 'learning'), ('Manifold Learning', 'manifold'), ('Calibration Curve', 'calibration'), ('Validation Curve', 'vc'), ('Dimensions', 'dimension'), ('Feature Importance', 'feature'), ('Feature Importance (All)', 'feature_all'), ('Decision Boundary', 'boundary'), ('Lift Chart', 'lift'), ('Gain Chart', 'gain'), ('Decision Tree', 'tree'), ('KS Statistic Plot', 'ks')), value='parameter'), Output()), _dom_classes=('widget-interact',))

plot_model参数失效情况

plot_model参数失效截图

解决方法

1. 调整PyCaret初始化配置

在setup()函数中启用HTML输出并指定固定会话ID,适配Databricks的渲染环境:

exp_name = setup(
    data=juice,
    target='Purchase',
    silent=True,
    html=True,  # 开启HTML格式输出,适配Databricks
    session_id=123,  # 固定会话ID,保证结果可复现
    verbose=False
)

2. 使用Databricks内置display()函数渲染图表

PyCaret的plot_model()支持返回matplotlib图形对象,配合display()可直接在Notebook中显示:

# 生成并显示特征重要性图
fig = plot_model(lr, plot='feature', return_plots=True)
display(fig)

# 生成并显示混淆矩阵
fig = plot_model(lr, plot='confusion_matrix', return_plots=True)
display(fig)

3. 启用ipywidgets支持(针对交互式组件)

若需使用evaluate_model()的交互式小部件,需在Databricks集群中配置ipywidgets:

  1. 进入集群的库页面,安装ipywidgets和widgetsnbextension包
  2. 在Notebook中运行以下命令启用扩展:
!jupyter nbextension enable --py widgetsnbextension
  1. 重启集群后,将setup()中的html设为False,再调用evaluate_model()即可正常渲染交互式组件

4. 手动提取指标并可视化(替代方案)

如果上述方法仍无效,可通过pull()提取评估数据,手动使用Databricks的可视化工具绘制:

# 获取模型评估指标表格
metrics = pull()
display(metrics)

# 手动绘制混淆矩阵
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt

# 生成预测结果
y_pred = predict_model(lr)['Label']
# 计算混淆矩阵
cm = confusion_matrix(juice['Purchase'], y_pred)
# 绘制热力图
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
display(plt.gcf())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 15:36:19