Skip to main content
All trained H2O models are instances of ModelBase (the base class) or one of its subclasses. These methods are available on every model returned by an estimator’s train() call or loaded via h2o.load_model().
from h2o.model.model_base import ModelBase

Prediction

predict

predictions = model.predict(test_data, custom_metric=None, custom_metric_func=None)
Generate predictions for a new dataset.
test_data
H2OFrame
required
Dataset to score. Must contain the same predictor columns used during training.
custom_metric
class
A custom evaluation function class reference, uploaded to the cluster before scoring.
custom_metric_func
string
Reference to a previously uploaded custom metric function (e.g., result of h2o.upload_custom_metric()). Cannot be specified together with custom_metric.
Returns an H2OFrame. For classification models, the frame contains:
  • predict — the predicted class label
  • p0, p1, … — predicted probabilities for each class
pred = model.predict(test)
pred.head()

Performance evaluation

model_performance

perf = model.model_performance(test_data=None, train=False, valid=False, xval=False, auc_type="none")
Compute and return model performance metrics.
test_data
H2OFrame
Compute metrics on this dataset. Takes precedence over train, valid, and xval flags.
train
boolean
default:"False"
Return metrics from the training set.
valid
boolean
default:"False"
Return metrics from the validation set.
xval
boolean
default:"False"
Return cross-validation metrics.
auc_type
string
default:"none"
AUC type for multinomial classification when test_data is provided. One of: "auto", "none", "macro_ovr", "weighted_ovr", "macro_ovo", "weighted_ovo".
Returns a metrics object (subclass of MetricsBase) with metric accessors such as .auc(), .mse(), .rmse(), .logloss(), .r2(), .confusion_matrix().
perf = model.model_performance(test_data=test)
print(perf.auc())
print(perf.mse())
print(perf.confusion_matrix())

cross_validation_metrics_summary

summary = model.cross_validation_metrics_summary()
Return a summary table of cross-validation metrics across all folds. Available only when the model was trained with nfolds >= 2. Returns an H2OTwoDimTable with mean, standard deviation, and per-fold values for each metric.
cv_summary = model.cross_validation_metrics_summary()
cv_summary

Variable importance

varimp

importance = model.varimp(use_pandas=False)
Return variable importances for tree-based models (GBM, XGBoost, DRF) and linear models (GLM).
use_pandas
boolean
default:"False"
Return results as a Pandas DataFrame when True. Returns a list of tuples otherwise.
vi = model.varimp(use_pandas=True)
print(vi)

varimp_plot

model.varimp_plot(num_of_features=None, server=False, save_plot_path=None)
Plot variable importances as a horizontal bar chart.
num_of_features
integer
Maximum number of features to display. Shows all features if None.
server
boolean
default:"False"
Run in non-interactive server mode (saves plot instead of rendering).
save_plot_path
string
File path to save the plot image. Requires server=True or a file extension.
model.varimp_plot(num_of_features=20)

Explainability

explain

explanation = model.explain(frame, columns=None, top_n_features=10, include_explanations="ALL")
Generate a comprehensive suite of model explanations for a given dataset. Returns a collection of plots and tables including variable importance, partial dependence plots, SHAP values, and more.
frame
H2OFrame
required
Dataset to use for generating explanations.
top_n_features
integer
default:"10"
Number of top features to include in each explanation.
expl = model.explain(test)

explain_row

explanation = model.explain_row(frame, row_index, columns=None, top_n_features=10)
Generate explanations for a single row in a dataset. Useful for understanding individual predictions.
frame
H2OFrame
required
Dataset containing the row to explain.
row_index
integer
required
Zero-based index of the row to explain.
row_expl = model.explain_row(test, row_index=0)

Model export

download_mojo

