This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

plot(cars)

# Install and load H2O package
install.packages("h2o")
Installing package into ‘/cloud/lib/x86_64-pc-linux-gnu-library/4.4’
(as ‘lib’ is unspecified)
also installing the dependencies ‘bitops’, ‘RCurl’

trying URL 'http://rspm/default/__linux__/focal/latest/src/contrib/bitops_1.0-9.tar.gz'
Content type 'application/x-gzip' length 25355 bytes (24 KB)
==================================================
downloaded 24 KB

trying URL 'http://rspm/default/__linux__/focal/latest/src/contrib/RCurl_1.98-1.16.tar.gz'
Content type 'application/x-gzip' length 1064350 bytes (1.0 MB)
==================================================
downloaded 1.0 MB

trying URL 'http://rspm/default/__linux__/focal/latest/src/contrib/h2o_3.44.0.3.tar.gz'
Content type 'application/x-gzip' length 266595032 bytes (254.2 MB)
==================================================
downloaded 254.2 MB

* installing *binary* package ‘bitops’ ...
* DONE (bitops)
* installing *binary* package ‘RCurl’ ...
* DONE (RCurl)
* installing *binary* package ‘h2o’ ...
* DONE (h2o)

The downloaded source packages are in
    ‘/tmp/RtmpRWVXeM/downloaded_packages’
library(h2o)

----------------------------------------------------------------------

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
# Initialize H2O
h2o.init(nthreads = -1)

H2O is not running yet, starting it now...

Note:  In case of errors look at the following log files:
    /tmp/RtmpRWVXeM/file14537d4cede/h2o_r2993546_started_from_r.out
    /tmp/RtmpRWVXeM/file14579e70688/h2o_r2993546_started_from_r.err
openjdk version "11.0.22" 2024-01-16
OpenJDK Runtime Environment (build 11.0.22+7-post-Ubuntu-0ubuntu220.04.1)
OpenJDK 64-Bit Server VM (build 11.0.22+7-post-Ubuntu-0ubuntu220.04.1, mixed mode, sharing)

Starting H2O JVM and connecting: ... Connection successful!

R is connected to the H2O cluster: 
    H2O cluster uptime:         2 seconds 805 milliseconds 
    H2O cluster timezone:       UTC 
    H2O data parsing timezone:  UTC 
    H2O cluster version:        3.44.0.3 
    H2O cluster version age:    1 year, 1 month and 14 days 
    H2O cluster name:           H2O_started_from_R_r2993546_vag819 
    H2O cluster total nodes:    1 
    H2O cluster total memory:   0.24 GB 
    H2O cluster total cores:    1 
    H2O cluster allowed cores:  1 
    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.4.2 (2024-10-31) 
Warning in h2o.clusterInfo() : 
Your H2O cluster version is (1 year, 1 month and 14 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
# Import dataset
datasets <- "https://raw.githubusercontent.com/DarrenCook/h2o/bk/datasets/"
data <- h2o.importFile(paste0(datasets, "iris_wheader.csv"))

  |                                                                                                    
  |                                                                                              |   0%
  |                                                                                                    
  |==============================================================================================| 100%
# Define target and predictor variables
y <- "class"
x <- setdiff(names(data), y)

# Split dataset into training and test sets
parts <- h2o.splitFrame(data, 0.8)
train <- parts[[1]]
test <- parts[[2]]

# Train deep learning model
m <- h2o.deeplearning(x = x, y = y, training_frame = train)

  |                                                                                                    
  |                                                                                              |   0%
  |                                                                                                    
  |==============================================================================================| 100%
# Make predictions
p <- h2o.predict(m, test)

  |                                                                                                    
  |                                                                                              |   0%
  |                                                                                                    
  |==============================================================================================| 100%
# Evaluate model performance
h2o.mse(m)
[1] 0.319801
h2o.confusionMatrix(m)
Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
# Convert predictions to data frame
as.data.frame(p)

# Compare predictions with actual class
as.data.frame(h2o.cbind(p$predict, test$class))

# Calculate accuracy
mean(p$predict == test$class)
[1] 0.7
# Alternative performance metrics
h2o.performance(m, test)
H2OMultinomialMetrics: deeplearning

Test Set Metrics: 
=====================

MSE: (Extract with `h2o.mse`) 0.2680958
RMSE: (Extract with `h2o.rmse`) 0.5177797
Logloss: (Extract with `h2o.logloss`) 1.288047
Mean Per-Class Error: 0.3333333
AUC: (Extract with `h2o.auc`) NaN
AUCPR: (Extract with `h2o.aucpr`) NaN
Confusion Matrix: Extract with `h2o.confusionMatrix(<model>, <data>)`)
=========================================================================
Confusion Matrix: Row labels: Actual class; Column labels: Predicted class

Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>, <data>)`
=======================================================================
Top-3 Hit Ratios: 
NANANA
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKVGhpcyBpcyBhbiBbUiBNYXJrZG93bl0oaHR0cDovL3JtYXJrZG93bi5yc3R1ZGlvLmNvbSkgTm90ZWJvb2suIFdoZW4geW91IGV4ZWN1dGUgY29kZSB3aXRoaW4gdGhlIG5vdGVib29rLCB0aGUgcmVzdWx0cyBhcHBlYXIgYmVuZWF0aCB0aGUgY29kZS4gCgpUcnkgZXhlY3V0aW5nIHRoaXMgY2h1bmsgYnkgY2xpY2tpbmcgdGhlICpSdW4qIGJ1dHRvbiB3aXRoaW4gdGhlIGNodW5rIG9yIGJ5IHBsYWNpbmcgeW91ciBjdXJzb3IgaW5zaWRlIGl0IGFuZCBwcmVzc2luZyAqQ3RybCtTaGlmdCtFbnRlciouIAoKYGBge3J9CnBsb3QoY2FycykKYGBgCgpgYGB7cn0KIyBJbnN0YWxsIGFuZCBsb2FkIEgyTyBwYWNrYWdlCmluc3RhbGwucGFja2FnZXMoImgybyIpCmxpYnJhcnkoaDJvKQoKIyBJbml0aWFsaXplIEgyTwpoMm8uaW5pdChudGhyZWFkcyA9IC0xKQoKIyBJbXBv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