1

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)

Orion
  • 65
  • 1
  • 9
  • Can you provide a sample of the array results? – itprorh66 Oct 03 '20 at 19:03
  • Your first `for` loop assigns to the same variable (`candidates`) each time, overwriting whatever was assigned in the previous iterations of the loop. Only the final iteration's value remains, and apparently that was an empty sequence, causing the second `for` loop to execute zero times. Looks like the second `for` loop was supposed to be indented one level further, so that it executes for each iteration of the first loop. – jasonharper Oct 03 '20 at 20:26

0 Answers0