Reputation: 6821
h2o_model.accuracy
prints model validation data when executed in a Jupyter Notebook cell (which is desirable, despite the function name). How to save this whole validation output (entire notebook cell contents) to a file? Please test before suggesting redirections.
Upvotes: 1
Views: 156
Reputation: 6821
Ok, so only the h2o_model.accuracy
output cannot be captured, while xgb_model.cross_validation_metrics_summary
or even h2o_model
alone can - e.g. like that:
%%capture captured_output
# print model validation
# data to `captured_output`
xgb_model
In another notebook cell:
# print(captured_output.stdout.replace("\n\n","\n"))
with open(filename, 'w') as f:
f.write((captured_output.stdout.replace("\n\n","\n")))
Upvotes: 0
Reputation: 591
I'd be careful using %%capture
, it doesn't capture html content (tables) in the stdout
.
The redirect_stdout
works flawlessly when used from python CLI/script. IPython/Jupyter might cause issues with tables as they are display
ed not print
ed. Note that you should not use .readlines()
to get the results from StringIO
- use .getvalue()
.
You can use h2o_model.save_model_details(path)
to persist information about the model to a json file (which might serve you better in a long run but it's not really human readable).
If you really want to have the output that looks like what would you get from a Jupyter notebook, you can use the following hack:
import os
import h2o
h2o.connect(verbose=False)
h2o.get_model(os.environ["H2O_MODEL"])
!H2O_MODEL={h2o_model.key} jupyter nbconvert --to html --execute template.ipynb --output={h2o_model.key}_results.html
You can also create a template for the nbconvert
to hide the code cells.
Upvotes: 2
Reputation: 591
You should call h2o_model.accuracy()
(note the parentheses). The reason the whole model gets printed is non-idiomatic implementation of __repl__
in h2o models which prints rather then returning a string (there's a JIRA to fix that).
If you encounter some other situation where you would like to save printed output of some command, you can use redirect_stdout
[1] to capture it (assuming you have python 3.4+).
[1] https://docs.python.org/3.9/library/contextlib.html#contextlib.redirect_stdout
Upvotes: 1