path = model.download_mojo(path=".", get_genmodel_jar=False, genmodel_name="")
Download the model in MOJO (Model Object, Optimized) format to the local machine. MOJOs are compact binary files that can be used for scoring without an H2O cluster.
path
string
default:"."
Local directory where the MOJO file will be saved.
get_genmodel_jar
boolean
default:"False"
Also download h2o-genmodel.jar to the same directory. Required for scoring with the MOJO outside of H2O.
genmodel_name
string
Custom filename for the genmodel jar. Defaults to h2o-genmodel.jar.
Returns the filename of the saved MOJO file.
mojo_path = model.download_mojo(path="/tmp/mojo", get_genmodel_jar=True)
MOJO export is supported by GBM, XGBoost, DRF, GLM, DeepLearning, and Stacked Ensemble models.

download_pojo

path = model.download_pojo(path="", get_genmodel_jar=False, genmodel_name="")
Download the model as a Plain Old Java Object (POJO). If path is an empty string, the Java source code is printed to stdout instead.
path
string
default:""
Absolute path to the directory where the POJO .java file will be saved. When empty, prints to stdout.
get_genmodel_jar
boolean
default:"False"
Also download h2o-genmodel.jar to the same directory.
genmodel_name
string
Custom filename for the genmodel jar.
model.download_pojo(path="/tmp/pojo", get_genmodel_jar=True)

save_model_details

path = model.save_model_details(path="", force=False, filename=None)
Save detailed model information in JSON format to disk on the server’s file system.
path
string
default:""
Directory on the server where the JSON file will be saved.
force
boolean
default:"False"
Overwrite any existing file at the destination.
filename
string
Custom filename. Defaults to <model_id>.json.
Returns the path of the saved JSON file.

Parameters and metadata

params

model.params  # dict
Dictionary of all model parameters with their default, actual, and input values.
{
    "ntrees": {"default": 50, "actual": 100, "input": 100},
    "max_depth": {"default": 5, "actual": 5, "input": 5},
    ...
}

actual_params

model.actual_params  # dict
Dictionary of the actual parameter values used to train the model.
params = model.actual_params
print(params["ntrees"])
print(params["learn_rate"])

model_id

model.model_id  # str
The unique string identifier for this model in the H2O cluster.

type

model.type  # str
The model type. One of "classifier", "regressor", or "unsupervised".

have_mojo / have_pojo

model.have_mojo  # bool
model.have_pojo  # bool
Whether this model supports MOJO or POJO export.

Training metadata

scoring_history

history = model.scoring_history(as_data_frame=True)
Retrieve the model’s score history over training iterations.
as_data_frame
boolean
default:"True"
Return as a Pandas DataFrame. When False, returns an H2OTwoDimTable.

start_time / end_time / run_time

model.start_time   # int (milliseconds since epoch)
model.end_time     # int (milliseconds since epoch)
model.run_time     # int (milliseconds)
Timestamps and duration of model training.

Cross-validation

is_cross_validated

model.is_cross_validated()  # bool
Returns True if the model was trained with cross-validation.

xvals

cv_models = model.xvals  # list[H2OModel]
List of cross-validation sub-models.

SHAP values

predict_contributions

shap = model.predict_contributions(
    test_data,
    output_format="Original",
    top_n=None,
    bottom_n=None,
    compare_abs=False,
    background_frame=None,
    output_space=False
)
Compute SHAP (SHapley Additive exPlanations) feature contribution values. Available for GBM, XGBoost, and DRF models.
test_data
H2OFrame
required
Dataset to compute SHAP values for.
output_format
string
default:"Original"
Feature contribution output format for XGBoost. "Original" (per one-hot encoded feature) or "Compact" (per original feature).
top_n
integer
Return only the top N highest SHAP contributions. Pass -1 to sort all contributions in descending order.
bottom_n
integer
Return only the bottom N lowest SHAP contributions.
background_frame
H2OFrame
Reference frame for marginal SHAP (when output_per_reference=False) or baseline SHAP values.
Returns an H2OFrame with shape (nrows, nfeatures + 1). Each column is the SHAP contribution of one feature; the last column is the model bias.
contributions = model.predict_contributions(test)
contributions.head()

Build docs developers (and LLMs) love