Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 498
oshribr Avatar asked Jan 02 '18 08:01

oshribr


Video Answer


1 Answers

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()
like image 110
Espoir Murhabazi Avatar answered Nov 07 '22 00:11

Espoir Murhabazi