Skip to content Skip to sidebar Skip to footer

Printing All Output From Sklearn Gridsearchcv To File?

I am running a long grid-search using sklearn and I want to log all (emphasis all) console output to file. Running from terminal using > and changing stdout to an open file etc.

Solution 1:

From source of the joblib package used internally by sklearn for parallelization:

def _print(self, msg, msg_args):
    """Display the message on stout or stderr depending on verbosity"""
    # XXX: Not using the logger framework: need to
    # learn to use logger better.
    if not self.verbose:
        return
    if self.verbose < 50:
        writer = sys.stderr.write
    else:
        writer = sys.stdout.write
    msg = msg % msg_args
    writer('[%s]: %s\n' % (self, msg))

So with verbose=1 as the OP was using, redirecting stderr ought to capture the missing lines. But then this will not get stdout. So one can just merge them using this answer and doing:

# necessary imports

logfile = open('test.txt', 'w')

original_stderr = sys.stderr
original_stdout = sys.stdout

sys.stdout = Tee(sys.stdout, logfile)
sys.stderr = sys.stdout
.
.
[code to log]
.
.

sys.stdout = original_stdout
sys.stderr = original_stderr
logfile.close()

Post a Comment for "Printing All Output From Sklearn Gridsearchcv To File?"