Unverified Commit 9478e52f authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] fix R examples and lgb.plot.interpretation() (#3002)



* [R-package] fix R examples and lgb.plot.interpretation

* remove space in gitignore

* try data.table from conda-forge

* update FAQ
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent 0d3e2046
...@@ -406,6 +406,14 @@ lightgbm_r/* ...@@ -406,6 +406,14 @@ lightgbm_r/*
lightgbm*.tar.gz lightgbm*.tar.gz
lightgbm.Rcheck/ lightgbm.Rcheck/
# Files created by R examples and tests
**/lgb-Dataset.data
**/lgb-model.rds
**/lgb.Dataset.data
**/model.rds
**/model.txt
**/lgb-model.txt
# Files from interactive R sessions # Files from interactive R sessions
.Rproj.user .Rproj.user
**/.Rhistory **/.Rhistory
......
...@@ -41,4 +41,4 @@ Imports: ...@@ -41,4 +41,4 @@ Imports:
utils utils
SystemRequirements: SystemRequirements:
C++11 C++11
RoxygenNote: 7.0.2 RoxygenNote: 7.1.0
...@@ -845,7 +845,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) { ...@@ -845,7 +845,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) {
#' , learning_rate = 1.0 #' , learning_rate = 1.0
#' , early_stopping_rounds = 5L #' , early_stopping_rounds = 5L
#' ) #' )
#' lgb.save(model, "model.txt") #' lgb.save(model, "lgb-model.txt")
#' } #' }
#' @export #' @export
lgb.save <- function(booster, filename, num_iteration = NULL) { lgb.save <- function(booster, filename, num_iteration = NULL) {
......
...@@ -1073,8 +1073,8 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) { ...@@ -1073,8 +1073,8 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) {
#' data(agaricus.train, package = "lightgbm") #' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train #' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label) #' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.save(dtrain, "lgb.Dataset.data") #' lgb.Dataset.save(dtrain, "lgb-Dataset.data")
#' dtrain <- lgb.Dataset("lgb.Dataset.data") #' dtrain <- lgb.Dataset("lgb-Dataset.data")
#' lgb.Dataset.set.categorical(dtrain, 1L:2L) #' lgb.Dataset.set.categorical(dtrain, 1L:2L)
#' #'
#' @rdname lgb.Dataset.set.categorical #' @rdname lgb.Dataset.set.categorical
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
#' ) #' )
#' lgb.plot.interpretation( #' lgb.plot.interpretation(
#' tree_interpretation_dt = tree_interpretation[[1L]] #' tree_interpretation_dt = tree_interpretation[[1L]]
#' , top_n = 5L #' , top_n = 3L
#' ) #' )
#' } #' }
#' @importFrom data.table setnames #' @importFrom data.table setnames
...@@ -141,7 +141,7 @@ multiple.tree.plot.interpretation <- function(tree_interpretation, ...@@ -141,7 +141,7 @@ multiple.tree.plot.interpretation <- function(tree_interpretation,
} }
# create plot # create plot
tree_interpretation[Contribution > 0.0, bar_color := "firebrick"] tree_interpretation[abs(Contribution) > 0.0, bar_color := "firebrick"]
tree_interpretation[Contribution == 0.0, bar_color := "steelblue"] tree_interpretation[Contribution == 0.0, bar_color := "steelblue"]
tree_interpretation[.N:1L, tree_interpretation[.N:1L,
graphics::barplot( graphics::barplot(
......
...@@ -36,9 +36,13 @@ ...@@ -36,9 +36,13 @@
#' data(iris) # Erase iris dataset #' data(iris) # Erase iris dataset
#' #'
#' # We remapped values differently #' # We remapped values differently
#' personal_rules <- list(Species = c("setosa" = 3L, #' personal_rules <- list(
#' "versicolor" = 2L, #' Species = c(
#' "virginica" = 1L)) #' "setosa" = 3L
#' , "versicolor" = 2L
#' , "virginica" = 1L
#' )
#' )
#' newest_iris <- lgb.prepare_rules(data = iris, rules = personal_rules) #' newest_iris <- lgb.prepare_rules(data = iris, rules = personal_rules)
#' str(newest_iris$data) # SUCCESS! #' str(newest_iris$data) # SUCCESS!
#' #'
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#' , learning_rate = 1.0 #' , learning_rate = 1.0
#' , early_stopping_rounds = 5L #' , early_stopping_rounds = 5L
#' ) #' )
#' saveRDS.lgb.Booster(model, "model.rds") #' saveRDS.lgb.Booster(model, "lgb-model.rds")
#' } #' }
#' @export #' @export
saveRDS.lgb.Booster <- function(object, saveRDS.lgb.Booster <- function(object,
......
...@@ -4,8 +4,10 @@ ...@@ -4,8 +4,10 @@
\name{agaricus.test} \name{agaricus.test}
\alias{agaricus.test} \alias{agaricus.test}
\title{Test part from Mushroom Data Set} \title{Test part from Mushroom Data Set}
\format{A list containing a label vector, and a dgCMatrix object with 1611 \format{
rows and 126 variables} A list containing a label vector, and a dgCMatrix object with 1611
rows and 126 variables
}
\usage{ \usage{
data(agaricus.test) data(agaricus.test)
} }
......
...@@ -4,8 +4,10 @@ ...@@ -4,8 +4,10 @@
\name{agaricus.train} \name{agaricus.train}
\alias{agaricus.train} \alias{agaricus.train}
\title{Training part from Mushroom Data Set} \title{Training part from Mushroom Data Set}
\format{A list containing a label vector, and a dgCMatrix object with 6513 \format{
rows and 127 variables} A list containing a label vector, and a dgCMatrix object with 6513
rows and 127 variables
}
\usage{ \usage{
data(agaricus.train) data(agaricus.train)
} }
......
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
\name{bank} \name{bank}
\alias{bank} \alias{bank}
\title{Bank Marketing Data Set} \title{Bank Marketing Data Set}
\format{A data.table with 4521 rows and 17 variables} \format{
A data.table with 4521 rows and 17 variables
}
\usage{ \usage{
data(bank) data(bank)
} }
......
...@@ -24,8 +24,8 @@ Set the categorical features of an \code{lgb.Dataset} object. Use this function ...@@ -24,8 +24,8 @@ Set the categorical features of an \code{lgb.Dataset} object. Use this function
data(agaricus.train, package = "lightgbm") data(agaricus.train, package = "lightgbm")
train <- agaricus.train train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label) dtrain <- lgb.Dataset(train$data, label = train$label)
lgb.Dataset.save(dtrain, "lgb.Dataset.data") lgb.Dataset.save(dtrain, "lgb-Dataset.data")
dtrain <- lgb.Dataset("lgb.Dataset.data") dtrain <- lgb.Dataset("lgb-Dataset.data")
lgb.Dataset.set.categorical(dtrain, 1L:2L) lgb.Dataset.set.categorical(dtrain, 1L:2L)
} }
...@@ -68,7 +68,7 @@ tree_interpretation <- lgb.interprete( ...@@ -68,7 +68,7 @@ tree_interpretation <- lgb.interprete(
) )
lgb.plot.interpretation( lgb.plot.interpretation(
tree_interpretation_dt = tree_interpretation[[1L]] tree_interpretation_dt = tree_interpretation[[1L]]
, top_n = 5L , top_n = 3L
) )
} }
} }
...@@ -48,9 +48,13 @@ all.equal(new_iris$data, newer_iris$data) ...@@ -48,9 +48,13 @@ all.equal(new_iris$data, newer_iris$data)
data(iris) # Erase iris dataset data(iris) # Erase iris dataset
# We remapped values differently # We remapped values differently
personal_rules <- list(Species = c("setosa" = 3L, personal_rules <- list(
"versicolor" = 2L, Species = c(
"virginica" = 1L)) "setosa" = 3L
, "versicolor" = 2L
, "virginica" = 1L
)
)
newest_iris <- lgb.prepare_rules(data = iris, rules = personal_rules) newest_iris <- lgb.prepare_rules(data = iris, rules = personal_rules)
str(newest_iris$data) # SUCCESS! str(newest_iris$data) # SUCCESS!
......
...@@ -39,6 +39,6 @@ model <- lgb.train( ...@@ -39,6 +39,6 @@ model <- lgb.train(
, learning_rate = 1.0 , learning_rate = 1.0
, early_stopping_rounds = 5L , early_stopping_rounds = 5L
) )
lgb.save(model, "model.txt") lgb.save(model, "lgb-model.txt")
} }
} }
...@@ -61,6 +61,6 @@ model <- lgb.train( ...@@ -61,6 +61,6 @@ model <- lgb.train(
, learning_rate = 1.0 , learning_rate = 1.0
, early_stopping_rounds = 5L , early_stopping_rounds = 5L
) )
saveRDS.lgb.Booster(model, "model.rds") saveRDS.lgb.Booster(model, "lgb-model.rds")
} }
} }
...@@ -211,7 +211,7 @@ This is a known bug: `Microsoft/LightGBM#539 <https://github.com/microsoft/Light ...@@ -211,7 +211,7 @@ This is a known bug: `Microsoft/LightGBM#539 <https://github.com/microsoft/Light
3. ``error in data.table::data.table()...argument 2 is NULL`` 3. ``error in data.table::data.table()...argument 2 is NULL``
------------------------------------------------------------- -------------------------------------------------------------
If you experiencing this error when running `lightgbm`, you may be facing the same issue reported in `#2715 <https://github.com/microsoft/LightGBM/issues/2715>`_. If you use ``lgb.dl()`` to build from source (i.e. not using pre-compiled dll), you need to upgrade your version of ``data.table`` to at least version 1.12.0. If you are experiencing this error when running ``lightgbm``, you may be facing the same issue reported in `#2715 <https://github.com/microsoft/LightGBM/issues/2715>`_ and later in `#2989 <https://github.com/microsoft/LightGBM/pull/2989#issuecomment-614374151>`_. We have seen that some in some situations, using ``data.table`` 1.11.x results in this error. To get around this, you can upgrade your version of ``data.table`` to at least version 1.12.0.
------ ------
......
...@@ -227,13 +227,13 @@ def generate_r_docs(app): ...@@ -227,13 +227,13 @@ def generate_r_docs(app):
/home/docs/.conda/bin/conda create -q -y -n r_env \ /home/docs/.conda/bin/conda create -q -y -n r_env \
r-base=3.5.1=h1e0a451_2 \ r-base=3.5.1=h1e0a451_2 \
r-devtools=1.13.6=r351h6115d3f_0 \ r-devtools=1.13.6=r351h6115d3f_0 \
r-data.table=1.11.4=r351h96ca727_0 \
r-jsonlite=1.5=r351h96ca727_0 \ r-jsonlite=1.5=r351h96ca727_0 \
r-matrix=1.2_14=r351h96ca727_0 \ r-matrix=1.2_14=r351h96ca727_0 \
r-testthat=2.0.0=r351h29659fb_0 \ r-testthat=2.0.0=r351h29659fb_0 \
cmake=3.14.0=h52cb24c_0 \ cmake=3.14.0=h52cb24c_0 \
ca-certificates=2019.11.27=0 ca-certificates=2019.11.27=0
/home/docs/.conda/bin/conda install -q -y -n r_env -c conda-forge \ /home/docs/.conda/bin/conda install -q -y -n r_env -c conda-forge \
r-data.table=1.12.8=r35hcdcec82_0 \
r-pkgdown=1.3.0=r35h6115d3f_1001 \ r-pkgdown=1.3.0=r35h6115d3f_1001 \
r-roxygen2=6.1.1=r35h0357c0b_1001 r-roxygen2=6.1.1=r35h0357c0b_1001
source /home/docs/.conda/bin/activate r_env source /home/docs/.conda/bin/activate r_env
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment