Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Isolation Forest grid search test and API [nocheck] #5660

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions h2o-r/h2o-package/R/grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ h2o.grid <- function(algorithm,
parallelism = 1)
{
#Unsupervised algos to account for in grid (these algos do not need response)
unsupervised_algos <- c("kmeans", "pca", "svd", "glrm", "extendedisolationforest")
unsupervised_algos <- c("kmeans", "pca", "svd", "glrm", "isolationforest", "extendedisolationforest")
forceSupervisedBehaviour <- if (!is.null(is_supervised)) is_supervised else FALSE
# Parameter list
dots <- list(...)
# Add x, y, and training_frame
if(!(algorithm %in% c(unsupervised_algos, toupper(unsupervised_algos)))) {
if(!(algorithm %in% c(unsupervised_algos, toupper(unsupervised_algos))) || forceSupervisedBehaviour) {
if(!missing(y)) {
dots$y <- y
} else {
Expand All @@ -94,7 +95,7 @@ h2o.grid <- function(algorithm,
stop("Must specify training frame, training_frame")
}
# If x is missing, then assume user wants to use all columns as features for supervised models only
if(!(algorithm %in% c(unsupervised_algos, toupper(unsupervised_algos)))) {
if(!(algorithm %in% c(unsupervised_algos, toupper(unsupervised_algos))) || forceSupervisedBehaviour) {
if (missing(x)) {
if (is.numeric(y)) {
dots$x <- setdiff(col(training_frame), y)
Expand Down
1 change: 1 addition & 0 deletions h2o-r/h2o-package/R/models.R
Original file line number Diff line number Diff line change
Expand Up @@ -4048,6 +4048,7 @@ h2o.sdev <- function(object) {
if (algo == "kmeans" ||
algo == "glrm" ||
algo == "pca" ||
algo == "isolationforest" ||
algo == "extendedisolationforest" ||
(algo == "deeplearning" && !is.null(params$autoencoder) && params$autoencoder)) {
FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ source("../../../scripts/h2o-r-test-setup.R")



test.grid.resume <- function() {
test.grid.isolationforest.supervised <- function() {
iris.hex <-
h2o.importFile(path = locate("smalldata/iris/iris.csv"),
destination_frame = "iris.hex")

ntrees_opts = c(1, 5)
size_of_hyper_space = length(ntrees_opts)
ntrees_opts <- c(1, 5)
size_of_hyper_space <- length(ntrees_opts)

hyper_parameters = list(ntrees = ntrees_opts)
hyper_parameters <- list(ntrees = ntrees_opts)
baseline_grid <-
h2o.grid(
"isolationforest",
grid_id = "isofor_grid_test",
x = 1:4,
y = 5,
training_frame = iris.hex,
hyper_params = hyper_parameters
is_supervised = TRUE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set the flag here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made IF unsupervised by default. This test is using the y parameter and then it should be supervised.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very surprised this works - I thought IF can only have a labeled validation frame, not the training frame. Hmm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check the output of the grid search, since I have only used API I didn't think about that. Good point 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to say that I didn't change the test, I just focused that the same code will be used with this flag. I will check what is the actual output of the grid search :)

hyper_params = hyper_parameters,
parallelism = 0
)
grid_id <- baseline_grid@grid_id
expect_equal(length(baseline_grid@model_ids),
length(ntrees_opts))
expect_equal(length(baseline_grid@model_ids), size_of_hyper_space)

# Grid search with validation frame and validation_response_column
train <-
Expand Down Expand Up @@ -53,8 +54,12 @@ test.grid.resume <- function() {
training_frame = train,
validation_frame = test,
hyper_params = hyper_parameters,
validation_response_column = "label"
is_supervised = TRUE,
validation_response_column = "label",
parallelism = 0
)


}

doTest("Parallel Grid Search test", test.grid.resume)
doTest("Parallel Grid Search test", test.grid.isolationforest.supervised)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
setwd(normalizePath(dirname(
R.utils::commandArgs(asValues = TRUE)$"f"
)))
source("../../../scripts/h2o-r-test-setup.R")


test.grid.isolationforest.unsupervised.default <- function() {
single_blob.hex <-
h2o.importFile(path = locate("smalldata/anomaly/single_blob.csv"),
destination_frame = "single_blob.hex")

ntrees <- c(25, 50, 100)
sample_size <- c(64, 128, 256)
size_of_hyper_space <- length(ntrees)*length(sample_size)

hyper_parameters <- list(ntrees = ntrees, sample_size = sample_size)
baseline_grid <-
h2o.grid(
"isolationforest",
grid_id = "isofor_grid_test",
x = c(1, 2),
training_frame = single_blob.hex,
hyper_params = hyper_parameters,
parallelism = 0
)
print(paste("Expected size of hyperparameter space is", length(baseline_grid@model_ids)))
expect_equal(length(baseline_grid@model_ids), size_of_hyper_space)
}


doTest("Parallel Grid Search test for Isolation Forest - default grid search is in unsupervised fashion", test.grid.isolationforest.unsupervised.default)