-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add error in exmplain for UpliftDRF models
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
h2o-py/tests/testdir_algos/uplift/pyunit_uplift_rf_explain.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys, os | ||
|
||
sys.path.insert(1, os.path.join("..", "..", "..")) | ||
import h2o | ||
from tests import pyunit_utils | ||
from h2o.estimators import H2OUpliftRandomForestEstimator | ||
|
||
|
||
def uplift_random_forest_explain(): | ||
print("Uplift Distributed Random Forest explain test") | ||
seed = 12345 | ||
|
||
treatment_column = "treatment" | ||
response_column = "outcome" | ||
x_names = ["feature_"+str(x) for x in range(1, 3)] | ||
|
||
train_h2o = h2o.upload_file(pyunit_utils.locate("smalldata/uplift/upliftml_train.csv")) | ||
train_h2o[treatment_column] = train_h2o[treatment_column].asfactor() | ||
train_h2o[response_column] = train_h2o[response_column].asfactor() | ||
|
||
valid_h2o = h2o.upload_file(pyunit_utils.locate("smalldata/uplift/upliftml_test.csv")) | ||
valid_h2o[treatment_column] = valid_h2o[treatment_column].asfactor() | ||
valid_h2o[response_column] = valid_h2o[response_column].asfactor() | ||
|
||
ntrees = 2 | ||
max_depth = 2 | ||
min_rows = 10 | ||
sample_rate = 0.8 | ||
|
||
uplift_model = H2OUpliftRandomForestEstimator( | ||
ntrees=ntrees, | ||
max_depth=max_depth, | ||
treatment_column=treatment_column, | ||
min_rows=min_rows, | ||
seed=seed, | ||
sample_rate=sample_rate, | ||
score_each_iteration=True | ||
) | ||
|
||
uplift_model.train(y=response_column, x=x_names, training_frame=train_h2o, validation_frame=valid_h2o) | ||
print(uplift_model) | ||
|
||
# should throw error | ||
try: | ||
uplift_model.explain(valid_h2o) | ||
except ValueError: | ||
assert True, "The explain function should fail with UpliftDRF." | ||
|
||
|
||
if __name__ == "__main__": | ||
pyunit_utils.standalone_test(uplift_random_forest_explain) | ||
else: | ||
uplift_random_forest_explain() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
setwd(normalizePath(dirname(R.utils::commandArgs(asValues = TRUE)$"f"))) | ||
source("../../../scripts/h2o-r-test-setup.R") | ||
|
||
|
||
test.uplift <- function() { | ||
ntrees <- 2 | ||
max_depth <- 2 | ||
min_rows <- 10 | ||
sample_rate <- 0.8 | ||
seed <- 42 | ||
set.seed(seed) | ||
x <- c("feature_1", "feature_2", "feature_3") | ||
y <- "outcome" | ||
treatment_col <- "treatment" | ||
|
||
# Test data preparation for each implementation | ||
train <- h2o.importFile(path=locate("smalldata/uplift/upliftml_train.csv"), | ||
col.types=list(by.col.name=c(treatment_col, y), types=c("factor", "factor"))) | ||
test <- h2o.importFile(path=locate("smalldata/uplift/upliftml_test.csv"), | ||
col.types=list(by.col.name=c(treatment_col, y), types=c("factor", "factor"))) | ||
|
||
model <- h2o.upliftRandomForest( | ||
x = x, | ||
y = y, | ||
training_frame = train, | ||
validation_frame = test, | ||
treatment_column = treatment_col, | ||
ntrees = ntrees, | ||
max_depth = max_depth, | ||
min_rows = min_rows, | ||
sample_rate = sample_rate, | ||
score_each_iteration=TRUE, | ||
seed = seed) | ||
|
||
print(model) | ||
expect_error(h2o.explain(model, test)) | ||
} | ||
|
||
doTest("Uplift Random Forest Test: Test H2O RF uplift", test.uplift) |