背景
prophet fit 输出大量无关日志,关闭对应的日志也照样输出
Initial log joint probability = -50.7823
Iteration 1. Log joint probability = 36.5594. Improved by 87.3417.
Iteration 2. Log joint probability = 47.9345. Improved by 11.3751.
Iteration 3. Log joint probability = 61.4267. Improved by 13.4922.
Iteration 4. Log joint probability = 70.6681. Improved by 9.24134.
Iteration 5. Log joint probability = 73.2246. Improved by 2.55655.
Iteration 6. Log joint probability = 73.247. Improved by 0.0223382.
Iteration 7. Log joint probability = 73.2556. Improved by 0.00860014.
解决办法
关闭日志
import warnings
import logging
warnings.filterwarnings("ignore")
logger = logging.getLogger('cmdstanpy')
logger.addHandler(logging.NullHandler())
logger.propagate = False
logger.setLevel(logging.CRITICAL)
logging.getLogger("cmdstanpy").disabled = True
logging.getLogger("prophet").disabled = True
包装屏蔽输出
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])
...
with suppress_stdout_stderr():
model.fit(df)
...

7214

被折叠的 条评论
为什么被折叠?



