Imagine you have a dataset too big to load into R, and you want a strong predictive model without hand-tuning a dozen algorithms. You would like to point a tool at the data, give it a time budget, and come back to a ranked list of trained models with the best one already assembled. That is the workflow h2o is built for, and this chapter shows how it works.
h2o is an open source, distributed in-memory machine learning platform written in Java with a thin R client. The key idea to hold onto is that the R package h2o does not run the algorithms inside R. Instead it starts (or connects to) a separate Java process called the H2O cluster, ships data and instructions to it, and reads results back. This split matters because it is what lets the same R code train a generalized linear model on a laptop and a gradient boosting machine on a multi-node cluster against data that never fits in R’s memory.
Intuition
Think of R as a remote control and the H2O cluster as the machine doing the work. Your R session holds only small pointers (the names of datasets and models); the heavy data and computation live in the Java process, possibly on another server entirely.
This chapter treats h2o as a production-oriented modeling engine. By the end you will know how to start and manage the cluster, move data into it as an H2OFrame, fit the four model families that cover most tabular work (generalized linear models, gradient boosting machines, random forests, and feed-forward deep learning), run the AutoML routine that searches and stacks those families automatically, and read the explainability tools that ship with the package. The algorithms themselves are covered in earlier chapters; here the emphasis is how they are exposed, scaled, and combined. A short base-R demonstration near the end makes the leaderboard-and-stacking idea concrete without needing a cluster at all.
Note
Because the algorithms in this chapter run inside a Java cluster started by h2o.init(), the h2o code chunks here are shown for reference and are not executed when the book is built (they need a live cluster). The one fully runnable example, the base-R leaderboard, deliberately uses only base packages so you can run it yourself.
97.1 Where this fits in a modern ML workflow
Most predictive work on tabular data follows the same loop: import, clean, split, train several model families, compare them on held-out data, then explain and deploy the winner. Two practical constraints shape the tooling:
The data may be larger than a single machine’s RAM, so computation has to be distributed and out-of-core.
The number of candidate models and hyperparameter settings is large, so the search should be partly automated.
h2o targets both. Data lives in the cluster as an H2OFrame, partitioned across the cluster’s cores and (optionally) nodes, compressed column by column. Algorithms are implemented in Java with distributed map-reduce, so a single h2o.gbm() call uses all available cores. AutoML wraps the model families in a time-budgeted search and a stacked ensemble, which gives a strong baseline with little code. The R session stays small: it holds pointers (frame and model identifiers), not the data.
Table 97.3 places h2o against the other engines used in this book.
Table 97.1: Comparison of h2o against the other modeling engines used in this book across compute location, support for larger-than-RAM data, built-in AutoML, breadth of model families, and model portability.
Aspect
h2o
tidymodels + engines
xgboost/lightgbm direct
Spark MLlib
Compute location
External JVM cluster
In-R, per engine
In-R (C++ backend)
External JVM cluster
Larger-than-RAM data
Yes (distributed frame)
No (in-R memory)
No
Yes
Built-in AutoML
Yes (h2o.automl)
Via tune + stacks (manual)
No
Limited
Model families in one API
GLM, GBM, RF, DL, stacked
Many via parsnip
Boosting only
Several
Language portability of models
MOJO/POJO (Java, any host)
R objects
Native boosters
JVM
The rows that distinguish h2o are larger-than-RAM data, a single API across model families, one-call AutoML, and exportable scoring artifacts (MOJO/POJO)1 that run wherever a JVM runs without R.
When to use this
Skim the table above first. If your data fits comfortably in memory and you want fine-grained control of the modeling pipeline, the tidymodels workflow (Chapter 90) is usually a better fit. Reach for h2o when the data is large, when you want a strong baseline from a single call, or when you need a portable scoring artifact.
97.2 Starting and managing the cluster
A session begins by initializing or connecting to the cluster. h2o.init() starts a local JVM if none is running; given an IP and port it connects to a remote one. The arguments that matter in practice are the number of threads (nthreads = -1 uses all cores) and the memory ceiling (max_mem_size), because the cluster will not grow past that ceiling and will spill or fail if a frame plus working memory exceeds it.
Show code
library(h2o)# Start a local cluster: all cores, 4 GB heap.h2o.init(nthreads =-1, max_mem_size ="4G")# Connect to a remote cluster instead:# h2o.init(ip = "10.0.0.12", port = 54321, startH2O = FALSE)h2o.clusterInfo()# cores, memory, version, node count
Two housekeeping habits prevent the most common problems. First, the cluster’s memory is separate from R’s, so frames and models accumulate in the JVM until you remove them with h2o.rm() or h2o.removeAll(). Second, the R client version and the running cluster version must match exactly; a mismatch is the usual cause of cryptic connection errors.
Warning
Removing an R object that points at a frame (for example with rm() in R) does not free the frame in the cluster. The data is still occupying JVM heap until you call h2o.rm() on it. Over a long session this is the most common way to run the cluster out of memory.
97.2.1 The H2OFrame
An H2OFrame is the cluster-side analogue of a data frame. It is stored by column, each column compressed independently, and split into row chunks distributed across cores. You manipulate it from R with familiar syntax (frame[ , "col"], frame[frame$y > 0, ], h2o.group_by), but the work happens in the cluster and only summaries return to R.
Show code
# Move an R data frame into the cluster.train_h2o<-as.h2o(train_df, destination_frame ="train")# Or import directly, bypassing R entirely (the file is read by the cluster):# train_h2o <- h2o.importFile("hdfs:///data/train.csv")# For classification the response must be a factor in the cluster.train_h2o["y"]<-as.factor(train_h2o["y"])x<-setdiff(colnames(train_h2o), "y")# predictor namesy<-"y"# response name
Specifying a model in h2o uses the names x (predictor columns) and y (response column) plus a training_frame, rather than an R formula. This is deliberate: passing column names instead of a formula avoids building a dense model matrix in memory, which is what makes wide, high-cardinality data feasible.2
Key idea
From here on, every model call follows the same template, supply x, y, and training_frame, then add the hyperparameters specific to that family. That uniformity is what makes the families easy to swap and what AutoML exploits when it searches over them.
97.3 The four core model families
h2o exposes four model families that together handle the great majority of tabular problems: a penalized linear model for interpretable, smooth effects, two tree ensembles for nonlinear structure and interactions, and a dense neural network for the cases where trees struggle with smooth high-order interactions. All four share the same call signature: column names, a training frame, an optional validation frame or cross-validation fold count, and family-specific hyperparameters. The shared signature is what makes them swappable, and it is the reason AutoML can treat them as interchangeable building blocks.
97.3.1 Generalized linear models
h2o.glm() fits the penalized GLM. For a response \(y_i\) with predictors \(\mathbf{x}_i \in \mathbb{R}^p\), a linear predictor \(\eta_i = \beta_0 + \mathbf{x}_i^\top \boldsymbol\beta\), and a link function \(g\) relating the conditional mean to the linear predictor through \(g(\mathbb{E}[y_i \mid \mathbf{x}_i]) = \eta_i\), the coefficients solve the penalized maximum likelihood problem
where \(\ell\) is the log-likelihood contribution for the chosen family (Gaussian, binomial, Poisson, gamma, and others), \(\lambda \ge 0\) is the overall penalty strength, and \(\alpha \in [0,1]\) mixes the lasso (\(\alpha = 1\)) and ridge (\(\alpha = 0\)) penalties. This is the elastic net penalty, solved here by a distributed iteratively reweighted least squares with coordinate descent. Setting lambda_search = TRUE traces the whole regularization path and picks \(\lambda\) by cross-validation.
Intuition
The penalty term is what keeps a linear model honest when there are many predictors. Ridge (\(\alpha = 0\)) shrinks coefficients smoothly toward zero; lasso (\(\alpha = 1\)) can set some exactly to zero and so performs variable selection. The elastic net mix lets you blend the two.
Show code
glm_fit<-h2o.glm( x =x, y =y, training_frame =train_h2o, family ="binomial", # logistic regression alpha =0.5, # elastic net mix lambda_search =TRUE, # choose lambda by CV nfolds =5, seed =1)h2o.performance(glm_fit, newdata =test_h2o)
97.3.2 Gradient boosting machines
h2o.gbm() fits a forward stagewise additive ensemble of regression trees, the algorithm of the boosting chapter (Chapter 11). Writing the model after \(m\) trees as \(F_m(\mathbf{x})\), each new tree \(h_m\) is fit to the negative gradient of the loss \(L\) evaluated at the current model, the so-called pseudo-residuals
The knobs that control the bias-variance trade-off are ntrees (number of stages \(m\)), learn_rate (\(\nu\)), max_depth (tree complexity), and the sampling rates sample_rate (rows) and col_sample_rate (columns), which inject the stochasticity that improves generalization.
Tip
A small learning rate with many trees almost always generalizes better than a large learning rate with few trees, at the cost of training time. Pair a small learn_rate with early stopping (stopping_rounds, shown in the code below) so the model stops adding trees once validation performance stops improving, which spends the budget without overfitting.
Show code
gbm_fit<-h2o.gbm( x =x, y =y, training_frame =train_h2o, ntrees =500, learn_rate =0.05, max_depth =5, sample_rate =0.8, col_sample_rate =0.8, stopping_rounds =5, stopping_metric ="AUC", # early stopping nfolds =5, seed =1)
97.3.3 Random forest
h2o.randomForest() fits a bagged ensemble of deep, decorrelated trees, the algorithm of the random forests chapter (Chapter 13). Each tree is grown on a bootstrap (or subsample) of rows, and at each split only a random subset of the predictors is considered. With \(B\) trees giving predictions \(\hat f_b(\mathbf{x})\), the ensemble averages them,
and the random predictor subset lowers the pairwise correlation between trees, which is what reduces the variance of the average below that of plain bagging. The main controls are ntrees, max_depth, and mtries (predictors tried per split, the source of the decorrelation).
Show code
rf_fit<-h2o.randomForest( x =x, y =y, training_frame =train_h2o, ntrees =500, max_depth =20, mtries =-1, # -1 = sqrt(p) for classification nfolds =5, seed =1)
97.3.4 Deep learning
h2o.deeplearning() fits a fully connected feed-forward network. With weight matrices \(W^{(\ell)}\), bias vectors \(b^{(\ell)}\), and an activation \(\sigma\), layer \(\ell\) computes \(a^{(\ell)} = \sigma\!\big(W^{(\ell)} a^{(\ell-1)} + b^{(\ell)}\big)\) with \(a^{(0)} = \mathbf{x}\). The H2O implementation is tuned for tabular data: it trains with the adaptive learning rate ADADELTA by default, uses dropout for regularization, and runs a distributed variant of stochastic gradient descent (HOGWILD!). It is not a substitute for the neural network architectures covered elsewhere, such as the transformer models (Chapter 38); it is the dense-tabular network you reach for when trees underfit smooth interactions.
Note
On most tabular problems the tree ensembles (GBM and random forest) are hard to beat, and they need far less tuning than a neural network. Treat h2o.deeplearning() as a complement worth adding to the AutoML search rather than a default first choice for tables.
Show code
dl_fit<-h2o.deeplearning( x =x, y =y, training_frame =train_h2o, hidden =c(64, 64), activation ="RectifierWithDropout", input_dropout_ratio =0.1, epochs =50, nfolds =5, seed =1)
97.4 AutoML
With the four families in hand, the natural next question is which to use and how to tune each one. AutoML answers it by trying many of them under a time budget and combining the results, so you get a strong model without choosing by hand.
h2o.automl() automates the comparison loop. Given a training frame, a response, and a stopping budget (a wall-clock limit max_runtime_secs or a model count max_models), it trains a sequence of GLMs, GBMs, random forests, extremely randomized trees, and deep nets, tunes their hyperparameters with random search, and then trains two kinds of stacked ensemble on top of the base learners: one over all models and one over the best model of each family.
The stacked ensemble is the model stacking of the earlier chapter (Chapter 93). Each base model \(\hat f_k\) produces cross-validated (out-of-fold) predictions \(z_{ik} = \hat f_k^{(-\text{fold}(i))}(\mathbf{x}_i)\), and a metalearner \(g\) (a non-negative GLM by default) is fit to combine them,
Using out-of-fold predictions to fit \(g\) is what keeps the metalearner honest: it never sees a base model’s prediction on a row that base model trained on, so the combination weights are estimated without leakage.3 The result is ranked on a leaderboard, sorted by a default metric (AUC for binary classification, deviance or RMSE for regression).
Key idea
AutoML’s two outputs are a leaderboard (every model it trained, ranked) and a leader (usually a stacked ensemble). The leaderboard is for understanding the landscape; the leader is the model you would deploy, subject to the serving-cost caveat below.
Show code
aml<-h2o.automl( x =x, y =y, training_frame =train_h2o, max_runtime_secs =300, # budget, or use max_models nfolds =5, sort_metric ="AUC", seed =1)lb<-h2o.get_leaderboard(aml, extra_columns ="ALL")print(lb)best<-aml@leader# usually a stacked ensembleh2o.performance(best, newdata =test_h2o)
A common pitfall is to read the leaderboard and conclude the ensemble is always best for deployment. The ensemble usually wins on accuracy, but it requires every base model at scoring time. When latency or portability matters, the best single GBM is often within a small margin and far cheaper to serve, so the right comparison is accuracy against serving cost, not accuracy alone.
97.5 Model explainability
A leaderboard tells you which model predicts best, but not why it predicts what it does. For that you need explainability tools, and h2o ships a set that works uniformly across the families. h2o.explain() produces a panel (leaderboard, variable importance, partial dependence, SHAP summary) for a model or an AutoML object. The individual pieces are also callable: h2o.varimp_plot() for tree-based importance, h2o.partialPlot() for partial dependence, and h2o.shap_summary_plot() for SHAP values on tree models.
Partial dependence of the model \(\hat f\) on a predictor subset \(\mathbf{x}_S\), marginalizing over the remaining predictors \(\mathbf{x}_C\), is the average
which holds the feature of interest fixed while sweeping the others over their observed values. This is the same partial dependence defined in the interpretable machine learning chapter (Chapter 35); h2o just computes it in the cluster.
Intuition
A partial dependence plot answers the question “as this one predictor changes, how does the model’s average prediction move, with everything else held at its typical mix?” It is the model’s marginal story about that feature.
97.6 A runnable demonstration: a base-R leaderboard
The h2o code above cannot run when this book is built, because it needs a live cluster. So to make the two central ideas concrete, the honest comparison of model families and the out-of-fold stack, this section rebuilds them from scratch in base R, with code you can run yourself. The AutoML leaderboard idea (train several model families with honest cross-validation, then rank them on a held-out metric) does not require the cluster to understand. The demonstration below reproduces the logic in base R: it simulates a binary classification problem, fits four simple model families with a common \(k\)-fold cross-validation harness, scores them by held-out accuracy, and builds a tiny stacked ensemble whose metalearner is fit on out-of-fold predictions. The point is to make the mechanics of a leaderboard and an out-of-fold stack concrete, not to compete with h2o on speed.
Show code
.libPaths(c("C:/Users/miken/R/library-4.4", .libPaths()))suppressPackageStartupMessages({library(MASS)library(class)library(knitr)})set.seed(1)# Simulate: two classes with a nonlinear boundary plus noise predictors.n<-1200x1<-rnorm(n); x2<-rnorm(n)x3<-rnorm(n); x4<-rnorm(n)# pure noiselin<-1.2*x1-0.8*x2+1.5*x1*x2# interaction => nonlinear boundaryp<-1/(1+exp(-lin))y<-factor(rbinom(n, 1, p), levels =c(0, 1))dat<-data.frame(y, x1, x2, x3, x4)K<-5folds<-sample(rep(1:K, length.out =n))# Generic K-fold harness: returns out-of-fold predicted probabilities of class 1.oof_prob<-function(fit_predict){oof<-numeric(n)for(kin1:K){tr<-which(folds!=k); te<-which(folds==k)oof[te]<-fit_predict(dat[tr, , drop =FALSE], dat[te, , drop =FALSE])}oof}# Model family 1: logistic regression (the GLM analogue).p_glm<-oof_prob(function(tr, te){m<-glm(y~x1+x2+x3+x4, data =tr, family =binomial)predict(m, te, type ="response")})# Model family 2: logistic regression with the true interaction term.p_glmx<-oof_prob(function(tr, te){m<-glm(y~x1*x2+x3+x4, data =tr, family =binomial)predict(m, te, type ="response")})# Model family 3: linear discriminant analysis.p_lda<-oof_prob(function(tr, te){m<-lda(y~x1+x2+x3+x4, data =tr)predict(m, te)$posterior[, "1"]})# Model family 4: k-nearest neighbours (k = 15), probability of class 1.p_knn<-oof_prob(function(tr, te){cl<-knn(tr[, c("x1","x2","x3","x4")], te[, c("x1","x2","x3","x4")], cl =tr$y, k =15, prob =TRUE)pr<-attr(cl, "prob")# prob of the winning classifelse(cl=="1", pr, 1-pr)# convert to prob of class 1})# Stacked ensemble: metalearner is a logistic regression on out-of-fold preds.truth<-as.integer(dat$y)-1Lmeta<-glm(truth~p_glm+p_glmx+p_lda+p_knn, family =binomial)p_stack<-predict(meta, type ="response")# Score each model by out-of-fold accuracy, AUC, and log-loss.acc<-function(pp)mean((pp>0.5)==(truth==1))auc<-function(pp){# Mann-Whitney form of AUCpos<-pp[truth==1]; neg<-pp[truth==0]mean(outer(pos, neg, ">")+0.5*outer(pos, neg, "=="))}logloss<-function(pp){pp<-pmin(pmax(pp, 1e-12), 1-1e-12)-mean(truth*log(pp)+(1-truth)*log(1-pp))}leaderboard<-data.frame( model =c("GLM", "GLM + interaction", "LDA", "kNN (k=15)", "Stacked ensemble"), accuracy =c(acc(p_glm), acc(p_glmx), acc(p_lda), acc(p_knn), acc(p_stack)), auc =c(auc(p_glm), auc(p_glmx), auc(p_lda), auc(p_knn), auc(p_stack)), logloss =c(logloss(p_glm), logloss(p_glmx), logloss(p_lda),logloss(p_knn), logloss(p_stack)))leaderboard<-leaderboard[order(-leaderboard$auc), ]rownames(leaderboard)<-NULLkable(leaderboard, digits =4, col.names =c("Model", "Accuracy", "AUC", "Log-loss"), caption ="Base-R leaderboard ranking four model families and their stacked ensemble by out-of-fold accuracy, AUC, and log-loss on the simulated binary classification problem.")
Table 97.2: Base-R leaderboard ranking four model families and their stacked ensemble by out-of-fold accuracy, AUC, and log-loss on the simulated binary classification problem.
Model
Accuracy
AUC
Log-loss
Stacked ensemble
0.7575
0.8364
0.4917
GLM + interaction
0.7575
0.8360
0.4845
kNN (k=15)
0.7317
0.7887
0.6043
GLM
0.7167
0.7522
0.5995
LDA
0.7150
0.7521
0.5996
Table 97.2 shows the patterns you expect from a real leaderboard. The plain GLM, which assumes a linear log-odds boundary, trails because the true boundary has an interaction; the GLM with the interaction term and the stacked ensemble lead. The stack rarely loses to its best member because the metalearner can down-weight the weak learners. Figure 97.1 plots the leaderboard so the gaps are visible at a glance.
Show code
lb<-leaderboard[order(leaderboard$auc), ]# ascending for a horizontal bar plotpar(mar =c(4, 9, 2, 1))cols<-ifelse(lb$model=="Stacked ensemble", "firebrick", "steelblue")bp<-barplot(lb$auc, horiz =TRUE, names.arg =lb$model, las =1, col =cols, xlim =c(0.5, 1), xlab ="Out-of-fold AUC", main ="Base-R model leaderboard")text(lb$auc, bp, labels =sprintf("%.3f", lb$auc), pos =2, col ="white", cex =0.9)abline(v =0.5, lty =3)# AUC of a random classifier
Figure 97.1: Out-of-fold AUC for each model family in the base-R leaderboard. Higher is better.
The takeaways carry over directly to h2o.automl(): honest comparison needs out-of-fold predictions, a stacked ensemble usually edges out its best base model, and the gain over a well-specified single model can be small enough that serving cost decides which to deploy.
97.7 Practical guidance, pitfalls, and when to use it
Reach for h2o when at least one of these holds: the data is too large for R’s memory, you want a strong tabular baseline from one h2o.automl() call, or you need a scoring artifact (MOJO/POJO) that runs on a JVM without R. For small data that fits in memory and a workflow you want full control over, tidymodels with the engines in this book is usually a better fit, and for boosting specifically, calling xgboost or lightgbm directly avoids the cluster overhead.
Recurring pitfalls:
Forgetting the response factor. For classification the response column must be a factor in the cluster (as.factor); a numeric 0/1 response makes h2o fit a regression and silently report regression metrics.
Cluster memory leaks. Frames and models persist in the JVM. Long sessions exhaust the heap; call h2o.rm() on intermediates or h2o.removeAll() between experiments.
Version skew. The R h2o package and the running cluster must be the same version. Reinstall the client when you upgrade the cluster.
Reading too much into AutoML accuracy. The leaderboard optimizes one metric on cross-validation; always confirm on a truly held-out frame and weigh ensemble accuracy against serving cost.
Set a seed and a reproducibility mode. Distributed training is only reproducible when you fix seed and, for deep learning, set reproducible = TRUE (which forces single-threaded training and is slow).
Leakage through preprocessing. If you impute or encode in R before as.h2o, do it on training rows only. Better, let h2o handle missing values and high-cardinality factors natively inside cross-validation.
97.8 Further reading
Hastie, Tibshirian, and Friedman (2009), The Elements of Statistical Learning, for the GLM, boosting, random forest, and stacking theory the h2o algorithms implement.
Friedman (2001), “Greedy Function Approximation: A Gradient Boosting Machine,” for the gradient boosting derivation.
Breiman (2001), “Random Forests,” for the bagging-plus-feature-subsampling argument.
Wolpert (1992), “Stacked Generalization,” and van der Laan, Polley, and Hubbard (2007), “Super Learner,” for the out-of-fold stacking that AutoML’s ensembles use.
LeDell and Poirier (2020), “H2O AutoML: Scalable Automatic Machine Learning,” for the design of h2o.automl.
Molnar (2022), Interpretable Machine Learning, for partial dependence and SHAP, the explainability tools exposed by h2o.explain().
The H2O.ai documentation and R package vignettes for current arguments, defaults, and the MOJO/POJO export workflow.
Imagine you have a dataset too big to load into R, and you want a strong predictive model without hand-tuning a dozen algorithms. You would like to point a tool at the data, give it a time budget, and come back to a ranked list of trained models with the best one already assembled. That is the workflow h2o is built for, and this chapter shows how it works.
h2o is an open source, distributed in-memory machine learning platform written in Java with a thin R client. The key idea to hold onto is that the R package h2o does not run the algorithms inside R. Instead it starts (or connects to) a separate Java process called the H2O cluster, ships data and instructions to it, and reads results back. This split matters because it is what lets the same R code train a generalized linear model on a laptop and a gradient boosting machine on a multi-node cluster against data that never fits in R’s memory.
Intuition
Think of R as a remote control and the H2O cluster as the machine doing the work. Your R session holds only small pointers (the names of datasets and models); the heavy data and computation live in the Java process, possibly on another server entirely.
This chapter treats h2o as a production-oriented modeling engine. By the end you will know how to start and manage the cluster, move data into it as an H2OFrame, fit the four model families that cover most tabular work (generalized linear models, gradient boosting machines, random forests, and feed-forward deep learning), run the AutoML routine that searches and stacks those families automatically, and read the explainability tools that ship with the package. The algorithms themselves are covered in earlier chapters; here the emphasis is how they are exposed, scaled, and combined. A short base-R demonstration near the end makes the leaderboard-and-stacking idea concrete without needing a cluster at all.
Note
Because the algorithms in this chapter run inside a Java cluster started by h2o.init(), the h2o code chunks here are shown for reference and are not executed when the book is built (they need a live cluster). The one fully runnable example, the base-R leaderboard, deliberately uses only base packages so you can run it yourself.
97.9 Where this fits in a modern ML workflow
Most predictive work on tabular data follows the same loop: import, clean, split, train several model families, compare them on held-out data, then explain and deploy the winner. Two practical constraints shape the tooling:
The data may be larger than a single machine’s RAM, so computation has to be distributed and out-of-core.
The number of candidate models and hyperparameter settings is large, so the search should be partly automated.
h2o targets both. Data lives in the cluster as an H2OFrame, partitioned across the cluster’s cores and (optionally) nodes, compressed column by column. Algorithms are implemented in Java with distributed map-reduce, so a single h2o.gbm() call uses all available cores. AutoML wraps the model families in a time-budgeted search and a stacked ensemble, which gives a strong baseline with little code. The R session stays small: it holds pointers (frame and model identifiers), not the data.
Table 97.3 places h2o against the other engines used in this book.
Table 97.3: Comparison of h2o against the other modeling engines used in this book across compute location, support for larger-than-RAM data, built-in AutoML, breadth of model families, and model portability.
Aspect
h2o
tidymodels + engines
xgboost/lightgbm direct
Spark MLlib
Compute location
External JVM cluster
In-R, per engine
In-R (C++ backend)
External JVM cluster
Larger-than-RAM data
Yes (distributed frame)
No (in-R memory)
No
Yes
Built-in AutoML
Yes (h2o.automl)
Via tune + stacks (manual)
No
Limited
Model families in one API
GLM, GBM, RF, DL, stacked
Many via parsnip
Boosting only
Several
Language portability of models
MOJO/POJO (Java, any host)
R objects
Native boosters
JVM
The rows that distinguish h2o are larger-than-RAM data, a single API across model families, one-call AutoML, and exportable scoring artifacts (MOJO/POJO)4 that run wherever a JVM runs without R.
When to use this
Skim the table above first. If your data fits comfortably in memory and you want fine-grained control of the modeling pipeline, the tidymodels workflow (Chapter 90) is usually a better fit. Reach for h2o when the data is large, when you want a strong baseline from a single call, or when you need a portable scoring artifact.
97.10 Starting and managing the cluster
A session begins by initializing or connecting to the cluster. h2o.init() starts a local JVM if none is running; given an IP and port it connects to a remote one. The arguments that matter in practice are the number of threads (nthreads = -1 uses all cores) and the memory ceiling (max_mem_size), because the cluster will not grow past that ceiling and will spill or fail if a frame plus working memory exceeds it.
```{r h2o-init, eval=FALSE} library(h2o)
98 Start a local cluster: all cores, 4 GB heap.
h2o.init(nthreads = -1, max_mem_size = “4G”)
99 Connect to a remote cluster instead:
100 h2o.init(ip = “10.0.0.12”, port = 54321, startH2O = FALSE)
Two housekeeping habits prevent the most common problems. First, the cluster's memory is separate from R's, so frames and models accumulate in the JVM until you remove them with `h2o.rm()` or `h2o.removeAll()`. Second, the R client version and the running cluster version must match exactly; a mismatch is the usual cause of cryptic connection errors.
::: {.callout-warning}
Removing an R object that points at a frame (for example with `rm()` in R) does not free the frame in the cluster. The data is still occupying JVM heap until you call `h2o.rm()` on it. Over a long session this is the most common way to run the cluster out of memory.
:::
### The H2OFrame
An `H2OFrame` is the cluster-side analogue of a data frame. It is stored by column, each column compressed independently, and split into row chunks distributed across cores. You manipulate it from R with familiar syntax (`frame[ , "col"]`, `frame[frame$y > 0, ]`, `h2o.group_by`), but the work happens in the cluster and only summaries return to R.
```{r h2o-frame, eval=FALSE}
# Move an R data frame into the cluster.
train_h2o <- as.h2o(train_df, destination_frame = "train")
# Or import directly, bypassing R entirely (the file is read by the cluster):
# train_h2o <- h2o.importFile("hdfs:///data/train.csv")
# For classification the response must be a factor in the cluster.
train_h2o["y"] <- as.factor(train_h2o["y"])
x <- setdiff(colnames(train_h2o), "y") # predictor names
y <- "y" # response name
Specifying a model in h2o uses the names x (predictor columns) and y (response column) plus a training_frame, rather than an R formula. This is deliberate: passing column names instead of a formula avoids building a dense model matrix in memory, which is what makes wide, high-cardinality data feasible.5
Key idea
From here on, every model call follows the same template, supply x, y, and training_frame, then add the hyperparameters specific to that family. That uniformity is what makes the families easy to swap and what AutoML exploits when it searches over them.
100.1 The four core model families
h2o exposes four model families that together handle the great majority of tabular problems: a penalized linear model for interpretable, smooth effects, two tree ensembles for nonlinear structure and interactions, and a dense neural network for the cases where trees struggle with smooth high-order interactions. All four share the same call signature: column names, a training frame, an optional validation frame or cross-validation fold count, and family-specific hyperparameters. The shared signature is what makes them swappable, and it is the reason AutoML can treat them as interchangeable building blocks.
100.1.1 Generalized linear models
h2o.glm() fits the penalized GLM. For a response \(y_i\) with predictors \(\mathbf{x}_i \in \mathbb{R}^p\), a linear predictor \(\eta_i = \beta_0 + \mathbf{x}_i^\top \boldsymbol\beta\), and a link function \(g\) relating the conditional mean to the linear predictor through \(g(\mathbb{E}[y_i \mid \mathbf{x}_i]) = \eta_i\), the coefficients solve the penalized maximum likelihood problem
where \(\ell\) is the log-likelihood contribution for the chosen family (Gaussian, binomial, Poisson, gamma, and others), \(\lambda \ge 0\) is the overall penalty strength, and \(\alpha \in [0,1]\) mixes the lasso (\(\alpha = 1\)) and ridge (\(\alpha = 0\)) penalties. This is the elastic net penalty, solved here by a distributed iteratively reweighted least squares with coordinate descent. Setting lambda_search = TRUE traces the whole regularization path and picks \(\lambda\) by cross-validation.
Intuition
The penalty term is what keeps a linear model honest when there are many predictors. Ridge (\(\alpha = 0\)) shrinks coefficients smoothly toward zero; lasso (\(\alpha = 1\)) can set some exactly to zero and so performs variable selection. The elastic net mix lets you blend the two.
{r h2o-glm, eval=FALSE} glm_fit <- h2o.glm( x = x, y = y, training_frame = train_h2o, family = "binomial", # logistic regression alpha = 0.5, # elastic net mix lambda_search = TRUE, # choose lambda by CV nfolds = 5, seed = 1 ) h2o.performance(glm_fit, newdata = test_h2o)
100.1.2 Gradient boosting machines
h2o.gbm() fits a forward stagewise additive ensemble of regression trees, the algorithm of the boosting chapter (Chapter 11). Writing the model after \(m\) trees as \(F_m(\mathbf{x})\), each new tree \(h_m\) is fit to the negative gradient of the loss \(L\) evaluated at the current model, the so-called pseudo-residuals
The knobs that control the bias-variance trade-off are ntrees (number of stages \(m\)), learn_rate (\(\nu\)), max_depth (tree complexity), and the sampling rates sample_rate (rows) and col_sample_rate (columns), which inject the stochasticity that improves generalization.
Tip
A small learning rate with many trees almost always generalizes better than a large learning rate with few trees, at the cost of training time. Pair a small learn_rate with early stopping (stopping_rounds, shown in the code below) so the model stops adding trees once validation performance stops improving, which spends the budget without overfitting.
h2o.randomForest() fits a bagged ensemble of deep, decorrelated trees, the algorithm of the random forests chapter (Chapter 13). Each tree is grown on a bootstrap (or subsample) of rows, and at each split only a random subset of the predictors is considered. With \(B\) trees giving predictions \(\hat f_b(\mathbf{x})\), the ensemble averages them,
and the random predictor subset lowers the pairwise correlation between trees, which is what reduces the variance of the average below that of plain bagging. The main controls are ntrees, max_depth, and mtries (predictors tried per split, the source of the decorrelation).
{r h2o-rf, eval=FALSE} rf_fit <- h2o.randomForest( x = x, y = y, training_frame = train_h2o, ntrees = 500, max_depth = 20, mtries = -1, # -1 = sqrt(p) for classification nfolds = 5, seed = 1 )
100.1.4 Deep learning
h2o.deeplearning() fits a fully connected feed-forward network. With weight matrices \(W^{(\ell)}\), bias vectors \(b^{(\ell)}\), and an activation \(\sigma\), layer \(\ell\) computes \(a^{(\ell)} = \sigma\!\big(W^{(\ell)} a^{(\ell-1)} + b^{(\ell)}\big)\) with \(a^{(0)} = \mathbf{x}\). The H2O implementation is tuned for tabular data: it trains with the adaptive learning rate ADADELTA by default, uses dropout for regularization, and runs a distributed variant of stochastic gradient descent (HOGWILD!). It is not a substitute for the neural network architectures covered elsewhere, such as the transformer models (Chapter 38); it is the dense-tabular network you reach for when trees underfit smooth interactions.
Note
On most tabular problems the tree ensembles (GBM and random forest) are hard to beat, and they need far less tuning than a neural network. Treat h2o.deeplearning() as a complement worth adding to the AutoML search rather than a default first choice for tables.
With the four families in hand, the natural next question is which to use and how to tune each one. AutoML answers it by trying many of them under a time budget and combining the results, so you get a strong model without choosing by hand.
h2o.automl() automates the comparison loop. Given a training frame, a response, and a stopping budget (a wall-clock limit max_runtime_secs or a model count max_models), it trains a sequence of GLMs, GBMs, random forests, extremely randomized trees, and deep nets, tunes their hyperparameters with random search, and then trains two kinds of stacked ensemble on top of the base learners: one over all models and one over the best model of each family.
The stacked ensemble is the model stacking of the earlier chapter (Chapter 93). Each base model \(\hat f_k\) produces cross-validated (out-of-fold) predictions \(z_{ik} = \hat f_k^{(-\text{fold}(i))}(\mathbf{x}_i)\), and a metalearner \(g\) (a non-negative GLM by default) is fit to combine them,
Using out-of-fold predictions to fit \(g\) is what keeps the metalearner honest: it never sees a base model’s prediction on a row that base model trained on, so the combination weights are estimated without leakage.6 The result is ranked on a leaderboard, sorted by a default metric (AUC for binary classification, deviance or RMSE for regression).
Key idea
AutoML’s two outputs are a leaderboard (every model it trained, ranked) and a leader (usually a stacked ensemble). The leaderboard is for understanding the landscape; the leader is the model you would deploy, subject to the serving-cost caveat below.
```{r h2o-automl, eval=FALSE} aml <- h2o.automl( x = x, y = y, training_frame = train_h2o, max_runtime_secs = 300, # budget, or use max_models nfolds = 5, sort_metric = “AUC”, seed = 1 )
best <- aml@leader # usually a stacked ensemble h2o.performance(best, newdata = test_h2o)
A common pitfall is to read the leaderboard and conclude the ensemble is always best for deployment. The ensemble usually wins on accuracy, but it requires every base model at scoring time. When latency or portability matters, the best single GBM is often within a small margin and far cheaper to serve, so the right comparison is accuracy against serving cost, not accuracy alone.
## Model explainability
A leaderboard tells you which model predicts best, but not why it predicts what it does. For that you need explainability tools, and `h2o` ships a set that works uniformly across the families. `h2o.explain()` produces a panel (leaderboard, variable importance, partial dependence, SHAP summary) for a model or an AutoML object. The individual pieces are also callable: `h2o.varimp_plot()` for tree-based importance, `h2o.partialPlot()` for partial dependence, and `h2o.shap_summary_plot()` for SHAP values on tree models.
Partial dependence of the model $\hat f$ on a predictor subset $\mathbf{x}_S$, marginalizing over the remaining predictors $\mathbf{x}_C$, is the average
$$
\widehat{\text{PD}}_S(\mathbf{x}_S) = \frac{1}{n}\sum_{i=1}^{n} \hat f\big(\mathbf{x}_S, \mathbf{x}_{C,i}\big),
$$
which holds the feature of interest fixed while sweeping the others over their observed values. This is the same partial dependence defined in the interpretable machine learning chapter (@sec-interpretable-machine-learning); `h2o` just computes it in the cluster.
::: {.callout-tip title="Intuition"}
A partial dependence plot answers the question "as this one predictor changes, how does the model's average prediction move, with everything else held at its typical mix?" It is the model's marginal story about that feature.
:::
```{r h2o-explain, eval=FALSE}
h2o.explain(aml, test_h2o) # full panel for the whole leaderboard
h2o.explain(best, test_h2o) # panel for one model
h2o.varimp_plot(gbm_fit)
h2o.partialPlot(gbm_fit, data = train_h2o, cols = c("x1", "x2"))
h2o.shap_summary_plot(gbm_fit, newdata = test_h2o)
After modeling, shut the cluster down so the JVM does not linger:
100.3 A runnable demonstration: a base-R leaderboard
The h2o code above cannot run when this book is built, because it needs a live cluster. So to make the two central ideas concrete, the honest comparison of model families and the out-of-fold stack, this section rebuilds them from scratch in base R, with code you can run yourself. The AutoML leaderboard idea (train several model families with honest cross-validation, then rank them on a held-out metric) does not require the cluster to understand. The demonstration below reproduces the logic in base R: it simulates a binary classification problem, fits four simple model families with a common \(k\)-fold cross-validation harness, scores them by held-out accuracy, and builds a tiny stacked ensemble whose metalearner is fit on out-of-fold predictions. The point is to make the mechanics of a leaderboard and an out-of-fold stack concrete, not to compete with h2o on speed.
leaderboard <- data.frame( model = c(“GLM”, “GLM + interaction”, “LDA”, “kNN (k=15)”, “Stacked ensemble”), accuracy = c(acc(p_glm), acc(p_glmx), acc(p_lda), acc(p_knn), acc(p_stack)), auc = c(auc(p_glm), auc(p_glmx), auc(p_lda), auc(p_knn), auc(p_stack)), logloss = c(logloss(p_glm), logloss(p_glmx), logloss(p_lda), logloss(p_knn), logloss(p_stack)) ) leaderboard <- leaderboard[order(-leaderboard$auc), ] rownames(leaderboard) <- NULL kable(leaderboard, digits = 4, col.names = c(“Model”, “Accuracy”, “AUC”, “Log-loss”), caption = “Base-R leaderboard ranking four model families and their stacked ensemble by out-of-fold accuracy, AUC, and log-loss on the simulated binary classification problem.”)
@tbl-h2o-automl-leaderboard shows the patterns you expect from a real leaderboard. The plain GLM, which assumes a linear log-odds boundary, trails because the true boundary has an interaction; the GLM with the interaction term and the stacked ensemble lead. The stack rarely loses to its best member because the metalearner can down-weight the weak learners. @fig-h2o-automl-leaderboard-plot plots the leaderboard so the gaps are visible at a glance.
```{r fig-h2o-automl-leaderboard-plot, eval=TRUE, fig.cap="Out-of-fold AUC for each model family in the base-R leaderboard. Higher is better.", fig.width=7, fig.height=4}
lb <- leaderboard[order(leaderboard$auc), ] # ascending for a horizontal bar plot
par(mar = c(4, 9, 2, 1))
cols <- ifelse(lb$model == "Stacked ensemble", "firebrick", "steelblue")
bp <- barplot(lb$auc, horiz = TRUE, names.arg = lb$model, las = 1,
col = cols, xlim = c(0.5, 1), xlab = "Out-of-fold AUC",
main = "Base-R model leaderboard")
text(lb$auc, bp, labels = sprintf("%.3f", lb$auc), pos = 2, col = "white", cex = 0.9)
abline(v = 0.5, lty = 3) # AUC of a random classifier
The takeaways carry over directly to h2o.automl(): honest comparison needs out-of-fold predictions, a stacked ensemble usually edges out its best base model, and the gain over a well-specified single model can be small enough that serving cost decides which to deploy.
108.1 Practical guidance, pitfalls, and when to use it
Reach for h2o when at least one of these holds: the data is too large for R’s memory, you want a strong tabular baseline from one h2o.automl() call, or you need a scoring artifact (MOJO/POJO) that runs on a JVM without R. For small data that fits in memory and a workflow you want full control over, tidymodels with the engines in this book is usually a better fit, and for boosting specifically, calling xgboost or lightgbm directly avoids the cluster overhead.
Recurring pitfalls:
Forgetting the response factor. For classification the response column must be a factor in the cluster (as.factor); a numeric 0/1 response makes h2o fit a regression and silently report regression metrics.
Cluster memory leaks. Frames and models persist in the JVM. Long sessions exhaust the heap; call h2o.rm() on intermediates or h2o.removeAll() between experiments.
Version skew. The R h2o package and the running cluster must be the same version. Reinstall the client when you upgrade the cluster.
Reading too much into AutoML accuracy. The leaderboard optimizes one metric on cross-validation; always confirm on a truly held-out frame and weigh ensemble accuracy against serving cost.
Set a seed and a reproducibility mode. Distributed training is only reproducible when you fix seed and, for deep learning, set reproducible = TRUE (which forces single-threaded training and is slow).
Leakage through preprocessing. If you impute or encode in R before as.h2o, do it on training rows only. Better, let h2o handle missing values and high-cardinality factors natively inside cross-validation.
108.2 Further reading
Hastie, Tibshirian, and Friedman (2009), The Elements of Statistical Learning, for the GLM, boosting, random forest, and stacking theory the h2o algorithms implement.
Friedman (2001), “Greedy Function Approximation: A Gradient Boosting Machine,” for the gradient boosting derivation.
Breiman (2001), “Random Forests,” for the bagging-plus-feature-subsampling argument.
Wolpert (1992), “Stacked Generalization,” and van der Laan, Polley, and Hubbard (2007), “Super Learner,” for the out-of-fold stacking that AutoML’s ensembles use.
LeDell and Poirier (2020), “H2O AutoML: Scalable Automatic Machine Learning,” for the design of h2o.automl.
Molnar (2022), Interpretable Machine Learning, for partial dependence and SHAP, the explainability tools exposed by h2o.explain().
The H2O.ai documentation and R package vignettes for current arguments, defaults, and the MOJO/POJO export workflow.
:::
Use this book with your AI assistant
Ask questions about this book in Claude, Cursor, VS Code, or ChatGPT, with answers that cite the section.
A MOJO (Model Object, Optimized) or POJO (Plain Old Java Object) is a self-contained file that encodes a trained model so it can score new rows on any machine with a Java runtime, with no R, no Python, and no H2O cluster required. This is what makes h2o models easy to embed in production services.↩︎
An R formula like y ~ . forces R to expand every factor into dummy columns up front, producing a dense design matrix. With thousands of categories that matrix can be larger than the original data. The column-name interface lets the cluster handle factors lazily and sparsely instead.↩︎
If you fit the metalearner on in-sample predictions instead, a model that memorized the training data would look brilliant and get a large weight, then fail on new data. The out-of-fold trick is the whole reason stacking generalizes.↩︎
A MOJO (Model Object, Optimized) or POJO (Plain Old Java Object) is a self-contained file that encodes a trained model so it can score new rows on any machine with a Java runtime, with no R, no Python, and no H2O cluster required. This is what makes h2o models easy to embed in production services.↩︎
An R formula like y ~ . forces R to expand every factor into dummy columns up front, producing a dense design matrix. With thousands of categories that matrix can be larger than the original data. The column-name interface lets the cluster handle factors lazily and sparsely instead.↩︎
If you fit the metalearner on in-sample predictions instead, a model that memorized the training data would look brilliant and get a large weight, then fail on new data. The out-of-fold trick is the whole reason stacking generalizes.↩︎