Goal is to automate building and tuning a classification model to predict employee attrition, using the h2o::h2o.automl.
Import the cleaned data from Module 7.
library(h2o)
## Warning: package 'h2o' was built under R version 4.2.3
##
## ----------------------------------------------------------------------
##
## Your next step is to start H2O:
## > h2o.init()
##
## For H2O package documentation, ask for help:
## > ??h2o
##
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit https://docs.h2o.ai
##
## ----------------------------------------------------------------------
##
## Attaching package: 'h2o'
## The following objects are masked from 'package:stats':
##
## cor, sd, var
## The following objects are masked from 'package:base':
##
## &&, %*%, %in%, ||, apply, as.factor, as.numeric, colnames,
## colnames<-, ifelse, is.character, is.factor, is.numeric, log,
## log10, log1p, log2, round, signif, trunc
library(tidyverse)
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::day() masks h2o::day()
## ✖ dplyr::filter() masks stats::filter()
## ✖ lubridate::hour() masks h2o::hour()
## ✖ dplyr::lag() masks stats::lag()
## ✖ lubridate::month() masks h2o::month()
## ✖ lubridate::week() masks h2o::week()
## ✖ lubridate::year() masks h2o::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidymodels)
## ── Attaching packages ────────────────────────────────────── tidymodels 1.1.1 ──
## ✔ broom 1.0.5 ✔ rsample 1.2.0
## ✔ dials 1.2.0 ✔ tune 1.1.2
## ✔ infer 1.0.6 ✔ workflows 1.1.3
## ✔ modeldata 1.3.0 ✔ workflowsets 1.0.1
## ✔ parsnip 1.1.1 ✔ yardstick 1.3.0
## ✔ recipes 1.0.9
## Warning: package 'infer' was built under R version 4.2.3
## Warning: package 'modeldata' was built under R version 4.2.3
## Warning: package 'recipes' was built under R version 4.2.3
## Warning: package 'yardstick' was built under R version 4.2.3
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ scales::discard() masks purrr::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ recipes::fixed() masks stringr::fixed()
## ✖ dplyr::lag() masks stats::lag()
## ✖ yardstick::spec() masks readr::spec()
## ✖ recipes::step() masks stats::step()
## • Search for functions across packages at https://www.tidymodels.org/find/
library(tidyquant)
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
##
## Attaching package: 'TTR'
##
## The following object is masked from 'package:dials':
##
## momentum
##
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
data <- read_csv("../00_data/data_wrangled/data_clean.csv") %>%
# h2o requires all variables to be either numeric or factors
mutate(across(where(is.character), factor))
## Rows: 1470 Columns: 32
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): Attrition, BusinessTravel, Department, EducationField, Gender, Job...
## dbl (24): Age, DailyRate, DistanceFromHome, Education, EmployeeNumber, Envir...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
set.seed(1234)
data_split <- initial_split(data, strata = "Attrition")
train_tbl <- training(data_split)
test_tbl <- testing(data_split)
recipe_obj <- recipe(Attrition ~ ., data = train_tbl) %>%
# Remove zero variance variables
step_zv(all_predictors())
# Initialize H2o
h2o.init()
## Connection successful!
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 6 hours 20 minutes
## H2O cluster timezone: America/New_York
## H2O data parsing timezone: UTC
## H2O cluster version: 3.44.0.3
## H2O cluster version age: 4 months and 3 days
## H2O cluster name: H2O_started_from_R_Reed_fyb567
## H2O cluster total nodes: 1
## H2O cluster total memory: 0.75 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
## R Version: R version 4.2.2 (2022-10-31)
## Warning in h2o.clusterInfo():
## Your H2O cluster version is (4 months and 3 days) old. There may be a newer version available.
## Please download and install the latest version from: https://h2o-release.s3.amazonaws.com/h2o/latest_stable.html
split.h2o <- h2o.splitFrame(as.h2o(train_tbl), ratios = c(0.85), seed = 2567)
##
|
| | 0%
|
|======================================================================| 100%
train_h2o <- split.h2o[[1]]
valid_h2o <- split.h2o[[2]]
test_h2o <- as.h2o(test_tbl)
##
|
| | 0%
|
|======================================================================| 100%
y <- "Attrition"
x <- setdiff(names(train_tbl), y)
auto_ml_models_h2o <- h2o.automl(
x = x,
y = y,
training_frame = train_h2o,
validation_frame = valid_h2o,
leaderboard_frame = test_h2o,
max_runtime_secs = 30,
nfolds = 5,
seed = 3456
)
##
|
| | 0%
|
|== | 3%
## 17:17:07.577: User specified a validation frame with cross-validation still enabled. Please note that the models will still be validated using cross-validation only, the validation frame will be used to provide purely informative validation metrics on the trained models.
|
|======= | 10%
|
|============ | 17%
|
|================= | 24%
|
|===================== | 30%
|
|========================== | 37%
|
|=============================== | 44%
|
|==================================== | 51%
|
|======================================== | 58%
|
|============================================= | 64%
|
|================================================== | 71%
|
|======================================================= | 78%
|
|=========================================================== | 85%
|
|================================================================ | 92%
|
|===================================================================== | 99%
|
|======================================================================| 100%
auto_ml_models_h2o@leaderboard
## model_id auc logloss
## 1 StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707 0.8355987 0.3215735
## 2 StackedEnsemble_BestOfFamily_2_AutoML_16_20240423_171707 0.8313376 0.3260096
## 3 StackedEnsemble_BestOfFamily_3_AutoML_16_20240423_171707 0.8307443 0.3257772
## 4 StackedEnsemble_AllModels_3_AutoML_16_20240423_171707 0.8295038 0.3283596
## 5 StackedEnsemble_BestOfFamily_1_AutoML_16_20240423_171707 0.8290723 0.3271145
## 6 GLM_1_AutoML_16_20240423_171707 0.8269687 0.3310361
## aucpr mean_per_class_error rmse mse
## 1 0.9521769 0.3097087 0.3059740 0.09362011
## 2 0.9528207 0.2863269 0.3086916 0.09529052
## 3 0.9521960 0.2863269 0.3087260 0.09531175
## 4 0.9512482 0.3113269 0.3116303 0.09711343
## 5 0.9504737 0.2930421 0.3087986 0.09535655
## 6 0.9469916 0.2796117 0.3081993 0.09498682
##
## [47 rows x 7 columns]
auto_ml_models_h2o@leader
## Model Details:
## ==============
##
## H2OBinomialModel: stackedensemble
## Model ID: StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707
## Model Summary for Stacked Ensemble:
## key value
## 1 Stacking strategy cross_validation
## 2 Number of base models (used / total) 4/6
## 3 # GBM base models (used / total) 1/1
## 4 # XGBoost base models (used / total) 1/1
## 5 # GLM base models (used / total) 1/1
## 6 # DeepLearning base models (used / total) 0/1
## 7 # DRF base models (used / total) 1/2
## 8 Metalearner algorithm GLM
## 9 Metalearner fold assignment scheme Random
## 10 Metalearner nfolds 5
## 11 Metalearner fold_column NA
## 12 Custom metalearner hyperparameters None
##
##
## H2OBinomialMetrics: stackedensemble
## ** Reported on training data. **
##
## MSE: 0.05524508
## RMSE: 0.2350427
## LogLoss: 0.2027111
## Mean Per-Class Error: 0.1253357
## AUC: 0.9648515
## AUCPR: 0.9913961
## Gini: 0.9297029
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 118 36 0.233766 =36/154
## No 13 756 0.016905 =13/769
## Totals 131 792 0.053088 =49/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.625583 0.968610 284
## 2 max f2 0.514595 0.983060 307
## 3 max f0point5 0.748957 0.968657 238
## 4 max accuracy 0.632679 0.946912 282
## 5 max precision 0.999168 1.000000 0
## 6 max recall 0.265650 1.000000 364
## 7 max specificity 0.999168 1.000000 0
## 8 max absolute_mcc 0.632679 0.801043 282
## 9 max min_per_class_accuracy 0.776072 0.909091 226
## 10 max mean_per_class_accuracy 0.748957 0.914242 238
## 11 max tns 0.999168 154.000000 0
## 12 max fns 0.999168 767.000000 0
## 13 max fps 0.044943 154.000000 399
## 14 max tps 0.265650 769.000000 364
## 15 max tnr 0.999168 1.000000 0
## 16 max fnr 0.999168 0.997399 0
## 17 max fpr 0.044943 1.000000 399
## 18 max tpr 0.265650 1.000000 364
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on validation data. **
##
## MSE: 0.06846555
## RMSE: 0.2616592
## LogLoss: 0.2611165
## Mean Per-Class Error: 0.1650771
## AUC: 0.8678822
## AUCPR: 0.9618995
## Gini: 0.7357644
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 16 7 0.304348 =7/23
## No 4 151 0.025806 =4/155
## Totals 20 158 0.061798 =11/178
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.627246 0.964856 157
## 2 max f2 0.438246 0.979772 170
## 3 max f0point5 0.639664 0.962773 155
## 4 max accuracy 0.639664 0.938202 155
## 5 max precision 0.998412 1.000000 0
## 6 max recall 0.438246 1.000000 170
## 7 max specificity 0.998412 1.000000 0
## 8 max absolute_mcc 0.639664 0.720439 155
## 9 max min_per_class_accuracy 0.818803 0.782609 126
## 10 max mean_per_class_accuracy 0.639664 0.853436 155
## 11 max tns 0.998412 23.000000 0
## 12 max fns 0.998412 154.000000 0
## 13 max fps 0.036197 23.000000 177
## 14 max tps 0.438246 155.000000 170
## 15 max tnr 0.998412 1.000000 0
## 16 max fnr 0.998412 0.993548 0
## 17 max fpr 0.036197 1.000000 177
## 18 max tpr 0.438246 1.000000 170
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on cross-validation data. **
## ** 5-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
##
## MSE: 0.09818985
## RMSE: 0.3133526
## LogLoss: 0.3364112
## Mean Per-Class Error: 0.3136558
## AUC: 0.8300627
## AUCPR: 0.9441355
## Gini: 0.6601253
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 62 92 0.597403 =92/154
## No 23 746 0.029909 =23/769
## Totals 85 838 0.124594 =115/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.540936 0.928438 326
## 2 max f2 0.348310 0.965222 370
## 3 max f0point5 0.719592 0.920171 254
## 4 max accuracy 0.540936 0.875406 326
## 5 max precision 0.999195 1.000000 0
## 6 max recall 0.187422 1.000000 393
## 7 max specificity 0.999195 1.000000 0
## 8 max absolute_mcc 0.713515 0.510044 258
## 9 max min_per_class_accuracy 0.842303 0.742523 182
## 10 max mean_per_class_accuracy 0.750678 0.777190 241
## 11 max tns 0.999195 154.000000 0
## 12 max fns 0.999195 768.000000 0
## 13 max fps 0.054103 154.000000 399
## 14 max tps 0.187422 769.000000 393
## 15 max tnr 0.999195 1.000000 0
## 16 max fnr 0.999195 0.998700 0
## 17 max fpr 0.054103 1.000000 399
## 18 max tpr 0.187422 1.000000 393
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## Cross-Validation Metrics Summary:
## mean sd cv_1_valid cv_2_valid cv_3_valid cv_4_valid
## accuracy 0.884252 0.028423 0.913044 0.848168 0.864407 0.884817
## auc 0.829827 0.045185 0.859138 0.848993 0.803784 0.873428
## err 0.115747 0.028423 0.086957 0.151832 0.135593 0.115183
## err_count 21.400000 5.727129 18.000000 29.000000 24.000000 22.000000
## f0point5 0.909742 0.024896 0.935829 0.872162 0.906631 0.905421
## cv_5_valid
## accuracy 0.910828
## auc 0.763789
## err 0.089172
## err_count 14.000000
## f0point5 0.928668
##
## ---
## mean sd cv_1_valid cv_2_valid cv_3_valid
## precision 0.895001 0.029697 0.925926 0.848837 0.899329
## r2 0.276242 0.069370 0.332766 0.289536 0.262182
## recall 0.974962 0.022030 0.977654 0.979866 0.937063
## residual_deviance 123.166010 21.592514 115.167755 148.735320 140.263760
## rmse 0.312648 0.030056 0.279367 0.349105 0.338383
## specificity 0.418511 0.111173 0.500000 0.380952 0.558824
## cv_4_valid cv_5_valid
## precision 0.887006 0.913907
## r2 0.332403 0.164324
## recall 0.987421 0.992806
## residual_deviance 117.098526 94.564660
## rmse 0.305139 0.291248
## specificity 0.375000 0.277778
best_model <- auto_ml_models_h2o@leader
best_model
## Model Details:
## ==============
##
## H2OBinomialModel: stackedensemble
## Model ID: StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707
## Model Summary for Stacked Ensemble:
## key value
## 1 Stacking strategy cross_validation
## 2 Number of base models (used / total) 4/6
## 3 # GBM base models (used / total) 1/1
## 4 # XGBoost base models (used / total) 1/1
## 5 # GLM base models (used / total) 1/1
## 6 # DeepLearning base models (used / total) 0/1
## 7 # DRF base models (used / total) 1/2
## 8 Metalearner algorithm GLM
## 9 Metalearner fold assignment scheme Random
## 10 Metalearner nfolds 5
## 11 Metalearner fold_column NA
## 12 Custom metalearner hyperparameters None
##
##
## H2OBinomialMetrics: stackedensemble
## ** Reported on training data. **
##
## MSE: 0.05524508
## RMSE: 0.2350427
## LogLoss: 0.2027111
## Mean Per-Class Error: 0.1253357
## AUC: 0.9648515
## AUCPR: 0.9913961
## Gini: 0.9297029
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 118 36 0.233766 =36/154
## No 13 756 0.016905 =13/769
## Totals 131 792 0.053088 =49/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.625583 0.968610 284
## 2 max f2 0.514595 0.983060 307
## 3 max f0point5 0.748957 0.968657 238
## 4 max accuracy 0.632679 0.946912 282
## 5 max precision 0.999168 1.000000 0
## 6 max recall 0.265650 1.000000 364
## 7 max specificity 0.999168 1.000000 0
## 8 max absolute_mcc 0.632679 0.801043 282
## 9 max min_per_class_accuracy 0.776072 0.909091 226
## 10 max mean_per_class_accuracy 0.748957 0.914242 238
## 11 max tns 0.999168 154.000000 0
## 12 max fns 0.999168 767.000000 0
## 13 max fps 0.044943 154.000000 399
## 14 max tps 0.265650 769.000000 364
## 15 max tnr 0.999168 1.000000 0
## 16 max fnr 0.999168 0.997399 0
## 17 max fpr 0.044943 1.000000 399
## 18 max tpr 0.265650 1.000000 364
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on validation data. **
##
## MSE: 0.06846555
## RMSE: 0.2616592
## LogLoss: 0.2611165
## Mean Per-Class Error: 0.1650771
## AUC: 0.8678822
## AUCPR: 0.9618995
## Gini: 0.7357644
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 16 7 0.304348 =7/23
## No 4 151 0.025806 =4/155
## Totals 20 158 0.061798 =11/178
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.627246 0.964856 157
## 2 max f2 0.438246 0.979772 170
## 3 max f0point5 0.639664 0.962773 155
## 4 max accuracy 0.639664 0.938202 155
## 5 max precision 0.998412 1.000000 0
## 6 max recall 0.438246 1.000000 170
## 7 max specificity 0.998412 1.000000 0
## 8 max absolute_mcc 0.639664 0.720439 155
## 9 max min_per_class_accuracy 0.818803 0.782609 126
## 10 max mean_per_class_accuracy 0.639664 0.853436 155
## 11 max tns 0.998412 23.000000 0
## 12 max fns 0.998412 154.000000 0
## 13 max fps 0.036197 23.000000 177
## 14 max tps 0.438246 155.000000 170
## 15 max tnr 0.998412 1.000000 0
## 16 max fnr 0.998412 0.993548 0
## 17 max fpr 0.036197 1.000000 177
## 18 max tpr 0.438246 1.000000 170
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on cross-validation data. **
## ** 5-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
##
## MSE: 0.09818985
## RMSE: 0.3133526
## LogLoss: 0.3364112
## Mean Per-Class Error: 0.3136558
## AUC: 0.8300627
## AUCPR: 0.9441355
## Gini: 0.6601253
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 62 92 0.597403 =92/154
## No 23 746 0.029909 =23/769
## Totals 85 838 0.124594 =115/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.540936 0.928438 326
## 2 max f2 0.348310 0.965222 370
## 3 max f0point5 0.719592 0.920171 254
## 4 max accuracy 0.540936 0.875406 326
## 5 max precision 0.999195 1.000000 0
## 6 max recall 0.187422 1.000000 393
## 7 max specificity 0.999195 1.000000 0
## 8 max absolute_mcc 0.713515 0.510044 258
## 9 max min_per_class_accuracy 0.842303 0.742523 182
## 10 max mean_per_class_accuracy 0.750678 0.777190 241
## 11 max tns 0.999195 154.000000 0
## 12 max fns 0.999195 768.000000 0
## 13 max fps 0.054103 154.000000 399
## 14 max tps 0.187422 769.000000 393
## 15 max tnr 0.999195 1.000000 0
## 16 max fnr 0.999195 0.998700 0
## 17 max fpr 0.054103 1.000000 399
## 18 max tpr 0.187422 1.000000 393
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## Cross-Validation Metrics Summary:
## mean sd cv_1_valid cv_2_valid cv_3_valid cv_4_valid
## accuracy 0.884252 0.028423 0.913044 0.848168 0.864407 0.884817
## auc 0.829827 0.045185 0.859138 0.848993 0.803784 0.873428
## err 0.115747 0.028423 0.086957 0.151832 0.135593 0.115183
## err_count 21.400000 5.727129 18.000000 29.000000 24.000000 22.000000
## f0point5 0.909742 0.024896 0.935829 0.872162 0.906631 0.905421
## cv_5_valid
## accuracy 0.910828
## auc 0.763789
## err 0.089172
## err_count 14.000000
## f0point5 0.928668
##
## ---
## mean sd cv_1_valid cv_2_valid cv_3_valid
## precision 0.895001 0.029697 0.925926 0.848837 0.899329
## r2 0.276242 0.069370 0.332766 0.289536 0.262182
## recall 0.974962 0.022030 0.977654 0.979866 0.937063
## residual_deviance 123.166010 21.592514 115.167755 148.735320 140.263760
## rmse 0.312648 0.030056 0.279367 0.349105 0.338383
## specificity 0.418511 0.111173 0.500000 0.380952 0.558824
## cv_4_valid cv_5_valid
## precision 0.887006 0.913907
## r2 0.332403 0.164324
## recall 0.987421 0.992806
## residual_deviance 117.098526 94.564660
## rmse 0.305139 0.291248
## specificity 0.375000 0.277778
Examine the output of h2o.automl
auto_ml_models_h2o %>% typeof()
## [1] "S4"
auto_ml_models_h2o %>% slotNames()
## [1] "project_name" "leader" "leaderboard" "event_log"
## [5] "modeling_steps" "training_info"
auto_ml_models_h2o@leaderboard
## model_id auc logloss
## 1 StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707 0.8355987 0.3215735
## 2 StackedEnsemble_BestOfFamily_2_AutoML_16_20240423_171707 0.8313376 0.3260096
## 3 StackedEnsemble_BestOfFamily_3_AutoML_16_20240423_171707 0.8307443 0.3257772
## 4 StackedEnsemble_AllModels_3_AutoML_16_20240423_171707 0.8295038 0.3283596
## 5 StackedEnsemble_BestOfFamily_1_AutoML_16_20240423_171707 0.8290723 0.3271145
## 6 GLM_1_AutoML_16_20240423_171707 0.8269687 0.3310361
## aucpr mean_per_class_error rmse mse
## 1 0.9521769 0.3097087 0.3059740 0.09362011
## 2 0.9528207 0.2863269 0.3086916 0.09529052
## 3 0.9521960 0.2863269 0.3087260 0.09531175
## 4 0.9512482 0.3113269 0.3116303 0.09711343
## 5 0.9504737 0.2930421 0.3087986 0.09535655
## 6 0.9469916 0.2796117 0.3081993 0.09498682
##
## [47 rows x 7 columns]
auto_ml_models_h2o@leader
## Model Details:
## ==============
##
## H2OBinomialModel: stackedensemble
## Model ID: StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707
## Model Summary for Stacked Ensemble:
## key value
## 1 Stacking strategy cross_validation
## 2 Number of base models (used / total) 4/6
## 3 # GBM base models (used / total) 1/1
## 4 # XGBoost base models (used / total) 1/1
## 5 # GLM base models (used / total) 1/1
## 6 # DeepLearning base models (used / total) 0/1
## 7 # DRF base models (used / total) 1/2
## 8 Metalearner algorithm GLM
## 9 Metalearner fold assignment scheme Random
## 10 Metalearner nfolds 5
## 11 Metalearner fold_column NA
## 12 Custom metalearner hyperparameters None
##
##
## H2OBinomialMetrics: stackedensemble
## ** Reported on training data. **
##
## MSE: 0.05524508
## RMSE: 0.2350427
## LogLoss: 0.2027111
## Mean Per-Class Error: 0.1253357
## AUC: 0.9648515
## AUCPR: 0.9913961
## Gini: 0.9297029
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 118 36 0.233766 =36/154
## No 13 756 0.016905 =13/769
## Totals 131 792 0.053088 =49/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.625583 0.968610 284
## 2 max f2 0.514595 0.983060 307
## 3 max f0point5 0.748957 0.968657 238
## 4 max accuracy 0.632679 0.946912 282
## 5 max precision 0.999168 1.000000 0
## 6 max recall 0.265650 1.000000 364
## 7 max specificity 0.999168 1.000000 0
## 8 max absolute_mcc 0.632679 0.801043 282
## 9 max min_per_class_accuracy 0.776072 0.909091 226
## 10 max mean_per_class_accuracy 0.748957 0.914242 238
## 11 max tns 0.999168 154.000000 0
## 12 max fns 0.999168 767.000000 0
## 13 max fps 0.044943 154.000000 399
## 14 max tps 0.265650 769.000000 364
## 15 max tnr 0.999168 1.000000 0
## 16 max fnr 0.999168 0.997399 0
## 17 max fpr 0.044943 1.000000 399
## 18 max tpr 0.265650 1.000000 364
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on validation data. **
##
## MSE: 0.06846555
## RMSE: 0.2616592
## LogLoss: 0.2611165
## Mean Per-Class Error: 0.1650771
## AUC: 0.8678822
## AUCPR: 0.9618995
## Gini: 0.7357644
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 16 7 0.304348 =7/23
## No 4 151 0.025806 =4/155
## Totals 20 158 0.061798 =11/178
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.627246 0.964856 157
## 2 max f2 0.438246 0.979772 170
## 3 max f0point5 0.639664 0.962773 155
## 4 max accuracy 0.639664 0.938202 155
## 5 max precision 0.998412 1.000000 0
## 6 max recall 0.438246 1.000000 170
## 7 max specificity 0.998412 1.000000 0
## 8 max absolute_mcc 0.639664 0.720439 155
## 9 max min_per_class_accuracy 0.818803 0.782609 126
## 10 max mean_per_class_accuracy 0.639664 0.853436 155
## 11 max tns 0.998412 23.000000 0
## 12 max fns 0.998412 154.000000 0
## 13 max fps 0.036197 23.000000 177
## 14 max tps 0.438246 155.000000 170
## 15 max tnr 0.998412 1.000000 0
## 16 max fnr 0.998412 0.993548 0
## 17 max fpr 0.036197 1.000000 177
## 18 max tpr 0.438246 1.000000 170
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## H2OBinomialMetrics: stackedensemble
## ** Reported on cross-validation data. **
## ** 5-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
##
## MSE: 0.09818985
## RMSE: 0.3133526
## LogLoss: 0.3364112
## Mean Per-Class Error: 0.3136558
## AUC: 0.8300627
## AUCPR: 0.9441355
## Gini: 0.6601253
##
## Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold:
## Left No Error Rate
## Left 62 92 0.597403 =92/154
## No 23 746 0.029909 =23/769
## Totals 85 838 0.124594 =115/923
##
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.540936 0.928438 326
## 2 max f2 0.348310 0.965222 370
## 3 max f0point5 0.719592 0.920171 254
## 4 max accuracy 0.540936 0.875406 326
## 5 max precision 0.999195 1.000000 0
## 6 max recall 0.187422 1.000000 393
## 7 max specificity 0.999195 1.000000 0
## 8 max absolute_mcc 0.713515 0.510044 258
## 9 max min_per_class_accuracy 0.842303 0.742523 182
## 10 max mean_per_class_accuracy 0.750678 0.777190 241
## 11 max tns 0.999195 154.000000 0
## 12 max fns 0.999195 768.000000 0
## 13 max fps 0.054103 154.000000 399
## 14 max tps 0.187422 769.000000 393
## 15 max tnr 0.999195 1.000000 0
## 16 max fnr 0.999195 0.998700 0
## 17 max fpr 0.054103 1.000000 399
## 18 max tpr 0.187422 1.000000 393
##
## Gains/Lift Table: Extract with `h2o.gainsLift(<model>, <data>)` or `h2o.gainsLift(<model>, valid=<T/F>, xval=<T/F>)`
## Cross-Validation Metrics Summary:
## mean sd cv_1_valid cv_2_valid cv_3_valid cv_4_valid
## accuracy 0.884252 0.028423 0.913044 0.848168 0.864407 0.884817
## auc 0.829827 0.045185 0.859138 0.848993 0.803784 0.873428
## err 0.115747 0.028423 0.086957 0.151832 0.135593 0.115183
## err_count 21.400000 5.727129 18.000000 29.000000 24.000000 22.000000
## f0point5 0.909742 0.024896 0.935829 0.872162 0.906631 0.905421
## cv_5_valid
## accuracy 0.910828
## auc 0.763789
## err 0.089172
## err_count 14.000000
## f0point5 0.928668
##
## ---
## mean sd cv_1_valid cv_2_valid cv_3_valid
## precision 0.895001 0.029697 0.925926 0.848837 0.899329
## r2 0.276242 0.069370 0.332766 0.289536 0.262182
## recall 0.974962 0.022030 0.977654 0.979866 0.937063
## residual_deviance 123.166010 21.592514 115.167755 148.735320 140.263760
## rmse 0.312648 0.030056 0.279367 0.349105 0.338383
## specificity 0.418511 0.111173 0.500000 0.380952 0.558824
## cv_4_valid cv_5_valid
## precision 0.887006 0.913907
## r2 0.332403 0.164324
## recall 0.987421 0.992806
## residual_deviance 117.098526 94.564660
## rmse 0.305139 0.291248
## specificity 0.375000 0.277778
# ?h2o.getModel
# ?h2o.saveModel
# ?h2o.loadModel
# h2o.getModel("GLM_1_AutoML_2_20240423_111019") %>%
# h2o.saveModel("h2o_models/")
# best_model <- h2o.loadModel("h2o_models/StackedEnsemble_BestOfFamily_3_AutoML_2_20240423_111019")
predictions <- h2o.predict(best_model, newdata = test_h2o)
##
|
| | 0%
|
|======================================================================| 100%
predictions_tbl <- predictions %>%
as_tibble()
predictions_tbl %>%
bind_cols(test_tbl)
## # A tibble: 369 × 35
## predict Left No Age Attrition BusinessTravel DailyRate Department
## <fct> <dbl> <dbl> <dbl> <fct> <fct> <dbl> <fct>
## 1 Left 0.508 0.492 41 Left Travel_Rarely 1102 Sales
## 2 No 0.0285 0.972 49 No Travel_Frequently 279 Research & …
## 3 No 0.313 0.687 33 No Travel_Frequently 1392 Research & …
## 4 No 0.226 0.774 59 No Travel_Rarely 1324 Research & …
## 5 No 0.0624 0.938 38 No Travel_Frequently 216 Research & …
## 6 No 0.297 0.703 29 No Travel_Rarely 153 Research & …
## 7 No 0.0860 0.914 34 No Travel_Rarely 1346 Research & …
## 8 Left 0.854 0.146 28 Left Travel_Rarely 103 Research & …
## 9 No 0.294 0.706 22 No Non-Travel 1123 Research & …
## 10 No 0.0470 0.953 53 No Travel_Rarely 1219 Sales
## # ℹ 359 more rows
## # ℹ 27 more variables: DistanceFromHome <dbl>, Education <dbl>,
## # EducationField <fct>, EmployeeNumber <dbl>, EnvironmentSatisfaction <dbl>,
## # Gender <fct>, HourlyRate <dbl>, JobInvolvement <dbl>, JobLevel <dbl>,
## # JobRole <fct>, JobSatisfaction <dbl>, MaritalStatus <fct>,
## # MonthlyIncome <dbl>, MonthlyRate <dbl>, NumCompaniesWorked <dbl>,
## # OverTime <fct>, PercentSalaryHike <dbl>, PerformanceRating <dbl>, …
?h2o.performance
performance_h2o <- h2o.performance(best_model, newdata = test_h2o)
typeof(performance_h2o)
## [1] "S4"
slotNames(performance_h2o)
## [1] "algorithm" "on_train" "on_valid" "on_xval" "metrics"
performance_h2o@metrics
## $model
## $model$`__meta`
## $model$`__meta`$schema_version
## [1] 3
##
## $model$`__meta`$schema_name
## [1] "ModelKeyV3"
##
## $model$`__meta`$schema_type
## [1] "Key<Model>"
##
##
## $model$name
## [1] "StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707"
##
## $model$type
## [1] "Key<Model>"
##
## $model$URL
## [1] "/3/Models/StackedEnsemble_BestOfFamily_4_AutoML_16_20240423_171707"
##
##
## $model_checksum
## [1] "1379314344025728720"
##
## $frame
## $frame$name
## [1] "test_tbl_sid_9fb0_3"
##
##
## $frame_checksum
## [1] "-54192601206779456"
##
## $description
## NULL
##
## $scoring_time
## [1] 1.713907e+12
##
## $predictions
## NULL
##
## $MSE
## [1] 0.09362011
##
## $RMSE
## [1] 0.305974
##
## $nobs
## [1] 369
##
## $custom_metric_name
## NULL
##
## $custom_metric_value
## [1] 0
##
## $r2
## [1] 0.3124376
##
## $logloss
## [1] 0.3215735
##
## $AUC
## [1] 0.8355987
##
## $pr_auc
## [1] 0.9521769
##
## $Gini
## [1] 0.6711974
##
## $mean_per_class_error
## [1] 0.3097087
##
## $domain
## [1] "Left" "No"
##
## $cm
## $cm$`__meta`
## $cm$`__meta`$schema_version
## [1] 3
##
## $cm$`__meta`$schema_name
## [1] "ConfusionMatrixV3"
##
## $cm$`__meta`$schema_type
## [1] "ConfusionMatrix"
##
##
## $cm$table
## Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
## Left No Error Rate
## Left 24 36 0.6000 = 36 / 60
## No 6 303 0.0194 = 6 / 309
## Totals 30 339 0.1138 = 42 / 369
##
##
## $thresholds_and_metric_scores
## Metrics for Thresholds: Binomial metrics as a function of classification thresholds
## threshold f1 f2 f0point5 accuracy precision recall specificity
## 1 0.998640 0.006452 0.004042 0.015974 0.165312 1.000000 0.003236 1.000000
## 2 0.997506 0.012862 0.008078 0.031546 0.168022 1.000000 0.006472 1.000000
## 3 0.997235 0.019231 0.012107 0.046729 0.170732 1.000000 0.009709 1.000000
## 4 0.996570 0.025559 0.016129 0.061538 0.173442 1.000000 0.012945 1.000000
## 5 0.996426 0.031847 0.020145 0.075988 0.176152 1.000000 0.016181 1.000000
## absolute_mcc min_per_class_accuracy mean_per_class_accuracy tns fns fps tps
## 1 0.022971 0.003236 0.501618 60 308 0 1
## 2 0.032530 0.006472 0.503236 60 307 0 2
## 3 0.039895 0.009709 0.504854 60 306 0 3
## 4 0.046130 0.012945 0.506472 60 305 0 4
## 5 0.051645 0.016181 0.508091 60 304 0 5
## tnr fnr fpr tpr idx
## 1 1.000000 0.996764 0.000000 0.003236 0
## 2 1.000000 0.993528 0.000000 0.006472 1
## 3 1.000000 0.990291 0.000000 0.009709 2
## 4 1.000000 0.987055 0.000000 0.012945 3
## 5 1.000000 0.983819 0.000000 0.016181 4
##
## ---
## threshold f1 f2 f0point5 accuracy precision recall
## 364 0.211987 0.918276 0.965625 0.875354 0.850949 0.848901 1.000000
## 365 0.159938 0.916914 0.965022 0.873375 0.848238 0.846575 1.000000
## 366 0.156074 0.915556 0.964419 0.871404 0.845528 0.844262 1.000000
## 367 0.146337 0.914201 0.963818 0.869443 0.842818 0.841962 1.000000
## 368 0.053633 0.912851 0.963217 0.867490 0.840108 0.839674 1.000000
## 369 0.050832 0.911504 0.962617 0.865546 0.837398 0.837398 1.000000
## specificity absolute_mcc min_per_class_accuracy mean_per_class_accuracy tns
## 364 0.083333 0.265973 0.083333 0.541667 5
## 365 0.066667 0.237568 0.066667 0.533333 4
## 366 0.050000 0.205458 0.050000 0.525000 3
## 367 0.033333 0.167527 0.033333 0.516667 2
## 368 0.016667 0.118299 0.016667 0.508333 1
## 369 0.000000 0.000000 0.000000 0.500000 0
## fns fps tps tnr fnr fpr tpr idx
## 364 0 55 309 0.083333 0.000000 0.916667 1.000000 363
## 365 0 56 309 0.066667 0.000000 0.933333 1.000000 364
## 366 0 57 309 0.050000 0.000000 0.950000 1.000000 365
## 367 0 58 309 0.033333 0.000000 0.966667 1.000000 366
## 368 0 59 309 0.016667 0.000000 0.983333 1.000000 367
## 369 0 60 309 0.000000 0.000000 1.000000 1.000000 368
##
## $max_criteria_and_metric_scores
## Maximum Metrics: Maximum metrics at their respective thresholds
## metric threshold value idx
## 1 max f1 0.460716 0.935185 338
## 2 max f2 0.257183 0.967439 360
## 3 max f0point5 0.647668 0.924018 310
## 4 max accuracy 0.548472 0.886179 326
## 5 max precision 0.998640 1.000000 0
## 6 max recall 0.257183 1.000000 360
## 7 max specificity 0.998640 1.000000 0
## 8 max absolute_mcc 0.637093 0.537339 313
## 9 max min_per_class_accuracy 0.814929 0.766667 250
## 10 max mean_per_class_accuracy 0.858457 0.775890 231
## 11 max tns 0.998640 60.000000 0
## 12 max fns 0.998640 308.000000 0
## 13 max fps 0.050832 60.000000 368
## 14 max tps 0.257183 309.000000 360
## 15 max tnr 0.998640 1.000000 0
## 16 max fnr 0.998640 0.996764 0
## 17 max fpr 0.050832 1.000000 368
## 18 max tpr 0.257183 1.000000 360
##
## $gains_lift_table
## Gains/Lift Table: Avg response rate: 83.74 %, avg score: 82.73 %
## group cumulative_data_fraction lower_threshold lift cumulative_lift
## 1 1 0.01084011 0.996472 1.194175 1.194175
## 2 2 0.02168022 0.994762 1.194175 1.194175
## 3 3 0.03252033 0.993227 1.194175 1.194175
## 4 4 0.04065041 0.992830 1.194175 1.194175
## 5 5 0.05149051 0.991970 1.194175 1.194175
## 6 6 0.10027100 0.985567 1.127832 1.161900
## 7 7 0.15176152 0.980163 1.194175 1.172850
## 8 8 0.20054201 0.973178 1.061489 1.145762
## 9 9 0.30081301 0.955092 1.097350 1.129625
## 10 10 0.40108401 0.938115 1.194175 1.145762
## 11 11 0.50135501 0.912385 1.129625 1.142535
## 12 12 0.59891599 0.869956 1.161003 1.145543
## 13 13 0.69918699 0.809161 1.000525 1.124746
## 14 14 0.79945799 0.710212 0.968250 1.105118
## 15 15 0.89972900 0.496434 0.839150 1.075477
## 16 16 1.00000000 0.050832 0.322750 1.000000
## response_rate score cumulative_response_rate cumulative_score
## 1 1.000000 0.997488 1.000000 0.997488
## 2 1.000000 0.995871 1.000000 0.996680
## 3 1.000000 0.993833 1.000000 0.995731
## 4 1.000000 0.992918 1.000000 0.995168
## 5 1.000000 0.992459 1.000000 0.994598
## 6 0.944444 0.988266 0.972973 0.991517
## 7 1.000000 0.982770 0.982143 0.988549
## 8 0.888889 0.976622 0.959459 0.985648
## 9 0.918919 0.963735 0.945946 0.978344
## 10 1.000000 0.946735 0.959459 0.970441
## 11 0.945946 0.925915 0.956757 0.961536
## 12 0.972222 0.893785 0.959276 0.950500
## 13 0.837838 0.839927 0.941860 0.934642
## 14 0.810811 0.767138 0.925424 0.913633
## 15 0.702703 0.625716 0.900602 0.881546
## 16 0.270270 0.340207 0.837398 0.827266
## capture_rate cumulative_capture_rate gain cumulative_gain
## 1 0.012945 0.012945 19.417476 19.417476
## 2 0.012945 0.025890 19.417476 19.417476
## 3 0.012945 0.038835 19.417476 19.417476
## 4 0.009709 0.048544 19.417476 19.417476
## 5 0.012945 0.061489 19.417476 19.417476
## 6 0.055016 0.116505 12.783172 16.189976
## 7 0.061489 0.177994 19.417476 17.285021
## 8 0.051780 0.229773 6.148867 14.576227
## 9 0.110032 0.339806 9.734978 12.962477
## 10 0.119741 0.459547 19.417476 14.576227
## 11 0.113269 0.572816 12.962477 14.253477
## 12 0.113269 0.686084 16.100324 14.554321
## 13 0.100324 0.786408 0.052480 12.474599
## 14 0.097087 0.883495 -3.175020 10.511766
## 15 0.084142 0.967638 -16.085017 7.547666
## 16 0.032362 1.000000 -67.725007 0.000000
## kolmogorov_smirnov
## 1 0.012945
## 2 0.025890
## 3 0.038835
## 4 0.048544
## 5 0.061489
## 6 0.099838
## 7 0.161327
## 8 0.179773
## 9 0.239806
## 10 0.359547
## 11 0.439482
## 12 0.536084
## 13 0.536408
## 14 0.516828
## 15 0.417638
## 16 0.000000
##
## $residual_deviance
## [1] 237.3213
##
## $null_deviance
## [1] 327.6898
##
## $AIC
## [1] 247.3213
##
## $loglikelihood
## [1] 0
##
## $null_degrees_of_freedom
## [1] 368
##
## $residual_degrees_of_freedom
## [1] 364
h2o.auc(performance_h2o)
## [1] 0.8355987
h2o.confusionMatrix(performance_h2o)
## Confusion Matrix (vertical: actual; across: predicted) for max f1 @ threshold = 0.46071624816231:
## Left No Error Rate
## Left 24 36 0.600000 =36/60
## No 6 303 0.019417 =6/309
## Totals 30 339 0.113821 =42/369
h2o.metric(performance_h2o)
## Metrics for Thresholds: Binomial metrics as a function of classification thresholds
## threshold f1 f2 f0point5 accuracy precision recall specificity
## 1 0.998640 0.006452 0.004042 0.015974 0.165312 1.000000 0.003236 1.000000
## 2 0.997506 0.012862 0.008078 0.031546 0.168022 1.000000 0.006472 1.000000
## 3 0.997235 0.019231 0.012107 0.046729 0.170732 1.000000 0.009709 1.000000
## 4 0.996570 0.025559 0.016129 0.061538 0.173442 1.000000 0.012945 1.000000
## 5 0.996426 0.031847 0.020145 0.075988 0.176152 1.000000 0.016181 1.000000
## absolute_mcc min_per_class_accuracy mean_per_class_accuracy tns fns fps tps
## 1 0.022971 0.003236 0.501618 60 308 0 1
## 2 0.032530 0.006472 0.503236 60 307 0 2
## 3 0.039895 0.009709 0.504854 60 306 0 3
## 4 0.046130 0.012945 0.506472 60 305 0 4
## 5 0.051645 0.016181 0.508091 60 304 0 5
## tnr fnr fpr tpr idx
## 1 1.000000 0.996764 0.000000 0.003236 0
## 2 1.000000 0.993528 0.000000 0.006472 1
## 3 1.000000 0.990291 0.000000 0.009709 2
## 4 1.000000 0.987055 0.000000 0.012945 3
## 5 1.000000 0.983819 0.000000 0.016181 4
##
## ---
## threshold f1 f2 f0point5 accuracy precision recall
## 364 0.211987 0.918276 0.965625 0.875354 0.850949 0.848901 1.000000
## 365 0.159938 0.916914 0.965022 0.873375 0.848238 0.846575 1.000000
## 366 0.156074 0.915556 0.964419 0.871404 0.845528 0.844262 1.000000
## 367 0.146337 0.914201 0.963818 0.869443 0.842818 0.841962 1.000000
## 368 0.053633 0.912851 0.963217 0.867490 0.840108 0.839674 1.000000
## 369 0.050832 0.911504 0.962617 0.865546 0.837398 0.837398 1.000000
## specificity absolute_mcc min_per_class_accuracy mean_per_class_accuracy tns
## 364 0.083333 0.265973 0.083333 0.541667 5
## 365 0.066667 0.237568 0.066667 0.533333 4
## 366 0.050000 0.205458 0.050000 0.525000 3
## 367 0.033333 0.167527 0.033333 0.516667 2
## 368 0.016667 0.118299 0.016667 0.508333 1
## 369 0.000000 0.000000 0.000000 0.500000 0
## fns fps tps tnr fnr fpr tpr idx
## 364 0 55 309 0.083333 0.000000 0.916667 1.000000 363
## 365 0 56 309 0.066667 0.000000 0.933333 1.000000 364
## 366 0 57 309 0.050000 0.000000 0.950000 1.000000 365
## 367 0 58 309 0.033333 0.000000 0.966667 1.000000 366
## 368 0 59 309 0.016667 0.000000 0.983333 1.000000 367
## 369 0 60 309 0.000000 0.000000 1.000000 1.000000 368