Skip to content

Commit

Permalink
fix R and Py example
Browse files Browse the repository at this point in the history
  • Loading branch information
maurever committed Dec 1, 2023
1 parent 4d30c38 commit e18157f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions h2o-py/h2o/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4668,12 +4668,12 @@ def match(self, table, nomatch=float("nan"), start_index=1):
:examples:
>>> iris = h2o.import_file("http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris.csv")
>>> iris = h2o.import_file("h2o://iris")
>>> match_col = iris["C5"].match(['Iris-setosa', 'Iris-versicolor'])
>>> match_col.names = ['match']
>>> iris_match = iris.cbind(match_col)
>>> iris_split = iris_match.split_frame(ratios=[0.05], seed=1)[0]
>>> print(iris_split)
>>> iris_split
"""
return H2OFrame._expr(expr=ExprNode("match", self, table, nomatch, start_index))

Expand Down
8 changes: 4 additions & 4 deletions h2o-r/h2o-package/R/frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -863,11 +863,11 @@ cut.H2OFrame <- h2o.cut
#' @examples
#' \dontrun{
#' h2o.init()
#' iris <- as.h2o(iris)
#' match_col <- h2o.match(iris$Species, c("setosa", "versicolor", "setosa"))
#' iris_match <- h2o.cbind(iris, match_col)
#' frame <- as.h2o(iris)
#' match_col <- h2o.match(frame$Species, c("setosa", "versicolor", "setosa"))
#' iris_match <- h2o.cbind(frame, match_col)
#' iris_split <- h2o.splitFrame(iris_match, ratios=0.05, seed=1)
#' print(iris_split[1])
#' iris_split[1]
#' }
#' @export
h2o.match <- function(x, table, nomatch=NA_integer_, start_index=1) {
Expand Down
34 changes: 17 additions & 17 deletions h2o-r/tests/testdir_munging/slice/runit_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,74 @@ source("../../../scripts/h2o-r-test-setup.R")

test.match <- function() {

iris <- as.h2o(iris)
frame <- as.h2o(iris)

# compare h2o and base %in%
h2o_in <- 'setosa' %in% iris$Species
base_in <- base::`%in%`("setosa", as.vector(iris$Species))
h2o_in <- 'setosa' %in% frame$Species
base_in <- base::`%in%`("setosa", as.vector(frame$Species))
expect_equal(h2o_in, base_in)

sub_h2o_in <- iris$Species %in% c("setosa", "versicolor")
hh_in <- iris[sub_h2o_in,]
sub_h2o_in <- frame$Species %in% c("setosa", "versicolor")
hh_in <- frame[sub_h2o_in,]
expect_equal(dim(hh_in), c(100, 5))

sub_base_in <- base::`%in%`(as.vector(iris$Species), c("setosa", "versicolor"))
hh_in_base <- iris[as.h2o(sub_base_in),]
sub_base_in <- base::`%in%`(as.vector(frame$Species), c("setosa", "versicolor"))
hh_in_base <- frame[as.h2o(sub_base_in),]
expect_equal(dim(hh_in_base), c(100, 5))

expect_equal(hh_in, hh_in_base)

# compare h2o and base match
# string values, default setting
sub_h2o_match <- h2o.match(iris$Species, c("setosa", "versicolor"))
sub_h2o_match <- h2o.match(frame$Species, c("setosa", "versicolor"))
sub_h2o_match <- as.vector(sub_h2o_match)
expect_equal(sub_h2o_match[1], 1)
expect_equal(sub_h2o_match[51], 2)
expect_equal(sub_h2o_match[101], NA_integer_)

sub_base_match <- base::match(as.vector(iris$Species), c("setosa", "versicolor"))
sub_base_match <- base::match(as.vector(frame$Species), c("setosa", "versicolor"))
expect_equal(sub_base_match[1], 1)
expect_equal(sub_base_match[51], 2)
expect_equal(sub_base_match[101], NA_integer_)

expect_equal(sub_h2o_match, sub_base_match)

# string values, nomatch=0
sub_h2o_match <- h2o.match(iris$Species, c("setosa", "versicolor"), nomatch=0)
sub_h2o_match <- h2o.match(frame$Species, c("setosa", "versicolor"), nomatch=0)
sub_h2o_match <- as.vector(sub_h2o_match)
expect_equal(sub_h2o_match[1], 1)
expect_equal(sub_h2o_match[51], 2)
expect_equal(sub_h2o_match[101], 0)

# string values, start_index=0
sub_h2o_match <- h2o.match(iris$Species, c("setosa", "versicolor"), start_index=0)
sub_h2o_match <- h2o.match(frame$Species, c("setosa", "versicolor"), start_index=0)
sub_h2o_match <- as.vector(sub_h2o_match)
expect_equal(sub_h2o_match[1], 0)
expect_equal(sub_h2o_match[51], 1)
expect_equal(sub_h2o_match[101], NA_integer_)

sub_h2o_in <- iris$Sepal.Length %in% c(5.1)
hh_in <- iris[sub_h2o_in,]
sub_h2o_in <- frame$Sepal.Length %in% c(5.1)
hh_in <- frame[sub_h2o_in,]
expect_equal(dim(hh_in), c(9,5))

# numeric value, default setting
sub_h2o_match <- h2o.match(iris$Sepal.Length, c(5.1))
sub_h2o_match <- h2o.match(frame$Sepal.Length, c(5.1))
sub_h2o_match <- as.vector(sub_h2o_match)
expect_equal(sub_h2o_match[1], 1)
expect_equal(sub_h2o_match[18], 1)
expect_equal(sub_h2o_match[20], 1)
expect_equal(sub_h2o_match[2], NA_integer_)

# string values, duplicates in match values
sub_h2o_match <- h2o.match(iris$Species, c("setosa", "versicolor", "setosa"))
sub_h2o_match <- h2o.match(frame$Species, c("setosa", "versicolor", "setosa"))
sub_h2o_match <- as.vector(sub_h2o_match)
expect_equal(sub_h2o_match[1], 1)
expect_equal(sub_h2o_match[51], 2)
expect_equal(sub_h2o_match[101], NA_integer_)

# test doc example
match_col <- h2o.match(iris$Species, c("setosa", "versicolor", "setosa"))
iris_match <- h2o.cbind(iris, match_col)
match_col <- h2o.match(frame$Species, c("setosa", "versicolor", "setosa"))
iris_match <- h2o.cbind(frame, match_col)
splited <- h2o.splitFrame(iris_match, ratios=0.05, seed=1)
print(splited[1])
}
Expand Down

0 comments on commit e18157f

Please sign in to comment.