gpt4 book ai didi

python - 如何控制 fbprophet 的输出?

转载 作者:太空狗 更新时间:2023-10-29 22:57:12 25 4
gpt4 key购买 nike

我希望能够在拟合预测模型时抑制来自 fbprophet 的一些输出。此输出(“初始对数 union 概率...”、“优化正常终止:”、“检测到收敛:...”等)显然来自 Stan 的 cpp 代码,我找不到任何明显的方法来控制它 [我正在使用 python 界面]。深入研究代码会发现 verbose=False 作为高级 stan() 例程(在 pystan/api.py 中)的默认值,但显然此参数不会抑制此打印输出。不修改代码是否可行?

最佳答案

不幸的是,它比它应该的更复杂。我认为一些输出来自 C 或 Fortran 编译代码或其他东西。以下是您的操作方法(找到 here ):

import os
import sys

import pandas as pd
from fbprophet import Prophet


# from https://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions
class suppress_stdout_stderr(object):
'''
A context manager for doing a "deep suppression" of stdout and stderr in
Python, i.e. will suppress all print, even if the print originates in a
compiled C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed
to stderr just before a script exits, and after the context manager has
exited (at least, I think that is why it lets exceptions through).

'''
def __init__(self):
# Open a pair of null files
self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in range(2)]
# Save the actual stdout (1) and stderr (2) file descriptors.
self.save_fds = (os.dup(1), os.dup(2))

def __enter__(self):
# Assign the null pointers to stdout and stderr.
os.dup2(self.null_fds[0], 1)
os.dup2(self.null_fds[1], 2)

def __exit__(self, *_):
# Re-assign the real stdout/stderr back to (1) and (2)
os.dup2(self.save_fds[0], 1)
os.dup2(self.save_fds[1], 2)
# Close the null files
os.close(self.null_fds[0])
os.close(self.null_fds[1])

m = Prophet()
df = pd.read_csv('somefile.csv')

with suppress_stdout_stderr():
m.fit(minimal_df)

“更简单”的方式(如果它会起作用,但它不会)会是这样的:

import os
import sys

import pandas as pd
from fbprophet import Prophet

m = Prophet()
df = pd.read_csv('somefile.csv')

orig_out = sys.stdout
sys.stdout = open(os.devnull, 'w')
m.fit(df)
sys.stdout = orig_out

关于python - 如何控制 fbprophet 的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com