H2O is a fully open source, distributed in-memory machine learning platform with linear scalability. H2O supports the most widely used statistical & machine learning algorithms including gradient boosted machines, generalized linear models, deep learning and more. H2O also has an industry leading AutoML functionality that automatically runs through all the algorithms and their hyperparameters to produce a leaderboard of the best models. The H2O platform is used by over 18,000 organizations globally and is extremely popular in both the R & Python communities.
Algorithms developed from the ground up for distributed computing and for both supervised and unsupervised approaches including Random Forest, GLM, GBM, XGBoost, GLRM, Word2Vec and many more.
Train the best model in the least amount of time to save human hours. Reduce the need for expertise in machine learning by reducing the manual code-writing time. Improve the performance of machine learning models. Increase reproducibility and establish a baseline for scientific research or applications. Scales training data set to clusters (Hadoop, Spark, Kubernetes)
Normally we follow these steps:
With H2O, we have additional steps to initialize the package and convert data into H2O frame.
First, copy the code in h2o website and paste it into the console.
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
#install one time only
#install.packages("h2o")
#install bit64, otherwise it gives an error
#install.packages('bit64', repos = "https://cran.rstudio.com")
library(bit64)
library(tidyverse)
library(ggplot2)
library(h2o)
# Initialize h2o cluster
h2o.init()
## Connection successful!
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 8 days 4 hours
## H2O cluster timezone: Europe/Istanbul
## H2O data parsing timezone: UTC
## H2O cluster version: 3.30.1.1
## H2O cluster version age: 30 days
## H2O cluster name: H2O_started_from_R_ilaydacelenk_dvp184
## H2O cluster total nodes: 1
## H2O cluster total memory: 3.64 GB
## H2O cluster total cores: 8
## H2O cluster allowed cores: 8
## H2O cluster healthy: TRUE
## H2O Connection ip: localhost
## H2O Connection port: 54321
## H2O Connection proxy: NA
## H2O Internal Security: FALSE
## H2O API Extensions: Amazon S3, XGBoost, Algos, AutoML, Core V3, TargetEncoder, Core V4
## R Version: R version 4.0.1 (2020-06-06)
# To connect to an established H2O cluster
# h2o.init(ip = "123.45.67.89", port = 54321)
# Cluster info
h2o.clusterInfo()
## R is connected to the H2O cluster:
## H2O cluster uptime: 8 days 4 hours
## H2O cluster timezone: Europe/Istanbul
## H2O data parsing timezone: UTC
## H2O cluster version: 3.30.1.1
## H2O cluster version age: 30 days
## H2O cluster name: H2O_started_from_R_ilaydacelenk_dvp184
## H2O cluster total nodes: 1
## H2O cluster total memory: 3.64 GB
## H2O cluster total cores: 8
## H2O cluster allowed cores: 8
## H2O cluster healthy: TRUE
## H2O Connection ip: localhost
## H2O Connection port: 54321
## H2O Connection proxy: NA
## H2O Internal Security: FALSE
## H2O API Extensions: Amazon S3, XGBoost, Algos, AutoML, Core V3, TargetEncoder, Core V4
## R Version: R version 4.0.1 (2020-06-06)
Data preparation/manipulation can be made using the methods available in h2o package.
as.data.frame(): Converts H2O data frame into R data frameas.h2o(): Transfers data from R to the H2O instancestr.H2OFrame(): Returns the elements of the new objecth2o.describe() : Returns information about column typesSome of the models H2O supports:
Iris is a built-in dataset in R. It has 4 numerical independent variables and a categorical independent variable. Therefore this is a classification problem, not a regression. This data has 150 rows and 5 columns.
data <- iris
glimpse(data)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, …
data %>% count(Species)
## Species n
## 1 setosa 50
## 2 versicolor 50
## 3 virginica 50
# Convert data to h2o frame
iris_data <- as.h2o(iris)
h2o.describe(iris_data)
## Label Type Missing Zeros PosInf NegInf Min Max Mean Sigma
## 1 Sepal.Length real 0 0 0 0 4.3 7.9 5.843333 0.8280661
## 2 Sepal.Width real 0 0 0 0 2.0 4.4 3.057333 0.4358663
## 3 Petal.Length real 0 0 0 0 1.0 6.9 3.758000 1.7652982
## 4 Petal.Width real 0 0 0 0 0.1 2.5 1.199333 0.7622377
## 5 Species enum 0 50 0 0 0.0 2.0 NA NA
## Cardinality
## 1 NA
## 2 NA
## 3 NA
## 4 NA
## 5 3
H2O package has h2o.splitFrame() function to split the H2O data frame.
# 80% train, 20% test
# Seed is defined in order to get the same result
split = h2o.splitFrame(data = iris_data, ratios = 0.8, seed=90)
train = split[[1]]
test = split[[2]]
Here the dependent variable is Sepal.Length and it’s numerical. After fitting the model, we can reach some statistics about it. One of the most important statistic is Mean Squared Error (MSE).
# x=features, y=target, we chose the target to be Sepal.Length
gbm_model <- h2o.gbm(x=2:5, y=1, training_frame = train, ntrees = 10, max_depth = 3,min_rows = 2, learn_rate = 0.2, distribution= "gaussian", seed=90)
gbm_model
## Model Details:
## ==============
##
## H2ORegressionModel: gbm
## Model ID: GBM_model_R_1598965103261_107
## Model Summary:
## number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1 10 10 1541 3
## max_depth mean_depth min_leaves max_leaves mean_leaves
## 1 3 3.00000 7 8 7.70000
##
##
## H2ORegressionMetrics: gbm
## ** Reported on training data. **
##
## MSE: 0.0673553
## RMSE: 0.259529
## MAE: 0.2143559
## RMSLE: 0.03822407
## Mean Residual Deviance : 0.0673553
We could also predict the categorical variable Species. Here we can see the confusion matrix.
# x=features, y=target, we chose the target to be Species
gbm_model2 <- h2o.gbm(y = 5, x = 1:4, training_frame
= train, ntrees = 15, max_depth = 5, min_rows =
2, learn_rate = 0.01, distribution= "multinomial", seed=90)
gbm_model2
## Model Details:
## ==============
##
## H2OMultinomialModel: gbm
## Model ID: GBM_model_R_1598965103261_108
## Model Summary:
## number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1 15 45 7560 1
## max_depth mean_depth min_leaves max_leaves mean_leaves
## 1 5 4.66667 2 15 8.80000
##
##
## H2OMultinomialMetrics: gbm
## ** Reported on training data. **
##
## Training Set Metrics:
## =====================
##
## Extract training frame with `h2o.getFrame("RTMP_sid_a248_3")`
## MSE: (Extract with `h2o.mse`) 0.3284647
## RMSE: (Extract with `h2o.rmse`) 0.5731184
## Logloss: (Extract with `h2o.logloss`) 0.8513657
## Mean Per-Class Error: 0
## R^2: (Extract with `h2o.r2`) 0.5092805
## Confusion Matrix: Extract with `h2o.confusionMatrix(<model>,train = TRUE)`)
## =========================================================================
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
## setosa versicolor virginica Error Rate
## setosa 40 0 0 0.0000 = 0 / 40
## versicolor 0 40 0 0.0000 = 0 / 40
## virginica 0 0 41 0.0000 = 0 / 41
## Totals 40 40 41 0.0000 = 0 / 121
##
## Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>,train = TRUE)`
## =======================================================================
## Top-3 Hit Ratios:
## k hit_ratio
## 1 1 1.000000
## 2 2 1.000000
## 3 3 1.000000
# Model's performance using test data, includes R^2 and confusion matrix
perf <- h2o.performance(gbm_model,test)
perf
## H2ORegressionMetrics: gbm
##
## MSE: 0.1367617
## RMSE: 0.369813
## MAE: 0.3225538
## RMSLE: 0.05592245
## Mean Residual Deviance : 0.1367617
perf2 <- h2o.performance(gbm_model2,test)
perf2
## H2OMultinomialMetrics: gbm
##
## Test Set Metrics:
## =====================
##
## MSE: (Extract with `h2o.mse`) 0.3433333
## RMSE: (Extract with `h2o.rmse`) 0.5859465
## Logloss: (Extract with `h2o.logloss`) 0.8835027
## Mean Per-Class Error: 0.06666667
## R^2: (Extract with `h2o.r2`) 0.4750121
## Confusion Matrix: Extract with `h2o.confusionMatrix(<model>, <data>)`)
## =========================================================================
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
## setosa versicolor virginica Error Rate
## setosa 10 0 0 0.0000 = 0 / 10
## versicolor 0 8 2 0.2000 = 2 / 10
## virginica 0 0 9 0.0000 = 0 / 9
## Totals 10 8 11 0.0690 = 2 / 29
##
## Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>, <data>)`
## =======================================================================
## Top-3 Hit Ratios:
## k hit_ratio
## 1 1 0.931034
## 2 2 1.000000
## 3 3 1.000000
Let’s fit Random Forest model into our train set and try to predict Species.
# x=features, y=target
glm_model <- h2o.glm(x=c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), y="Species", training_frame = train, family ="multinomial", nfolds = 10, seed=90)
glm_model
## Model Details:
## ==============
##
## H2OMultinomialModel: glm
## Model ID: GLM_model_R_1598965103261_109
## GLM Model: summary
## family link regularization
## 1 multinomial multinomial Elastic Net (alpha = 0.5, lambda = 8.653E-4 )
## number_of_predictors_total number_of_active_predictors number_of_iterations
## 1 15 11 19
## training_frame
## 1 RTMP_sid_a248_3
##
## Coefficients: glm multinomial coefficients
## names coefs_class_0 coefs_class_1 coefs_class_2 std_coefs_class_0
## 1 Intercept 1.071374 -1.260527 -19.417966 -3.989312
## 2 Sepal.Length -1.363125 1.391185 0.000000 -1.101293
## 3 Sepal.Width 4.883751 -0.963682 -3.419822 2.109079
## 4 Petal.Length -1.855808 -0.657764 3.332561 -3.277225
## 5 Petal.Width -4.221242 -1.446903 7.327886 -3.218247
## std_coefs_class_1 std_coefs_class_2
## 1 -0.278372 -8.555442
## 2 1.123964 0.000000
## 3 -0.416172 -1.476872
## 4 -1.161565 5.885066
## 5 -1.103109 5.586733
##
## H2OMultinomialMetrics: glm
## ** Reported on training data. **
##
## Training Set Metrics:
## =====================
##
## Extract training frame with `h2o.getFrame("RTMP_sid_a248_3")`
## MSE: (Extract with `h2o.mse`) 0.01356239
## RMSE: (Extract with `h2o.rmse`) 0.1164577
## Logloss: (Extract with `h2o.logloss`) 0.05513559
## Mean Per-Class Error: 0.02479675
## Null Deviance: (Extract with `h2o.nulldeviance`) 265.8477
## Residual Deviance: (Extract with `h2o.residual_deviance`) 13.34281
## R^2: (Extract with `h2o.r2`) 0.9797381
## AIC: (Extract with `h2o.aic`) NaN
## Confusion Matrix: Extract with `h2o.confusionMatrix(<model>,train = TRUE)`)
## =========================================================================
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
## setosa versicolor virginica Error Rate
## setosa 40 0 0 0.0000 = 0 / 40
## versicolor 0 38 2 0.0500 = 2 / 40
## virginica 0 1 40 0.0244 = 1 / 41
## Totals 40 39 42 0.0248 = 3 / 121
##
## Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>,train = TRUE)`
## =======================================================================
## Top-3 Hit Ratios:
## k hit_ratio
## 1 1 0.975207
## 2 2 1.000000
## 3 3 1.000000
##
##
##
## H2OMultinomialMetrics: glm
## ** Reported on cross-validation data. **
## ** 10-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
##
## Cross-Validation Set Metrics:
## =====================
##
## Extract cross-validation frame with `h2o.getFrame("RTMP_sid_a248_3")`
## MSE: (Extract with `h2o.mse`) 0.02292474
## RMSE: (Extract with `h2o.rmse`) 0.1514092
## Logloss: (Extract with `h2o.logloss`) 0.08051907
## Mean Per-Class Error: 0.03313008
## Null Deviance: (Extract with `h2o.nulldeviance`) 271.7119
## Residual Deviance: (Extract with `h2o.residual_deviance`) 19.48561
## R^2: (Extract with `h2o.r2`) 0.9657509
## AIC: (Extract with `h2o.aic`) NaN
## Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>,xval = TRUE)`
## =======================================================================
## Top-3 Hit Ratios:
## k hit_ratio
## 1 1 0.966942
## 2 2 1.000000
## 3 3 1.000000
##
##
## Cross-Validation Metrics Summary:
## mean sd cv_1_valid cv_2_valid
## accuracy 0.9677244 0.042623263 1.0 1.0
## err 0.03227564 0.042623263 0.0 0.0
## err_count 0.4 0.5163978 0.0 0.0
## logloss 0.08558812 0.062874585 0.07203252 0.045333378
## max_per_class_error 0.06468254 0.090568386 0.0 0.0
## mean_per_class_accuracy 0.97843915 0.030189462 1.0 1.0
## mean_per_class_error 0.021560846 0.030189462 0.0 0.0
## mse 0.024717443 0.023388011 0.017927276 0.009334384
## null_deviance 27.17119 11.21201 27.547539 48.8759
## r2 0.9527126 0.045775954 0.96926755 0.9878878
## residual_deviance 1.9485614 1.1776518 1.7287804 1.9946686
## rmse 0.13811857 0.07916722 0.13389277 0.09661462
## cv_3_valid cv_4_valid cv_5_valid cv_6_valid
## accuracy 1.0 1.0 1.0 1.0
## err 0.0 0.0 0.0 0.0
## err_count 0.0 0.0 0.0 0.0
## logloss 0.02732801 0.04332326 0.011267465 0.16420272
## max_per_class_error 0.0 0.0 0.0 0.0
## mean_per_class_accuracy 1.0 1.0 1.0 1.0
## mean_per_class_error 0.0 0.0 0.0 0.0
## mse 0.0031215441 0.005377187 2.3408124E-4 0.048050396
## null_deviance 17.652943 35.221733 13.292308 13.292308
## r2 0.99487746 0.9921339 0.9997094 0.94035125
## residual_deviance 0.43724817 1.3863443 0.13520958 1.9704326
## rmse 0.055870783 0.07332931 0.015299713 0.21920401
## cv_7_valid cv_8_valid cv_9_valid cv_10_valid
## accuracy 0.9166667 0.9375 0.9230769 0.9
## err 0.083333336 0.0625 0.07692308 0.1
## err_count 1.0 1.0 1.0 1.0
## logloss 0.09878396 0.08604867 0.09288897 0.21467222
## max_per_class_error 0.14285715 0.11111111 0.14285715 0.25
## mean_per_class_accuracy 0.95238096 0.962963 0.95238096 0.9166667
## mean_per_class_error 0.04761905 0.037037037 0.04761905 0.083333336
## mse 0.02629372 0.028165026 0.03327432 0.07539649
## null_deviance 28.087763 36.621292 29.035349 22.084764
## r2 0.89182013 0.9350428 0.95067227 0.8653634
## residual_deviance 2.3708153 2.7535574 2.4151134 4.2934446
## rmse 0.16215338 0.16782439 0.1824125 0.2745842
# Model's performance using test data, includes R^2 and confusion matrix
perf3 <- h2o.performance(glm_model,test)
perf3
## H2OMultinomialMetrics: glm
##
## Test Set Metrics:
## =====================
##
## MSE: (Extract with `h2o.mse`) 0.02441544
## RMSE: (Extract with `h2o.rmse`) 0.1562544
## Logloss: (Extract with `h2o.logloss`) 0.07799228
## Mean Per-Class Error: 0.03333333
## Null Deviance: (Extract with `h2o.nulldeviance`) 63.75638
## Residual Deviance: (Extract with `h2o.residual_deviance`) 4.523552
## R^2: (Extract with `h2o.r2`) 0.9626666
## AIC: (Extract with `h2o.aic`) NaN
## Confusion Matrix: Extract with `h2o.confusionMatrix(<model>, <data>)`)
## =========================================================================
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
## setosa versicolor virginica Error Rate
## setosa 10 0 0 0.0000 = 0 / 10
## versicolor 0 9 1 0.1000 = 1 / 10
## virginica 0 0 9 0.0000 = 0 / 9
## Totals 10 9 10 0.0345 = 1 / 29
##
## Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>, <data>)`
## =======================================================================
## Top-3 Hit Ratios:
## k hit_ratio
## 1 1 0.965517
## 2 2 1.000000
## 3 3 1.000000
To compare the classification models we look at the confusion matrices of the test data. Both of the methods work very well with 1 or 2 errors.