Jupyter Notebook中CatBoost训练损失图无法显示的解决方案咨询
解决Jupyter Notebook中CatBoost训练损失图无法显示的问题
方法1:关闭交互式widget,改用Matplotlib静态绘图
CatBoost支持直接通过Matplotlib生成训练曲线,无需依赖widget库。修改训练代码,先关闭plot=True,训练完成后手动调用绘图API:
# 执行训练,不启用交互式绘图 model.fit(train_pool, eval_set=val_pool, early_stopping_rounds=150) # 手动绘制训练损失曲线 import matplotlib.pyplot as plt model.plot() plt.show()
该方式会生成静态Matplotlib图表,可在Jupyter中正常显示。
方法2:提取训练日志,自定义绘图
若需要更灵活的可视化效果,可从模型中提取训练过程的指标数据,用Matplotlib或Seaborn自定义绘图:
# 训练时关闭日志输出,同时保存训练记录 model.fit(train_pool, eval_set=val_pool, early_stopping_rounds=150, verbose=False) # 提取训练与验证的损失数据 train_loss = model.get_evals_result()['learn']['Logloss'] val_loss = model.get_evals_result()['validation']['Logloss'] iterations = range(1, len(train_loss)+1) # 自定义绘制损失曲线 import matplotlib.pyplot as plt plt.figure(figsize=(10,6)) plt.plot(iterations, train_loss, label='训练损失') plt.plot(iterations, val_loss, label='验证损失') plt.xlabel('迭代次数') plt.ylabel('Logloss') plt.title('CatBoost训练损失曲线') plt.legend() plt.grid(True) plt.show()
这种方式可完全自定义图表样式,不受CatBoost内置绘图的限制。
方法3:降级CatBoost版本适配旧widget库
若坚持使用交互式widget,可尝试安装较旧版本的CatBoost(如v0.26.x系列),这些版本对应的widget库仍可通过pip正常安装:
pip install catboost==0.26.1
安装完成后重启Jupyter Notebook,再运行带plot=True的训练代码即可。
内容的提问来源于stack exchange,提问作者user22262000
相关产品推荐
相关产品推荐

