Skip to content

Commit

Permalink
[R-package] Turn matrix to storage mode "double" (microsoft#3140)
Browse files Browse the repository at this point in the history
* Turn matrix to storage mode "double"

* added "test_Predictor.R" to R tests with check for integer storage mode during prediction

* Explicit integers in test_Preditor to avoid TravisCI failure

* properly aligning comment on storage.mode

Co-authored-by: James Lamb <jaylamb20@gmail.com>

* split double assignment into two lines

Co-authored-by: James Lamb <jaylamb20@gmail.com>
  • Loading branch information
2 people authored and ChipKerchner committed Jun 11, 2020
1 parent 84497b6 commit 3c778f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R-package/R/lgb.Predictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ Predictor <- R6::R6Class(

# Check if data is a matrix
if (is.matrix(data)) {
# Check whether matrix is the correct type first ("double")
if (storage.mode(data) != "double") {
storage.mode(data) <- "double"
}
preds <- lgb.call(
"LGBM_BoosterPredictForMat_R"
, ret = preds
Expand Down
18 changes: 18 additions & 0 deletions R-package/tests/testthat/test_Predictor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
context("Predictor")

test_that("predictions do not fail for integer input", {
X <- as.matrix(as.integer(iris[, "Species"]), ncol = 1L)
y <- iris[["Sepal.Length"]]
dtrain <- lgb.Dataset(X, label = y)
fit <- lgb.train(
data = dtrain
, objective = "regression"
, verbose = -1L
)
X_double <- X[c(1L, 51L, 101L), , drop = FALSE]
X_integer <- X_double
storage.mode(X_double) <- "double"
pred_integer <- predict(fit, X_integer)
pred_double <- predict(fit, X_double)
expect_equal(pred_integer, pred_double)
})

0 comments on commit 3c778f9

Please sign in to comment.