英文:
Is there a way to not display a plot when calling Pycaret's plot_model()?
问题
I want to use pycaret's plot_model to save a plot image, however I don't want to display the plot. I'm looping through many figures and models and therefore displaying all the plots is resulting in a memory.
Ideally I'm looking for something like this, but I don't see any way to suppress the displaying of the plot:
plot_image = plot_model(plot='ts', return_fig=True, display_fig=False)
I tried setting verbose=False but it didn't do anything.
英文:
I want to use pycaret's plot_model to save a plot image, however I don't want to display the plot. I'm looping through many figures and models and therefore displaying all the plots is resulting in a memory.
Ideally I'm looking for something like this, but I don't see any way to suppress the displaying of the plot:
plot_image = plot_model(plot='ts', return_fig=True, display_fig=False)
I tried setting verbose=False but it didn't do anything
答案1
得分: 1
你可以使用 save 参数保存绘图,然后 PyCaret 将不显示任何绘图。您可以将 save=True 设置为将绘图保存到当前目录,或者您可以将 save={destination_dir} 设置为将绘图保存到您的目标目录。
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
import pathlib
# 获取样本数据
y = get_data('airline', verbose=False)
# 实验
exp = TSForecastingExperiment()
exp.setup(data=y, fh=12, session_id=42)
top3 = exp.compare_models(n_select=3, turbo=True)
# 获取所有绘图
all_plots = list(exp.get_config('_available_plots').keys())
base_dir = 'D:\\Python\\figures'
for model in top3:
# 获取模型名称
model_name = type(model).__name__
save_dir = f'{base_dir}\\{model_name}'
# 为每个模型创建一个目录
pathlib.Path(save_dir).mkdir(exist_ok=True)
for plot in all_plots:
try:
# 将每个模型的每个绘图保存到所需目录
exp.plot_model(model, plot=plot, save=save_dir)
except Exception as e:
print(f'{model_name} 在 {plot} 绘图中出错')
英文:
You can use save parameter to save a plot then PyCaret will not display any plot. You can set save=True to save a plot to the current directory or you can set save={destination_dir} to save a plot to your destination directory.
<!-- language: lang-py -->
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
import pathlib
# Get sample data
y = get_data('airline', verbose=False)
# Experiment
exp = TSForecastingExperiment()
exp.setup(data=y, fh=12, session_id=42)
top3 = exp.compare_models(n_select=3, turbo=True)
# Get all plots
all_plots = list(exp.get_config('_available_plots').keys())
base_dir = 'D:\\Python\\figures'
for model in top3:
# Get model name
model_name = type(model).__name__
save_dir = f'{base_dir}\\{model_name}'
# Create a directory for each model
pathlib.Path(save_dir).mkdir(exist_ok=True)
for plot in all_plots:
try:
# Save each plot of each model to desired directory
exp.plot_model(model, plot=plot, save=save_dir)
except Exception as e:
print(f'{model_name} has error in {plot} plot')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论