I have a bit of code that reports result of CV and hyperparameter optimization. I'm trying to print out the results to file. The file gets created but doesn't print the results there. It also doesn't print them to terminal. What's wrong?
def report(results, n_top=3):
original_stdout = sys.stdout
with open('filename.txt', 'w') as f:
sys.stdout = f
for i in range(1, n_top + 1):
candidates = np.flatnonzero(results['rank_test_score'] == i)
for candidate in candidates:
print("Model with rank: {0}".format(i))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
results['mean_test_score'][candidate],
results['std_test_score'][candidate]))
print("Parameters: {0}".format(results['params'][candidate]))
print("")
sys.stdout = original_stdout
report(gs.cv_results_,10)