oshribr
oshribr

Reputation: 666

GridSearchCV - save result each iteration

I'm using GridSearchCV, and after each iteration I want to save the clf.cv_results_ property to a file, (just in case that the process will crash in the middle).

I tried looking for a solution but I just couldn't figure it out.

Any help will be appreciated.

Upvotes: 9

Views: 4578

Answers (1)

Espoir Murhabazi
Espoir Murhabazi

Reputation: 6376

One of the ways to do it is to set the verbose parameter of your grid search to an integer higher than 0 like 10 or more, it will print the result for each iteration to the console.

With this, your output should be print or logged to your console, Then follow the answer to this or this question to see how to put the logged result to a file.

Basically, put this before running the GridSearch:

import sys
old_stdout = sys.stdout

log_file = open("message.log","w")

sys.stdout = log_file

Then after running your GridSearch, you should close all resources with this:

sys.stdout = old_stdout
log_file.close()

Upvotes: 7

Related Questions